Windows Phone 7 App Development First Look

Windows Phone 7 App Development First Look

with Joe Marini

 


In Windows Phone 7 App Development First Look, Joe Marini introduces the concepts and techniques behind developing Silverlight-based applications for Windows Phone 7. This course enables developers to apply their existing Silverlight programming skills and learn the fundamentals of building and debugging applications with Visual Studio 2010 and Expression Blend 4 for Windows Phone 7. The development software and accompanying phone emulator give developers an early taste of how simple it is to create custom apps. Exercise files accompany the course.
Topics include:
  • Creating application projects in Visual Studio and Expression Blend
  • Adding application controls to Windows Phone pages
  • Running and debugging in Visual Studio
  • Testing a Windows Phone app with the Emulator
  • Handling page orientation changes
  • Adding an application bar
  • Customizing text box input with InputScopes
  • Consuming Web content with the WebBrowser control

show more

author
Joe Marini
subject
Developer, Mobile Apps
software
Windows Phone 7
level
Intermediate
duration
1h 48m
released
Jun 15, 2010

Share this course

Ready to join? subscribe


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Welcome
00:03Hi! I am Joe Marini, and I want to welcome you to Windows Phone 7 Application
00:07Development, A First Look.
00:09Mobile application development has absolutely exploded over the last few years.
00:14These little guys have revolutionized the way that we entertain ourselves, get
00:17our news, and keep in touch with the world around us.
00:20This course is designed to be a quick introduction to the fundamental
00:23concepts and techniques of building applications for Microsoft's forthcoming
00:27Windows Phone 7 platform.
00:30In this course, we will see how to download and install the free tools for
00:33developing Windows Phone 7 applications.
00:35I will demonstrate the fundamental principles for building mobile applications
00:40on Windows Phone, and we will dig a little deeper into areas such as
00:43integrating with the Windows Phone Shell and embedding rich Web content into your applications.
00:49Now in this course, I am going to assume that you are already familiar with
00:51Silverlight Development because Silverlight is the foundation of building
00:54applications on Windows Phone 7.
00:56So if you are new to Silverlight, you may want to watch the Silverlight
00:59Essential Training course here on lynda.com first.
01:02Windows Phone 7 represents a great advance in mobile application platforms,
01:06and I am excited to be able to show you how to build applications that will wow the world.
01:10Let's get started!
Collapse this transcript
Using the exercise files
00:01If you have access to the exercise files for this course, you can put them on
00:04your desktop, just as I have done here, or anywhere else you want.
00:09The top folder contains the subfolders for each chapter, and each one of those
00:13contains the exercise files themselves.
00:16Now for chapters 02 and 03, I want to point out that there is only set of
00:20exercise files for the entire chapter, so you should watch the entire chapter
00:25all the way through, resist the temptation to jump around, and then work on the
00:29exercise files for those two chapters.
00:32When you get to chapter 04, there are four different examples in here, and you
00:35can do these in any order you like.
00:37But for chapters 02 and 03, I really strongly suggest that you follow the chapter
00:41in order and then work on the exercise files.
00:44Now if you don't have access to these files, you can follow along with me as
00:49we work on the chapter together, or you can use your own files as we proceed
00:53through the course.
Collapse this transcript
1. Getting Started
Downloading and installing the tools
00:01Okay, so before we can begin learning about building Windows Phone 7
00:05applications, the first we need to do is go and get the tools.
00:09This is the home site for all things related to developing applications for Windows Phone.
00:15So you can see here, I have gone to the Home page and right here on the
00:18right-hand side is a big banner link to get the tools.
00:22So I am going to go ahead and click on that, and this will take me off to
00:25the Download Center where I can get the latest version of the Windows Phone Developer tools.
00:31Now at the time of this recording, it's in CTP version, but this will be
00:35refreshed with the full tools when they are released.
00:38So I'll need to do is click the link
00:40this says get the download files, and that's these guys right here.
00:44Now I have already got the tools installed on my system, so I am not going to go
00:47through the big long process of showing you how all this is done.
00:50Suffice it to say all you need to is click on this little Download button right
00:55here, and that one click will install everything that you need to build Windows
01:00Phone 7 applications using Visual Studio 2010 Express for Windows Phone.
01:06Now once you have downloaded that and installed that, you are probably also
01:10going to want to get the Microsoft Expression Blend application, and you can see
01:15down here in the section labeled "What Others Are Downloading," there are few
01:19links that are directly related to this one.
01:21So here is the link for Expression Blend 4, and that's another application
01:26that Microsoft produces in the Expression family for building
01:29Silverlight-based applications, and this one allows you to build applications
01:34in Silverlight for the Windows Phone.
01:36Along with Expression Blend 4, you will also want to download the Add-in
01:41Preview for Windows Phone, and you will also want to get the software
01:46development kit for Windows Phone, and these three things right here, all of
01:50these are for Expression Blend, and we will taking a look at that a little bit
01:54later on in the title.
01:56Okay, so once you have downloaded the tools, you are pretty much ready to go, and
02:01what we are going to do now is take a look at how to download and install these
02:05sample applications.
Collapse this transcript
Downloading the sample apps
00:01Once you've downloaded and installed the applications, we need to now go and
00:06download and install the samples.
00:08So let's go ahead and go back to the developer.windowsphone.com Web site and
00:15see up at the top here, there are a few links. The one labeled Resources is the
00:19one we're going to be interested in because that's where the sample apps are located.
00:24So this will take you to the Windows Phone Development section on the Microsoft
00:29Developer Network Web site, and under the left-hand column here you can see we
00:33are in the Windows Phone Development section, and there is a link labeled "Code
00:37Samples." It's over here in the left, and it's also down here in this table.
00:40So either one of those would get into the right place, so I'm going ahead and
00:43click on Code Samples.
00:45Now there are seven of these code samples currently up on MSDN for downloading
00:50and exploring, and they are listed right here.
00:53There is one that shows how to build a Mini Web browser using the Web browser
00:58control that's included with the SDK for Windows Phone.
01:01There is also a sample that shows you how to use the Application Bar, and we
01:05will see what that is when we get into the anatomy of the Windows Phone apps,
01:09but it's a common piece of UI that all Windows Phone applications can use to
01:13provide access to quick pieces of functionality.
01:16There are also some samples that deal with the Location Service and the Accelerometer.
01:22So the Location Service is a service in the SDK that lets your application
01:27figure out where it is on Earth, and the Accelerometer Sample shows you how to
01:32use what's known as an accelerometer.
01:35It's a little device inside your phone that figures out which way your phone is
01:40tilting and pointing, and that could be useful in all kinds of scenarios.
01:43There are a few other examples. One is the Hello XNA Framework, and we'll get
01:48into that a little bit.
01:49We are not really going to focus on XNA in this title because it's more focused
01:52on application development.
01:54XNA is a framework for building high performance games.
01:59The same XNA that you use to build Xbox 360 games and PC-based games, you can use
02:04to build games based for Windows Phone.
02:06There is also a sample to show you how to make sure your application works well
02:11on a global scale using the right locale for the country that it's in, and this
02:16affects how things like dates and times and currencies are displayed in the User
02:20Interface with commas and decimal points in the right place.
02:24And then finally there is another location service example.
02:27This one is little bit more advanced so we are only going to be going into the
02:30first few, and they have been doctored up a little bit in the exercise files, so
02:34that you can see how to use them.
02:35So now that you know how to get the tools and get the sample applications,
02:40it's time to start learning about what Windows Phone applications are and how
02:44they are built.
Collapse this transcript
Understanding Windows Phone 7 apps
00:00All right. Let's take a look at how Windows Phone applications are built.
00:04Windows Phone applications are created using one of two different
00:09frameworks that are available from Microsoft, and it really depends on what
00:13kind of application you are building, which will determine what kind of
00:16framework you want to use.
00:18All the applications that you create are written in managed code, and they
00:23are executed in an isolated sandbox, and this is so you have increased
00:26security in the phone.
00:27This prevents one application from reading another application's data, or running
00:32amok and trashing your data on your phone, and so on.
00:38The first framework we are going to look at is Silverlight.
00:40So you may have heard of Microsoft Silverlight.
00:42It is a managed code framework, originally intended for use on the Web.
00:47You may have seen Silverlight for things like delivering video or other kinds of
00:52rich Internet applications.
00:53Well, now that same Silverlight runs on Windows Phone.
00:58And this is an event-driven and XAML-based markup, and XAML is an XML-based
01:03language that Microsoft has come up with to describe Silverlight application UI.
01:07Each application is broken into a series of pages, kind of like Web sites are,
01:13and the Silverlight framework is really good for applications other than games.
01:18Now you can use it to write simple games. Tetris, for example, might be a good
01:21game to write in Silverlight, but it's mainly intended for applications.
01:26The other framework is called XNA.
01:28XNA is the same framework that you use to create games for Xbox and
01:34Windows PC-based games.
01:36XNA is focused on high performance gaming, and your application is composed of
01:42all the things that go into a game, like graphics, and media, and logic.
01:46Now, in this title we are not going to talk about XNA because XNA is an advanced
01:51subject that could fill an entire title unto itself.
01:54So we are going to focus mainly on Silverlight, and that's how we are going to
01:57go and build our application throughout the rest of this course.
01:59Silverlight-based Windows Phone applications are based on a model that
02:04includes a frame and a page.
02:06Your application has exactly one frame in it, and that frame hosts all the
02:11various pages that make up your application.
02:15The page itself contains a content area.
02:18Now the frame has the job in life of exposing properties from the hosted page,
02:23such as the fullscreen, which way the screen is oriented.
02:27There is a client area where the content of your pages are displayed, and it
02:32reserves space for what's called the System Tray and the Application Bar, and we
02:37will see those in a little bit.
02:39Pages are where the bulk of the work is done.
02:42So Pages contain a title.
02:43They can have their own Application Bar, so on and so forth.
02:47And you can see in the diagram on the right here.
02:49At the top, we have the frame.
02:50The frame contains the page, one or more pages.
02:52And each page has a content area where content can be displayed.
02:58Now the System Tray and the Application Bar are areas at the top and bottom of the frame.
03:02And you don't have access to those when you are drawing your content.
03:06Those are managed by the system.
03:08So the System Tray is located at the top of the frame, and that's where you see
03:11things like all kinds of various sundry system level status indicators that are
03:16common to most phones, things like signal strength, battery strength, what time
03:21of date is, that kind of thing.
03:23The Application Bar, that's this little guy down here, he is located at the
03:27bottom of the frame, and that provides a place for your application to provide
03:31some icons that promote the most common tasks that your application does.
03:36So for example, if you have a Calendar application, you might put a button there
03:38that says, Go to today or Create a new appointment, so on and so forth.
03:43The Application Bar also expands and contracts to show menu items for your
03:48application, and you can control those as well.
03:52All Windows 7 phones also contain a Back button, which you can see highlighted
03:56there, and this Back button is implemented in hardware; all Windows phones have it.
04:01And this is used to navigate back in application.
04:04This is usually handled for you by the framework.
04:08The framework realizes the user has clicked the Back button, and if there is a
04:12Back page to go to, then that page will be navigated to automatically.
04:16In some cases, however, it's your application's responsibility to handle the
04:20Back button; for example, a Web browser might look at the Back button presses to
04:26go back within Web pages, and not application pages.
04:29Okay, so now that we have seen how Windows Phone applications are organized and
04:34what they look like,
04:35we can take a look at what kinds of features we can put in them.
04:39So Silverlight on Windows Phone provides a pretty rich set of stuff that you can
04:44put into your applications.
04:46So let's take a look at what I am talking about.
04:48Here is a list of all of the existing Silverlight controls that you may be
04:54familiar with which are supported in Windows Phone.
04:58So every one of these guys in a blue box, if you have used it in your
05:01Silverlight work elsewhere, you can also use it on the phone.
05:04Now some of these are not big surprises, like button, or canvas, or check box
05:08or hyperlink button.
05:10These are all pretty standard controls that you probably used to seeing if
05:12you've ever worked with Silverlight before.
05:14And in fact, if you have ever done any Web development, a lot of these names
05:16will probably sound familiar to you.
05:18Now Windows Phone also provides a couple of custom controls and classes that you
05:24can use on Windows Phone to enhance the phone experience.
05:27So for example, they provide a ListViewItem and a ToggleSwitch.
05:31These are a couple of UI elements that are specific to the phone and really lend
05:36themselves to the handheld phone UI experience.
05:41And of course, there is a couple of controls that represent both the application
05:45frame and the application page, which I talked about just a few moments ago.
05:49These are the controls that represent how your application is contained
05:53within the phone itself.
05:55Okay, so that's probably enough talk.
05:57It's about time we started getting writing some code.
Collapse this transcript
2. Creating a Sample App with Visual Studio
Creating a simple app
00:00Okay, let's take what we've learned so far and get started writing our first
00:05Windows Phone application.
00:07So I'm going to go over here to the Start menu.
00:09I'm going to fire up my development environment.
00:12And once you've installed it, you'll see it's called Microsoft Visual Studio
00:152010 Express for Windows Phone.
00:19This is the IDE right here.
00:21So when the IDE first pops up, you'll get this nice, little Start Page, which
00:24introduces you to the development environment.
00:27Now if you've never used Visual Studio Express before it, let me just give you a
00:31quick overview of what it is that we're looking at.
00:33Over here on the right side this is the Solution Explorer.
00:36The Solution Explorer contains information about the project that you're
00:40currently working on.
00:41This is where the pages will be stored.
00:43This is where all the resources you include and your application will be, and
00:47it's where you set various properties for how the application actually runs.
00:50Over here on the left side, this is the Toolbox.
00:54Now there is nothing in the toolbox right now because we don't have a project
00:58opened, but you can think of this as the place where all of those controls I
01:02talked about are stored during the authoring process.
01:05So when you want to add elements to a page, this is where you will come to do that.
01:11And you can choose to keep this open by setting that little pin there, and
01:14that pins it to the open state, or you can unpin it, and then it will only
01:18open when you mouse over it.
01:19This little Start Page right here is the Welcome page for the application.
01:24And it allows to do a couple of things.
01:26Over here you can open an existing project or create a new one.
01:30And if you've been working on any recent projects, those would be listed down here.
01:34Over in the main content area, this is the Getting Started area.
01:38And it shows you some welcome information.
01:40There's a couple of quick courses on Creating Window Phone 7 Applications and
01:45how to submit one to the marketplace, as well as a news feed where you can go and
01:50read the latest MSDN information, or whatever RSS Feed you like to subscribe to
01:55about phone-related development.
01:58So to get started, we're going to click on the New Project link over there.
02:02And that will present us with the New Project dialog.
02:07So in the New Project dialog, we have a couple of choices.
02:10We have Silverlight for Windows Phone, or we have XNA Game Studio for Windows Phone.
02:16We're just going to go ahead and stick with Silverlight.
02:19And to make things simple, we're going to choose the Windows Phone Application.
02:24There's a couple of other options here. The Windows Phone List Application is
02:27designed specifically for phone applications that are list-based.
02:30We're not going to do that right now.
02:33We're just going to build a simple application.
02:35And there is also a Class Library, and these are basically reusable libraries
02:39that you can use in other applications.
02:41So if you find yourself developing the same code over and over again, you
02:45might want to just toss it in a class library and then include that in your other apps.
02:50So let's go back up to the Windows Phone Application, and you can see that it's
02:55already prefilled in some name and location information for us.
03:00Since this is just going to be a throwaway app to show you how the IDE works, we
03:04can just go ahead and leave those as they are.
03:06So I'm going to go ahead and click OK, and Visual Studio for Windows Phone will
03:12now create my application.
03:14And you can see the Solution Explorer is now being filled in with all the
03:17default contents for a new project, and up comes the Design Surface. Over here in
03:22the right we have the XML-based XAML language.
03:27This is the XML code that determines the structure of the UI of the app.
03:33Over here on the left side we have the Design Surface, and you can see the
03:38Design Surface has already been prefilled in with some stuff for us.
03:41And I'm going to go ahead and scale it down a little bit so we can see more of it.
03:45And let's move this over.
03:47And then here is our Toolbox, and you can see the Toolbox has now been filled in.
03:51So now we have all the Silverlight control goodness that we can use to build
03:55our application with.
03:57So let's go ahead and just start setting some application properties.
04:02What I'm going to do is click on the Control Surface.
04:06And you can see that as I select things on the Control Surface, they become
04:09selected, and they also become highlighted over here in the Code View.
04:12So there is a little Text Block named MY APPLICATION.
04:15That's this guy right here. Here is the page title.
04:17That's this guy right here.
04:19Let's go ahead and just change that to MY APP DEMO, and then we'll change the
04:26other guy from page title to My First Page.
04:31And you can see that the Design Surface is updating while I do that.
04:35That's basically all you need to do to create a new Windows Phone 7 application.
04:41So now that we've done, that let's take a look at what's involved in adding
04:44controls to an application page.
Collapse this transcript
Adding controls to the app
00:00So now that we have got our basic project opened in our Visual Studio IDE, let's
00:08start off by adding some basic controls to the page.
00:12And before we do that, let's just take a quick look through the source code to
00:16see what it is that we have got here.
00:18So right at the top, you can see that there is a PhoneApplicationPage Control,
00:23and I talked about that a little bit earlier.
00:25And the PhoneApplicationPage Control is what contains all the content of the
00:31page that you are laying out for the phone, and your application can contain as
00:35many of these pages as it needs.
00:37In this case, we are looking at the MainPage.xaml one right here.
00:42So as we scroll down through the content, you can see that in the XAML code
00:46for the layout, we have a series of controls that have been created for us by default.
00:52There is the main Layout Grid, which is what contains all of the content for the page.
00:56You can see that when I select it, it gets highlighted.
00:59And then within that grid, there are two other grids:
01:02There is one called the TitleGrid that contains the text blocks for our titles,
01:08and then there is the other one, which is the ContentGrid, and that contains
01:11all the page content.
01:13Now, I just want to quickly note, you don't really need to keep the TitleGrid if
01:17you don't want it, or the other ContentGrid. It's really up to you what goes in
01:21the LayoutRoot Grid.
01:23That's the one that contains everything.
01:25So if I really wanted to, I could just go ahead and just delete these guys, just
01:28cut them out, and you can see that they disappear from the layout, and I can put
01:31whatever I want in there.
01:33But for the purposes of this example, let's go ahead and leave them in.
01:38So while we are at it, let's change the names of the text blocks.
01:42We are going to call this Basic Controls Demo, and then we will also name the page,
01:48instead of My First Page, let's just call it Basic Controls.
01:55So you can see that after I have typed in the changes in the XAML, the layout
01:59surface over here, has updated.
02:01So go ahead and save that.
02:03Now what we are going to do is open up our Toolbox.
02:07So the Toolbox you can see is now populated with all the controls that are
02:11available to us as a Windows Phone Silverlight application.
02:14So I am going to go ahead and pin this Window Open, so I can keep it open and
02:19just drag things out of it, and I will just move this over a little bit to make
02:23it a little easier to read.
02:24So let's go ahead and start putting some controls on the page.
02:27What we are going to do in this example is put some controls on the page and
02:31then wire them up to Event Handlers and have the Event Handler show some UI
02:36feedback to show us that something interesting is going on.
02:40So the first thing I am going to do is pull out one of the most basic of all the
02:44control types, and that of course is a Button.
02:46Just click on that and drag him right out.
02:48You can see that when I drop the Button on the design surface, a couple of things happen.
02:52First, there are some layout adornments that have been added around the Button,
02:57and its been dropped with a default size and content.
03:01So as I click on this Button and move it around, you can see that as I am doing
03:05that, the layout surface is giving me some hints as to where to put it.
03:10So it's snapping my control to various snap points, which are predefined in the
03:16Windows Phone User Interface Guidelines.
03:19So I am going to go ahead and drop that Button there. Okay.
03:22Let's go ahead and drag some other ones out while we are at it.
03:24Let's get ourselves a CheckBox.
03:28CheckBox, and we will make sure these guys lineup.
03:31What else should we use?
03:32How about a HyperlinkButton? Drag the HyperlinkButton out. Line that up. Get
03:38ourselves a ListBox.
03:41Now, ListBoxes, when they are added, they don't, by default, contain any content.
03:45So we are going to have to put some content in there.
03:47And I am just going to drop it here on the design surface, size it so it
03:51snaps to the right size.
03:53So this is an opportunity for me to now go and use the Properties Window to add some content.
03:58So make sure that ListBox is selected, which it is.
04:02So over here in the Properties Window, I am going to click on the Properties,
04:07and I am going to scroll down a little bit.
04:09You can see that there is an item here called Items, and this is where the
04:13ListBox items get entered, and it's a Collection.
04:15So I am going to click on that little Ellipsis button, and I am going to go
04:18ahead and add some ListBoxItems.
04:21So add, let's just say, four, and we will give each one a name:
04:26ListBox Item 1, and this guy will be ListBox Item 2, and this one, ListBox Item
04:363, and this one we will call ListBox Item 4.
04:43So now I have added some ListBoxItems, let's continue on.
04:47How about we drag out some RadioButtons?
04:49Here is a RadioButton right here.
04:51In fact, let's drag out two RadioButtons, because they are kind of useless
04:55unless there is more than one.
04:57So for RadioButtons, they need to be in the same grouping in order to have the
05:01correct toggling behavior work.
05:03So what I am going to do is select this RadioButton, and then in the Properties
05:07Window, I am going to scroll down to the IsChecked and make it checked.
05:11And then over here in the GroupName field, I am going to give it a group.
05:15I will just call it grp1, and I will do the same thing with the other RadioButton.
05:20This is very similar to how Web pages work, too.
05:23So now that they are both in the same group, they will know to toggle each other on and off.
05:27All right. What else should we use?
05:27How about a slider?
05:28We have a slider control out here.
05:32And let's see, what else should we use?
05:34How about a ToggleControlSwitch right there?
05:39And then finally, we need to have a piece of UI that's going to show us
05:41that things are actually happening when we operate these controls, so how
05:44about a ProgressBar?
05:46So we will bring the ProgressBar out, and we will line him up right over here. All right.
05:50Looks good. Okay.
05:52So now that I have got all the controls on the design surface, what I need to do
05:56now is create the Event Handlers.
05:59So I will do that by selecting the Button or the control I want to create
06:04the Event Handler for.
06:06And then back over here in the Properties Window, in addition to the Properties
06:09tab, there is also an Events tab.
06:11So when I click on the Events tab, you will see that all the events that are
06:15relevant to that control are listed here, and the one highlighted in blue is
06:20the event that is most likely the one that you are interested in for that
06:25particular control.
06:26Visual Studio is guessing that Buttons get clicked a lot, and therefore it's
06:30going to highlight the Click Event.
06:32And in this case it's right, so I am going to go ahead and type onClick. Okay.
06:39So now that we have our controls laid out on the design surface, we are going to
06:45wire up some events to them.
06:47So what I am going to do is select the item that I want to add an event for, in
06:52this case the Button, and then over in the Properties Window, you see that
06:56there is a list of all the Properties that are relevant to the control, but
07:00there is also an Events tab.
07:01And when I click the Events tab, you will see that there is a list of relevant
07:07events for that control.
07:10The one that's highlighted is the one that Visual Studio has guessed is the most
07:16relevant for the control type you have selected, in this case the Button.
07:20Visual Studio assumes that Buttons tend to get clicked and so therefore it
07:23highlights the Click Event.
07:25So I am going to go ahead and type in onClick here.
07:28And when I do that, it's going to add the onClick Event to the Button, and it's
07:32going to switch me over to the Code View for this page.
07:37So here in the Solution Explorer, you can see that there is the XAML page, and
07:43then there is the associated C# code page that goes with this layout.
07:47So it's added an onClick Event for me.
07:51Now, for the onClick Event, we want to update the UI, so that we can see that
07:54things are happening.
07:55And what we are going to do is just change the value of that ProgressBar to
07:59slowly increment over time.
08:01So we are going to do that by writing our own method called private void, and we
08:05will call it UpdateProgress.
08:10And UpdateProgress will do a couple of things.
08:12So first of all, ProgressBars have a number which represents the percentage of
08:16their current progress, and that is stored as a double.
08:19So we will name our variable double progValue, and we will set that to the
08:25value of ProgressBar.
08:27You can see that as I am typing, Visual Studio's IntelliSense is helping me
08:31choose which control I am talking about, so there is progressBar1 right there.
08:36So I will select that, progressBar1.value.
08:41And what we are going to do is increment the value of progValue by 10%.
08:47Now, we don't want this to overflow and just keep going on forever, so what we
08:51are going to do is check to see if progValue is greater than 100.
08:56Then we are going to set prgoValue back to 0.
09:02The ProgressBar will basically just keep going over and over and over again.
09:06Then finally, we are going to set the progressBar1's Value property equal to the updated value.
09:15Okay. So now that we have written our ProgressBar updating function, we can go back to
09:20the onClick handler and just simply call UpdateProgress, and we will save. Okay.
09:27So now we have wired up the Button to this onClick Event Handler, which will
09:32call the UpdateProgress function, which will take whatever the value of the
09:36ProgressBar is, add 10 to it, see if it's over 100, and if it is, reset it, and
09:41then put that value back into the ProgressBar.
09:42So let's go back to the layout.
09:45Now what we need to do is wire up the events for the rest of the controls.
09:50And just to make things easy, I am going to have each control responds to the onClick Event.
09:56So for the CheckBox, when I select it, we will just go over to the Properties
09:59and choose onClick, and I don't have to write it again.
10:03It's already written for me.
10:05Let's just take a quick look at the XAML code to see what's going on.
10:08You can see that when I set that onClick handler what happened was a Click Event
10:14got inserted with the name of my function.
10:17Let's go to the HyperlinkButton and do the same thing.
10:20For Click, we will set that to onClick, and for the RadioButtons, we will do the
10:25same thing. set that one to onClick, as well as this one.
10:31For the ToggleControl, we will do that same thing.
10:35So the ListBox and the slider are different cases.
10:38What we will do for the ListBox is we will select the ListBox control, which I
10:43will just click it in the XAML right there.
10:45ListBox control will just fire the event whenever the selection changes.
10:49I will select the SelectionChanged Event here, and just type in onSelChange, and
10:57once again, that puts me in the Code View.
11:00And just to make things simple, I will just call UpdateProgress again, so I will
11:04Copy that and Paste it.
11:07Then back to the slider, the slider control, same idea, slider controls over
11:12here in the Properties, you can see they have a ValueChanged Event, and I will
11:16just call this one onValChange.
11:20And same idea, I will just call UpdateProgress.
11:25So quick recap: We have now wired up all of our controls to the various Event
11:31Handlers for them, and the Event Handlers basically just update the value of our ProgressBar.
11:36So if we go back to the layout, we will see all of our controls are wired up,
11:40and all we need to do now is test it.
11:43And that's the subject of our next lesson.
Collapse this transcript
Running and debugging the app
00:01Now that we have gotten the UI and the EventHandlers all wired up, let's go
00:06ahead and test our application.
00:08Now there is a couple of ways you can test the application.
00:10You can deploy the application to the Windows Phone Emulator that comes built-in
00:16with the Developer tools when you install them, or you can deploy to an actual
00:20Windows Phone Device.
00:22Now I don't have a Windows Phone Device right here; what I have is the Emulator.
00:26The way you choose though is up here in the toolbar, there is this little
00:29dropdown that says Windows Phone 7 Emulator or Windows Phone 7 Device.
00:34And since I don't have a device, I will just leave it on the emulator.
00:38And to start the debugging process, I can do that one of a number of ways.
00:42I can either click this little green Play button right here, which means start
00:47debugging, or under the Debug menu, I can just choose Start Debugging.
00:52So I am going to make sure the Emulator is chosen, and I want to click the green arrow.
00:55Let's bring the Emulator back up so you can see it.
00:58This is the Windows Phone 7 Emulator, and when you install the Developer tools
01:02it gets installed along with the Visual Studio IDE, and it really is a full
01:09Windows Phone emulation.
01:11As far as the code in your application is concerned, it's running on a real phone.
01:15This is a virtual machine that contains the full Windows Phone operating system
01:19and Silverlight and all that good stuff.
01:22So as far as your code is concerned, this is running on a phone.
01:25Let's take a quick look at some of the Emulator controls.
01:29So down here are the Back, Start, and Search buttons that come with every
01:33Windows Phone, and we will see how to deal with those a little bit later.
01:37Over here in this Toolbar, there is the Close, which closes the Emulator, and
01:41there is Minimize, and those are pretty standard Windows controls.
01:44There is also these two controls which control how the Emulator is rotated.
01:49So to simulate Landscape mode, you can just click on one of those buttons, and
01:53it will put it in Landscape mode.
01:56This button right here zooms the Emulator, but I am going to leave it at its
01:59current size so you can see it, and then this one here controls the settings.
02:04So to test the application, we can go ahead and just start playing with the U.
02:09And if everything is wired up correctly, then every time we click or
02:13otherwise interact with the controls, the value of this progress bar down
02:17here should update.
02:19So let's go ahead and click on the button, and you can see that yeah, the
02:22progress bar in fact, did update.
02:25Let's try the others. There are the CheckBox, HyperlinkButton, RadioButtons are responding and the ToggleSwitch.
02:33Let's try making a selection of ListBox.
02:36You can see that that worked.
02:37I will just scroll up a little bit.
02:39So you can see that's working.
02:41If I change the value on the slider, you can see that that's working, too.
02:45And you can also see that the ProgressBar is correctly looping around, starting
02:50at zero each time it overflows.
02:54So we know our code is working, and that's great, but if our code wasn't working,
02:58we would need to debug it.
03:00So let's see how that works.
03:01We are going to switch back over to the IDE here.
03:05So here in the IDE, you can see that while we are debugging, these little lock
03:08icons have shown up in the title tabs, and what that basically means is I can't
03:13change the code because the code is currently running, and if I wanted to stop
03:17the code from running, I would just have to choose Stop Debugging up here in the menu.
03:21I am not going to do that, however, because we should just take a look at
03:25how debugging is done.
03:26Let's switch over to the Source Code, and scroll on down to our code.
03:32So to debug, all I need to do is put a breakpoint at the part of the code that I
03:38am interested in debugging.
03:40So, for example, if I wanted to stop right here before UpdateProgress is
03:44called, I will just put a breakpoint on that line. And similarly I can put a
03:48breakpoint in here as well in my UpdateProgress routine. And now I can go back
03:53to the Emulator and interact with the UI, and these breakpoints should trigger
03:57when I do the right thing. Okay.
04:00So let's try clicking on the button.
04:03So you can see that when I click on the button, what happened was my breakpoint
04:06got triggered, and it's now holding right here on the UpdateProgress call.
04:11So what I am going to do now is step through the code to make sure everything is
04:15working correctly, but before I do that, let's take a quick look at the debugging UI.
04:19Over here in the Call Stack, this shows all of the function calls that were made
04:26to get to the point where I am right now.
04:28So I clicked on a button, see right there the button onClick, and then some of
04:31the functions, and then some of the function got called, and then my EventHandler
04:34got triggered right here.
04:36Over here in the Locals window, this displays the value of all of the local
04:41variables that are currently within the scope of the function I am looking at.
04:45And I can also switch over to the Watch window.
04:48The Watch window just lets me type in any expression that I am interested
04:51in keeping an eye on.
04:52I don't have any right now, but that's what it does.
04:54So let's switch back to the Locals, and let's step into the onClick function.
04:59So for example, when I Step Over UpdateProgress, well there is a breakpoint in
05:05there, so it's not going to let me step over it.
05:07So now we step in here, and I can now investigate the code to make sure
05:11everything is working fine.
05:13So here we have our progValue, and you can see it's currently set to zero and
05:17when I Step Over it, so I can either Step Over using F10, or I can Step In to
05:22functions or Step Out of them, but instead, I am just going to use Step Over and
05:25that steps over that statement, and you can see now that progValue has now been
05:29set to what the ProgressBar is, and it will add 10 to it.
05:34So now you can see it's 30, and when a variable change in the Locals window, its
05:39color gets highlighted to red so you can see what changed.
05:42Now we are going to test to see if it's more than 100, and it's not, which is correct.
05:47So we are going to go ahead and set the progress bar equal to the progValue and
05:51to keep on running, I can just go back up here and hit Continue.
05:55I will just click this little Play button, and the code keeps on running.
05:59So that's pretty much how you use the Emulator and Debugger in Windows Phone 7.
Collapse this transcript
3. Creating a Sample App with Expression Blend
Creating a simple app
00:01In this section, we are going to see how to build a Windows Phone 7 application
00:05using Microsoft Expression Blend.
00:09So I am going to go to my Start menu and launch Blend.
00:12Now Expression Blend is part of the Microsoft Expression family of products and
00:19Expression Blend is essentially focused on the designer more than the
00:23developer. Whereas Visual Studio tends to be used more by developers and people writing code,
00:30Expression tends to focus more on the designer aspect of things.
00:35So before we get started building a project - let me just go ahead and close this -
00:39let me give you a quick overview of Microsoft Expression Blend.
00:44It looks a little bit like Visual Studio in the way that it's laid out.
00:47There is a Properties panel over here.
00:50In fact, there is a collection of panels not just Properties.
00:52There is Resources and Data.
00:54Over here there is a Project panel, and there is a panel for Assets.
00:58But there are also panels for things that you would expect to see in more of a
01:02designer-focused program such as an Object and Timeline View, a panel for
01:07managing States and Triggers and so on.
01:09These are all things that make up interactive design, which is what Blend focuses on.
01:14Okay, so let's take a quick look at how we would go about creating a new
01:20application in Blend, and once we have done that, we will see how to add
01:24some basic controls.
01:26So I am going to go up here into the File menu, and I am going to choose New Project.
01:31And if you have installed the Blend SDK for Windows Phone in addition to the
01:38Silverlight and WPF options that Blend comes with, you will see an option for
01:43building a Windows Phone application.
01:47And Windows Phone will give you two options.
01:49There is one for just a plain app, and then there is one for what's known as a
01:53data-driven application using something called the model-View-Viewmodel pattern.
01:59We are not going to get deep into that in this course.
02:02We are just going to stick with the basic application.
02:04So once I click OK, Blend will set up the essential parts of the project for me.
02:12So you can see here that the Design View now contains the same layout as we saw
02:20earlier in the course in Visual Studio.
02:23So here is the Layout piece with the text blocks for the application name and the page name.
02:29Let me zoom this down a little bit so you can see everything.
02:33So just like in Visual Studio, Blend gives me a Layout View and a Source Code View.
02:39So here I am looking at the file in the Design View.
02:43But I can also go up here to the View menu, and choose under the Active Document
02:48View to see things in either a Split View, which shows me the layout as well as
02:52the XAML code, or I can just go ahead and choose to see the XAML View by itself.
02:58So here you can see the XAML code, and this is the same XAML code that we have seen before.
03:03So let's go back to the Design View.
03:06So when I am in Design View, and I make selections, you will see that the
03:10Properties grid updates with the properties for the selected element, and over
03:15here now that I have created a project, you can see that the Assets panel has
03:20been populated with the various types of controls I can use in my project.
03:25So for example, if I want to just use a button, I will just drag a button out,
03:29and the properties change appropriately.
03:32So that's a quick overview of Expression Blend.
03:36What we are going to do now is use Blend to build a quick Windows Phone
03:41application that shows how to lay out controls and wire up some event handlers.
Collapse this transcript
Adding basic controls
00:00So let's use Expression Blend to build a sample Windows Phone 7 Application.
00:07So what I'm going to do is, up in my File menu, I'm going to open a project and
00:12if you have access to the Exercise Files, you'll see in the folder named
00:1803_blend, there is two folders:
00:21one named BasicControls_Start and one named BasicControls_Final.
00:26So I'm going to open up the Start version and build the solution from scratch.
00:31And if you'd like, you can follow along with me, or you can just open the Final
00:35version and see how everything works yourself. Okay.
00:37So I'm going to open up the solution file here and click Open.
00:42And Blend will open the project for me, and let me scale this down so that it's visible.
00:48So here is the layout page for the Basic Controls Demo.
00:54So what we're going to do is add a whole bunch of basic controls to the page and
01:00then wire up some event handlers and have the event handlers do something to the
01:04UI so that we can that they're working.
01:06What I'm going to do is, over in my Assets panel, I'm going to select the Button,
01:11and I'm going to draw a button on the surface. It's about right.
01:15And then I am going to position the button, and you can see as I'm dragging the
01:19button around, there's some, nice, little snap lines appearing that help me
01:23position the button properly.
01:24So I'm going to go ahead and just position the button there, and I'm going to get Checkbox.
01:29Do the same thing with the check box, move that into place, make sure they're
01:36lined up, and let's see what else.
01:38I can use a HyperlinkButton, but let's see, there is no HyperlinkButton in the
01:42Basic Controls list.
01:44I know it's in here somewhere.
01:45So what I'll do is I'll just type in the Search box "hyper," and you can see that
01:49right there there's a HyperlinkButton.
01:52So I'll just drag that one out as well, and I'll position the HyperlinkButton to
01:57be lined up with the Checkbox. Okay what else?
02:00How about a Listbox?
02:02So I'll clear the Search, go back to the - I will just get myself a Listbox, drag
02:08and drop the Listbox and resize it and for Listbox I'm going to need to add some
02:14items to the Listbox.
02:15So I'll scroll down to where the Common Properties are, and you can see here in
02:20the Common Properties, there's an Items property.
02:23So I'll click that little ellipsis, and I'll add some items to the Listbox, and
02:27I'll add few Listbox items.
02:29There's one, two, and three, and I'll give them an appearance.
02:35So this one I'll name Listbox Item 1, and this one will be Listbox Item 2, and
02:45then this one will be...
02:48So I've got my Listbox Items in there now.
02:51What else can I use?
02:51How about some Radio buttons?
02:53So I'll scroll down here, and we'll put some RadioButtons on the canvas.
02:57We'll put two RadioButtons out here, and we'll position the RadioButtons.
03:03Now RadioButtons, in order to work properly, have to be grouped, and the way you
03:08group them is you give them the same group name.
03:10So I'm going to select this RadioButton and start that one off as Ischecked, and
03:14I'll give it the GroupName grp1, and I'll name this RadioButton grp1 as well.
03:20This is pretty much the same thing you do in Web pages.
03:23When you name Radio buttons the same name that way, they all know to toggle each
03:26other on and off right way. So let's see.
03:28Radio Button, what else can we use?
03:30How about a slider control?
03:33Bring the slider control out here and make that the right size as well. Line
03:40that up with the edges. There we go.
03:43And how about a ProgressBar?
03:45ProgressBar down here, and we'll resize the ProgressBar and make it the right size.
03:52There we go.
03:54And we'll give it a name.
03:56We'll call it progressBar1 and how about the ToggleControlSwitch, too?
04:01Well that's also not in the base control category.
04:05So I'll just search for it, and there it is right there the
04:09ToggleControlSwitch and that.
04:15So now we have a whole bunch of Basic controls laid out here on the design surface.
04:21What we need to do now is wire up the event handlers for each.
04:26And so what we'll do we'll create event handlers.
04:29When the event handler fires, we'll update the ProgressBar to show some progress
04:34so that that way we can see that our event handlers are working.
04:37So to do that, I'm going to select each control, starting with the Button, and
04:42you can see that when I select the control, the Properties panel updates with
04:46all of the properties for that item.
04:48There's also, however, an Events view.
04:51So I'll switch over to the Events view, and I'll assign event handlers to each
04:56control just by giving each one a name.
04:58So for the Button, I'll handle the onClick event by typing "onClick" and when I
05:03hit Return, you'll see that Blend now switches me over to the Code view.
05:08So now I can write my Click handler.
05:12So I'll just write a function named UpdateProgress. Okay.
05:16So UpdateProgress is going to increment the value of the ProgressBar, and I need
05:21to write that function because there is no such thing by default.
05:25So I'll write private void UpdateProgress and UpdateProgress is going to do two things.
05:34First, it's going to retrieve the current value of the ProgressBar and the
05:39ProgressBar has a value that's a double format.
05:43So I'll name it var my variable double progValue = this.
05:49You can see that as I type, Blend is helping me with some intellisense to locate
05:54the control they need to access and there is right there progressBar1.
05:57I type dot and then you that highlights Value for me. Okay.
06:03So we get the Value.
06:05Now we need to increment the Value.
06:07So let's say that progValue gets incremented by 10%.
06:14Now if we don't do anything else, then the ProgressBar will fill up and keep on
06:18going, so we need to handle the case where if progValue is, let's say, greater
06:23than 100, then we set progValue back to zero.
06:29That way the ProgressBar will just keep looping over and over again.
06:33And then once we've done that, we set the Value of the ProgressBar equal to the progValue.
06:45Okay. So now we've written out UpdateProgress function, and we've wired it up to the
06:49onClick handler for the button.
06:51So let's go back and do the same thing with the rest of the controls.
06:54So I'll click the Checkbox, and the Checkbox had and event named onClick also,
06:59so I'll write onClick.
07:01And you can see that I don't have write it again because it's already been
07:04written once so I could leave it as it is, and we'll do the same thing with the Hyperlink.
07:09Just write onClick, and what else handles onClick?
07:17RadioButtons do onClick.
07:19So we'll do onClick here, and we'll do onClick for this one, and we'll do
07:28onClick for the ToggleControlSwitch. Okay.
07:34So that now we've wired up the click events for the controls that get clicked,
07:39what I'm going to do is select the Listbox and for the Listbox, we're not going to do click.
07:43We'll do something else.
07:45Let's say that when the user changes the Listbox selection, we want to handle the event.
07:50So we'll write an event called onSelChange, and I think you can guess what
07:55this ones going to do.
07:56It's basically just going to call UpdateProgress.
07:59Okay, and then back to the Code, back to the Layout and the slider.
08:03So now the slider control will do , whenever the user moves the slider control,
08:09that's the onValueChanged event.
08:11So we'll call this one onValChange and same thing call UpdateProgress.
08:17So now we've wired up all of our controls to an event handler that will change
08:22the value of this ProgressBar when any one of these guys gets interacted with.
08:26So to test this out, we need to run the project, and we'll see how to do
08:31that next.
Collapse this transcript
Running the app in the Emulator
00:01Once you've created your application using Blend and you've gotten everything
00:04laid out and all your events wired up, the next logical thing to do is to have
00:09to try the application out.
00:10And we can do that with the toolset that we downloaded by running the project in
00:16the Windows Phone 7 Emulator.
00:19And the way that we do that is up here on the Project menu there is an
00:24option called Run Project.
00:26And you can see that when I choose to run the project I get a dialog giving me
00:31an option of what to do.
00:32I can either run the project in the Emulator or on an actual Windows Phone 7
00:39Device that I have attached to my computer.
00:41Now I don't have a Windows Phone 7 with me, so I'll just run it in the Emulator.
00:46And I'll leave that selection highlighted and click OK.
00:50And you can see that the Emulator is going to launch.
00:54So let me switch over to the Emulator.
00:56So this is the Windows Phone 7 Emulator, and for all intents and purposes it
01:02is a Windows Phone.
01:05It's a virtual machine running the actual operating system with Silverlight and
01:09all that stuff in it.
01:10So as far as your code is concerned, it's running on an actual phone.
01:14And the controls along the side here in this little bar right here, the top two
01:19are Close and Minimize.
01:20Then the next two are controls for orientation changes.
01:24You can change the orientation of the phone, putting it into Landscape mode or
01:29Portrait mode by clicking those two buttons.
01:31This button over here means to zoom, and this button down here means to Minimize.
01:36So we are going to leave those alone, so you can see everything.
01:40So now that the application is actually running in the Emulator, we can try it
01:44out to make sure it's working.
01:46So remember, we wired up at event handlers to all these controls so that when the
01:50user interacts with them this Progress Bar should update.
01:54So I am going to try that out.
01:55I am going to click the button.
01:56And you can see that when I click the button, the Progress Bar did, in fact,
02:01update right down here by 10 points.
02:03So let's try some of the other controls.
02:06CheckBox, yeah seems to be working and the HyperlinkButton, yeah, and the
02:10ListBox, yeah, that seems to be working, too.
02:13RadioButton seem to be working. Toggle Switch seems to be working. Slider
02:18seems to be working.
02:19And you can see that the Progress Bar is looping around each time because of
02:24that logic we put in to check to see if its value was exceeding 100.
02:28And it seems to be working great. All right.
02:32That's pretty much all there is to creating, laying out and wiring up an
02:38application using Expression Blend 4.
Collapse this transcript
4. Exploring Advanced Features
Handling page orientation changes
00:00Windows Phone Applications are capable of handling different page
00:04orientations in their layout.
00:07This is going to happen because Windows phone users can rotate their phones to
00:12display information in landscape mode or portrait mode.
00:18Now, your application can respond to these changes, and you can also specify
00:23what type of orientation your application can handle.
00:25In fact, you can do this on an application page-by-page basis.
00:31Each page can tell the host application whether it can do portrait mode, or
00:37landscape, or both modes.
00:40If an application tries to navigate to a page that only supports one type of orientation,
00:45what will happen is the phone will adjust to show the page in that orientation,
00:50regardless of how the user is holding a device.
00:53So, for example if your application contains a page that can only be displayed
00:58in portrait mode, then when the used rotate the device, nothing will happen.
01:03Or similarly, if they are viewing application, and they are holding it in portrait
01:07mode, and they are navigate to a page that only knows how to do Landscape, then
01:11the phone will switch into Landscape mode while that page is being displayed.
01:15So, let's write some code and see if we can figure out how to do this.
01:19So, I am going to go ahead and open up my Orientation example.
01:24So, I am going to choose Open Project.
01:26Now, if you have the ExerciseFiles, you'll notice that there is a folder called
01:3004_advanced, and inside there there is a folder called Orientation.
01:34Now, I am going to be operating on the Orientation_Start version, and you can
01:37either do that as well and follow along with me, or you can just open the Final
01:41version and see how everything has done.
01:44So, I'll open the Orientation solution.
01:47So, here is my project, and you can see that I've already laid a few things out.
01:50So, on my homepage, there are three buttons:
01:53one for going to a Portrait, a Landscape and a Neutral page.
01:58So, here is the button for going to the Portrait Page.
02:02If we look at these XAML code, you'll see that this is the button right here.
02:05I'll scroll over, and you can see I've already added an EventHandler for going to a portrait page.
02:14So, we can write the EventHandler for that.
02:18If we go to the code for this file, we'll see here is the EventHandler right here.
02:23So, what we need to do is write the EventHandler that's going to navigate to the Portrait Page.
02:28So, let's add a Portrait Page to our project.
02:32I am going to right-click on Orientation and click Add > New Item.
02:38Now, I have to choose what kind of page I am going to add.
02:41The funny thing is it doesn't actually matter what kind of page I choose,
02:44because I can actually change it later on.
02:46But for the moment, I'll choose Windows Phone Portrait Page.
02:50So, I've added a new page to my project, and I am going to rename this.
02:55I'll Rename it Portrait Page. Get rid of the 1.
03:02In the XAML code for this page, you can see that there is a property right here
03:05at the top called Supported Orientations, and that is set to be Portrait.
03:10You can also set this property in code, as well.
03:13So, if I go to the code and here in the constructor if I were to type
03:17this.SupportedOrientations = SupportedOrientation., and then I can choose
03:25Landscape, Portrait or PortraitOrLandscape.
03:30If I set this in code, it will override what's specified in the XAML code.
03:36But it's up to you.
03:37You can do it in XAML. You can do in code.
03:39It doesn't really matter how you do it.
03:41So, I'll just do it in both ways here.
03:44So, I'll set it to Portrait, so back in the Design View, okay.
03:51So, now we've created a page that can only be displayed in Portrait mode.
03:56So, let's set some properties here.
03:58We'll change the name of it to be ORIENTATION DEMO, and we'll call this the Portrait Page.
04:13Now, we've created a page that can only be displayed in Portrait mode.
04:16Let's go back to the MainPage and for the EventHandler, we need to write the
04:22EventHandler that actually navigates to this page.
04:25So, to do that we are going to use what's called the NavigationService.
04:29The NavigationService is how the phone application navigates among the page, and
04:34this is a global object.
04:36So, we are going to write the code to navigate to the Portrait Page.
04:40So, I will write NavigationService. NavigationService is a global object.
04:48You'll see that when I type the period, I'd get a whole bunch of methods, and
04:52the one we want is Navigate.
04:54That's that guy right there.
04:56So, we are going to navigate to a page, and the way that you navigate to pages
05:02in Windows Phone 7 and Silverlight is the same way pretty much using the same
05:08syntax that you do with Web pages.
05:10So, we are going to navigate to a Uri, so we'll type "new Uri."
05:19The Uri that we are navigating to is going to be the Portrait Page.
05:25So, write it as a string.
05:27We'll write PortraitPage.XAML, and this is a Urikind of Relative
05:41because it's inside of our project here.
05:44Okay, so once I have done that,
05:46now when I click that button, we will navigate to the PortraitPage.
05:53So, let's go ahead and try that.
05:54We'll save, and then we well run this in the Emulator, and we are deploying.
06:01Welcome to application.
06:03We are in Debug mode now.
06:05Let's switch over to the Emulator.
06:08So, when I click on Go To Portrait Page,
06:10you can see that we go to the Portrait Page.
06:12So, I'll click Back.
06:13What I'll do now is put the phone into Landscape mode, and I'll click on Go To Portrait Page.
06:18You can see that the page is staying in Portrait mode, even though the phone is in Landscape.
06:23Let's go back to Portrait mode and go back. Now let's write a Landscape page.
06:32So we'll go back to the code, and we'll stop debugging.
06:36So, now we need to add a page that only does Landscape.
06:41So, same story as before.
06:44We'll Add > New Item, and this time we'll add a Landscape page, and we'll Rename
06:49this LandscapePage.XAML.
07:00We'll change this to say ORIENTATION DEMO, and we'll call this Landscape Page.
07:14What we are going to do is just leave this as Landscape mode right here in the XAML.
07:20Let me go back to the MainPage, and now we need to hook up the EventHandler
07:26to the Landscape Page.
07:29So, for this button, we'll make a new event called onLandscapePage, and similar to this one
07:39I'll just copy that and paste it, and this time we are going to
07:45LandscapePage.XAML, okay.
07:54So, now let's test that one out.
07:57Go back to the MainPage, and we'll launch the application.
08:04Now, I'll click Go To Landscape Page, and you can see that we went to
08:07the Landscape Page.
08:08It's only a Landscape mode, even though the phone is in Portrait.
08:13The last thing to do is to make a page that can handle both layouts, and that's
08:17called a Neutral Page.
08:18So, let's make one of those.
08:20Let's stop debugging.
08:24Now what we are going to do is Add > New Item, and we'll add a, we'll call it a
08:31Portrait Page, but that's not what it is.
08:34We'll just Rename it to NeutralPage.
08:45In the code for the NeutralPage, we will say that this.SupportedOrientations =
08:55SupportedPageOrientation.PortraitOrLandscape.
09:00So, we'll save that and go back to the layout, and we'll name this Neutral Page.
09:13Let's create a page element that responds to the layout change in a way that
09:20relays out the element to look good in both Portrait and Landscape.
09:27So, I'll take a TextBlock, and I'll bring it on the design surface here, and
09:33I'll make it this big or so.
09:36What I am going to do is change some of the properties so we can see it better.
09:40So, let's make the Color White, okay.
09:48We'll make the FontSize nice readable like 24.
09:53Then we'll make the Text content of something nice and long.
10:00See, how about we make it, let's see, "This is a textblock."?
10:09It should reposition itself to lay out in both Landscape and Portrait modes, and
10:24we'll wrap the text. All right.
10:29So what I am going to do is I am going to lay this out right here, and I am put
10:34these little arrows on all three sides.
10:37What that basically means is this arrow anchors this edge of the control to this
10:44side of the screen right here.
10:45Same thing over here:
10:46This arrow anchors this edge, and this arrow anchors this edge.
10:50So, when this layout changes, this TextBlock will be stretched in all the ways that
10:55it's anchored so that it relays out.
10:58So, let's go back to the MainPage, and now I need to add -
11:03I am going to change this guy to be anchored as well -
11:06So, now we need to add an EventHandler for going to the NeutralPage.
11:13So, I'll click, and we'll call it onNeutralPage and this time, I'll copy
11:22and paste this guy.
11:23Copy and Paste, and this is the NeutralPage, so we save.
11:31We go back to the MainPage.
11:36Now, all I need to do is run it and test it.
11:42Switch to the Emulator, so now we have our Neutral Page.
11:45This is the Neutral Page.
11:45You can see that text block is laid out.
11:47Now, when I rotate it, do you see how the text got stretched out?
11:50So, now both ends are anchored over here.
11:53So, once again, we're in Landscape mode. The text have been relayed out to look
11:57good in Landscape mode.
11:59Go back to Portrait mode.
12:02You can see that it's been squished in; now it's on three lines.
12:06So, there is Landscape on two lines, Portrait on three lines.
12:10The last thing that I want to show you is that you can actually respond to the
12:16orientation change event.
12:19So let's go back to the code, and we'll Stop Debugging.
12:25So, on the MainPage here in the XAML code, I am going to select the Page Control.
12:33Down here in the Events list, way down here at the bottom, you'll see that
12:37there is two events:
12:38There is OrientationChanged and OrientationChanging.
12:44So, OrientationChanged gets called when the orientation has already changed.
12:49OrientationChanging gets called when the orientation is about to change,
12:54but hasn't done so yet.
12:57So, let's write an EventHandler for OrientationChanged, and we'll call this
13:03onOrientationChanged.
13:09I'll just do something really simple, like show a MessageBox, and this is like an
13:13alert in the Web page.
13:16So, MessageBox.Show, and the title will be, You changed the orientation!
13:27So, we'll save that and go back to the MainPage, and now we'll run it.
13:32Let's go to the Emulator. So now when I change the orientation,
13:41you can see that I am getting a MessageBox that says, hey!
13:44You changed the orientation.
13:45When I change back, there it is again.
13:49That's all there is to it.
13:49That's how you handle orientation changes in Windows Phone 7.
Collapse this transcript
Adding an Application bar
00:00In this example we're going to learn how to add an Application Bar to our
00:05Windows Phone application.
00:07The Application Bar, and you can see it highlighted here in this example down at
00:11the bottom of the phone screen there, is a set of icons and menu items that
00:15appear at the bottom of the phone screen. And it's optional; you don't have to
00:19have this in your application.
00:21But it provides a way for users to get quick access to some of the features and
00:25functionality of your application.
00:28There are four steps to adding an Application Bar.
00:34The first: You have to add a reference to the Microsoft.Phone.Shell assembly.
00:40And you can think of an assembly as just a collection of code that contains some
00:46useful features that is reusable, kind of like a library, and so you need to add
00:51a reference to that to your project.
00:52Once you have done that, you can add the button images to the project.
00:57And these are images that are standard PNG files like you're probably used to using.
01:04And you add them in, and those become the icons on the Application Bar.
01:07Once you've done that, you can create the Application Bar, and there are two
01:11ways that you can go about doing this.
01:12You can either doing it in XAML markup code, or you can do it
01:17programmatically in C# code.
01:19It doesn't really matter which way you do it.
01:20They both work just fine, but it's up to you.
01:22And then once you have done that, you can add Button and menu items to the
01:27Application Bar and implement the event handlers for each one of the items.
01:31Now you should note there are only four buttons on the App Bar maximum.
01:38Even if you turn the phone to Landscape mode, they only provide support for four buttons.
01:42The reason for that is because the purpose of the Application Bar is to provide
01:47quick access to the most common features of your application.
01:51So rather than overloading the bar with lots of different icons and menu items,
01:56they limit you to four buttons.
01:59Let's go ahead and see how to add an Application Bar to our project.
02:03Let's switch over to the code.
02:05And okay, here I am in Visual Studio.
02:07So I'm going to open up my project, and in the example folder for the AppBar,
02:13you'll notice that there are two folders:
02:14There is one for Start and one for Final.
02:16I'll be working on the Start version, and you can follow along with me, or if
02:20you'd like, you can just open up the Final version and see how everything works.
02:25So, I'm going to open up my Application Bar solution.
02:29So let's go to the Layout.
02:32So, here is the layout for the Application Bar demonstration, and to make some
02:38more room I'm going to unpin the Toolbox for a moment, and slide this over so we
02:43can see more of the code.
02:44So remember the first thing we need to do is add a Reference to the
02:49Phone.Shell assembly.
02:51So the way we do that is, over here in the Solution Explorer for the project,
02:56you'll notice that there is a whole bunch of folders.
02:58There is Properties, References. Here is all the resources that make them application.
03:03So in the References folder you'll see that there is a whole bunch of assemblies
03:07that got added by default when Visual Studio created the project.
03:11There are some Microsoft.Phone assemblies.
03:14There is a couple of System assemblies, and these contain a whole bunch of the
03:17common code that we use when building our applications.
03:21Now the Phone.Shell assembly isn't there, by default, so I'm going to fix that.
03:25I'm going to right-click and then choose Add Reference.
03:29And I am presented with a whole bunch of assemblies I can choose to add to my project.
03:35So, I'm just going to scroll down to Shell, right there, and click OK.
03:40And then you can see that the Phone.Shell assembly got added into my project.
03:47So that's how we add the Reference.
03:49Now, we need to make it so that we can use the objects and types that are inside
03:55that assembly from within our XAML code.
03:58Now the way that we do that is by typing a line of XAML right here at the top of
04:02the XML file, and I want you to bear with me for a second because this looks
04:06kind of complicated, but it's really not. I'll explain.
04:09What we need to do is add an XML namespace, and we're going to call our
04:13namespace the shell and what this allows us to do is reference, in XML, the
04:20objects that are inside that Code Library.
04:23So we're going to say that this namespace is equal to, and then we need to type out a string.
04:27So we need to tell the XAML designer where to get the types from, both inside
04:33the C# code's namespace and which assembly to get it from.
04:37So, we need to say that the clr-namespace -
04:41and if you are not familiar with this, don't worry about it;
04:43it's not something you need to know too much about right now -
04:46But the clr-namespace, we're getting these types from is the
04:49Microsoft.Phone.Shell namespace, and that is inside the assembly of
04:57Microsoft.Phone.Shell.
05:01That's all there is to that.
05:03So now that we have added that line of code, we can work with the Application
05:06Bar objects right from within XAML.
05:09So now I'm going to scroll all the way down to the bottom here, and the
05:13Application Bar is a property of the ApplicationPage.
05:16So, I don't want to put it inside this grid or anywhere in the layout.
05:19I want to have it be its own control.
05:21So I'm going to put it out here, and I'm going to say
05:25<phoneNavigation. PhoneApplicationPage.ApplicationBar>.
05:30So now I've added these two tags, and they will serve as the container for
05:37the Application Bar.
05:38So I'm going to start off by adding an Application Bar with some menu items.
05:44So, now I need to use the shell namespace, the one that we just created.
05:46So, I'll say shell, and you can see the ApplicationBar is right there.
05:51And I'll create the Application Bar container.
05:54Now, I'm going to start by adding some menu items.
05:57And the way that I do that is by typing <shell.ApplicationBar.menuItems>.
06:07Inside this container I am going to put each one of the menu items. I'll write
06:13shell, and you can see that there is a type here called ApplicationBarmenuItem. I'll choose that.
06:20And now the ApplicationBarmenuItem has a Text property, so I'll write Text = menu Item 1.
06:29And I can also add an Event Handler.
06:31So for example, the Click right here.
06:35And I can choose to create a New Event Handler.
06:37So I'll just double-click that, and you can see that it added a Click Handler
06:41for the menu Item named ApplicationBarmenuItem_Click.
06:45And now what I want to do is close the tag, and that one is done. And you know what?
06:50I'll add two of these.
06:51So I'll copy this one and paste it, and I'll name this one menu Item 2.
06:58And I'll leave the Event Handler the same.
07:01So the same Event Handler will handle both menu items.
07:04And you can choose to do this in your applications.
07:05You may choose to have separate event handlers. That's up to you.
07:09The last thing I want to do before I try this out is set a couple of properties
07:14on the Application Bar.
07:15So I'm going to write IsVisible="True" because we want to be able to see it,
07:20and IsmenuEnabled also equals to "True," so that we can see the menu.
07:26So that's pretty much all I need to do to add the menu items.
07:29So we're at a point now where we can try the code out.
07:32So let's click Go. We'll start Debugging. Up comes the Emulator, so we're debugging
07:40now. Switch over to the Emulator.
07:45Okay, so here is my sample application, and sure enough, you can see the
07:48little white ellipsis down here with this little semidark area, and that is
07:53the Application Bar.
07:55So when I click the little ellipsis over here, you can see that the Menu bar
07:58slides up, and there is my menu items.
08:01So that's great, in and of itself, but they don't really do anything because I
08:05have to write the Event Handler.
08:06So let's stop debugging, and go back to the code and write the event handlers.
08:10So I'll stop Debugging.
08:15So remember we added these event handlers right here.
08:20So if you look in the code you'll see that, as a convenience, Visual Studio added
08:25the Event Handler for us.
08:27So, just to keep things simple, I'll just an Alert, or in the case of .NET, it's a
08:31MessageBox, and I'll just say Show("You chose a Menu Item!", and then I'll say
08:41the title is menu Item, and we'll just have an OK button, MessageBoxButton.OK.
08:55So now we have our Event Handler actually doing something, so now let's run this again.
09:02There is the Emulator. Switch over.
09:05So, now we'll bring it up, and we'll choose menu item 1, and you can see
09:09that the MessageBox is, sure enough, being shown, and sure enough, for menu
09:14item 2, there it is.
09:15It seems to be working just fine.
09:17So, that's adding menu items.
09:18Now, let's go back and add some icons for our Application Bar.
09:23Okay, switch back to the code, and we'll Stop Debugging.
09:30Back in the XAML code, what we're going to do now is first add the images that
09:35will form the icons for the ApplicationBar.
09:39So the way that you do that is over here in the Solution Explorer you'll notice
09:43that there is a button here labeled Show All Files.
09:47So I'm going to click that, and you'll see that in addition to the folders
09:51that are already in the projects, there are several folders in my Project
09:55folder that are not included.
09:57Well, this one here, Images, contains all the images I want to use as my icons.
10:01So, I'm just going to right-click and choose Include In Project, and then for
10:06each one of these icons, you see that they are just regular PNG files.
10:11What I need to do is select all four of them, and in the Build Action I need to choose Content.
10:18So these will be Content that are included in the output, and I also wanted to
10:24make sure I choose Copy always.
10:28So those two settings will bundle these images with my project, and that I can
10:33refer to them using URIs, which will extract them from the application, and I can
10:38use them in the user interface.
10:40So now I have added the images. There they are.
10:42All right.
10:43so now back here in the XAML code, I need to add the container for the icon buttons.
10:49So, just like we did with the menu items, we're going to say
10:52<shell:ApplicationBar.Buttons>.
11:00So now I have got the code that's going to contain my button items.
11:03And now I need to add each icon button.
11:06So I say shell, and you can see there is a class right here for
11:10ApplicationBarIconButton, so I'll choose that.
11:14Now, I need to point the IconUri property at the name for each one of these icons.
11:22And I do that using a URL-like path so they are in the Images folder, so I'll
11:29/Images, and then I'll just write the name of the file, which is
11:36appbar.delete.rest.png.
11:43And I can also add a Click Handler, and you can see that, again, I have the option
11:48of adding a Event Handler here, which I'll just double-click, and I've added an
11:53Event Handler called ApplicationBarIconButton_Click.
11:59So now I have added my button. All I need to do now is copy this four times, so
12:05I'll copy it and paste, paste, paste.
12:09And I'll just change the name of each one of these guys.
12:11That one is download, that one is edit, and that one is feature.settings. So, I'll save.
12:25So now I have added my icon buttons.
12:28I have pointed them to the images, and I've added Click Handlers. All I need
12:30to do now is go back to the code, and you can see here is the Event Handler
12:37that got added, and just to keep things simple, I'll show a MessageBox for this one too.
12:43Only this time I'll say "You chose an Icon Item!", and we'll change that one to
12:50Icon as well, and we'll save.
12:53Go back to the MainPage.
12:54So now we're ready to try this out again.
12:56So, let's click Run. Okay. And Emulator is running. And there you can see there
13:05are the icons for each one of my buttons.
13:09You can see there are my menu items.
13:11It's in the menu Bar.
13:13So now when I click on an Icon item, you can see that I'm getting a different
13:17message than I was for the Menu bar.
13:21I will click on that.
13:24That's pretty much all there is to it for adding Menu items and Icon Bar buttons.
13:30One last thing I want to show in this example, we're going to go back to the code.
13:33I'm going to Stop Debugging.
13:37The last thing I want to show is how you can dynamically change the visibility
13:41of the different pieces of the Application Bar.
13:45So this means going back into the layout now.
13:46We're going to bring back up our Toolbox.
13:49So I'm going to add a couple of switches that control how the Application Bar is displayed.
13:56So let me bring out a little Text Block here, and size it, and that one will
14:05say, Application Bar Visible, and we'll bring out another one, and this one
14:17will say, Menu Enabled.
14:25I'm going to bring out a couple of ToggleSwitches. They're kind of big.
14:29We're going to go right there and make them the right size.
14:38Okay, there is one, and here's the other one.
14:47And I'll set them both to be checked, because we want our stuff to be visible.
14:53So we'll set the IsChecked property.
15:01So these two controls will control whether or not the Application Bar is visible,
15:08and whether or not the menu is enabled.
15:10So what we're going to do now is add a couple of Event Handlers.
15:13So for the Application Bar one, we're going to switch over to the Events tab,
15:18and what we're going to do is for the Checked, we're going to add
15:22onAppBarVisible event.
15:30So in the OnAppBarVisible event what we need to check is first, if
15:36(this.ApplicationBar is != null) because we don't want to call any methods on
15:47the Application Bar if it's not there or hasn't been created,
15:51so if we have an Application Bar, then we're going to change this to
15:55this.ApplicationBar.IsVisible is equal to the value of the ToggleControlSwitch.
16:05Now, the sender argument will be equal to the control that sent the message.
16:10In that case, that's the ToggleControlSwitch, but we need to cast it to a
16:13ToggleControlSwitch.
16:15So we're going to say that that's a ToggleControlSwitch sender.IsChecked.
16:31We actually have to cast that to a Boolean value.
16:36So when the user turns the AppBar Control on or off, this will make it
16:40visible and invisible.
16:42Actually, we need to do this for both when it's checked and when it's unchecked.
16:50So we'll set Unchecked and Checked to onAppBarVisible.
16:55We'll do the same thing for the Menu Bar being enabled.
16:58So once again, we'll add an event here called onmenuBarEnabled, same kind of thing.
17:08In fact, I'll just copy this, and paste it in here,
17:14only in this case it's not going to be IsVisible;
17:16it's going to be .IsmenuEnabled.
17:22In that case, yup, same thing;
17:23the sender will be the right ToggleControlSwitch, and we need to make sure that
17:28that's being called on both Checked and on Unchecked.
17:36So now, we can try this out.
17:38Let's go back to Emulator, and we'll make it visible.
17:44So now you can see that when I turn the Application Bar visible on and off, you
17:47see how the Application Bar is showing up and going away and similarly, when I
17:52change the Menu Enabled, that little ellipsis there is going away.
17:55So now it's off. Now it's on. Now it's off.
17:58Now, I can't bring the menu up, but now I can, and now I can't do anything at all.
18:06That's pretty much it.
18:07That's how you include an Application Bar in your application.
Collapse this transcript
Specifying an input scope for a text box
00:00In this lesson we'll see how to ease the typing of characters for certain kinds
00:05of situations when you are using a Windows Phone.
00:09Windows Phone Applications have an onscreen virtual keyboard called the Software
00:15Input Panel or the SIP, and applications can tailor the way that this keyboard
00:22is laid out so that based on the type of information that's being typed in, the
00:28process can be made a lot simpler. And this table describes some of the
00:33different ways that the SIP can be laid out.
00:37So for example, at the top of the table you see the Default value.
00:41This provides the standard QWERTY-style keyboard for typing.
00:46However, if you know that the user is going to be typing in say a URL for a
00:51Web address, you can set the input style for a text box to be tailored to typing in a URL;
00:58for example, the keyboard will be re-laid out with a little .com button and so on.
01:03And each one of these different types of layouts - there is about 10 or so and in
01:10fact there is probably more than just 10; these are the ten most common -
01:13when you are using these different kinds of layouts, it can really make entering
01:16text a lot easier for the users of your application.
01:20A great example is when someone is typing in, say, a Phone number.
01:23If you know that they are going to be typing a phone number in, then you can set
01:26the Input Scope of a text box to be, for example, a telephone number and rather
01:31than getting some giant keyboard, most of which they can't use, they will get a
01:35telephone number pad.
01:36Let's see how this actually works in practice by looking at a real example.
01:40I am going to open the project in my Visual Studio IDE, which is Open Project.
01:48And in the example file folder for this, you will see that there is a Start and
01:52a Final version. And the Final is the finished version. You can go ahead, jump
01:56ahead and look at that one if you want to, or you can follow along with me as I
02:00work on the Start version here.
02:02So I am going to open up the SIPInputScope project.
02:05So what we are going to do is create a layout that has a text field in it, and
02:14then we are going to set some properties on that text field that enable the
02:19easy entry of text.
02:21So let's start by getting some control onto our surface here.
02:24So I will bring out TextBlock Select that and make it a little bit bigger,
02:29and I will set the text for that to be surprisingly enough, enter some text. Hit Return.
02:40So below that I am going to put a TextBox.
02:43Here is my TextBox, and I'll make it a little bit, size it properly.
02:55So now I have a text box that I can type some text in.
02:57So now you have some controls that control how the textbox keyboard shows up.
03:05So I'll pull another TextBlocks out, and for this one, I will label it display
03:16the SIP for, and again, remember SIP means Software Input panel.
03:24So I am not going to do all of the options that I show before.
03:28I'll just choose a bunch that I think really illustrate the advantages.
03:33So we'll pull out some RadioButtons, and we'll get a whole bunch of these guys out here.
03:39Choose four: one, two, three and four.
03:46So each one of these guys need to put into a RadioButton group, remember, so
03:51that they all know how to toggle correctly, and we'll just call that grp1, and
03:56we'll do this for all of these guys, grp1 and last but not least, grp1.
04:08Now we'll set the text for each one of these guys.
04:09So the first one will be the Default setting, and then this one would be for
04:19Email, and then this one will be for a Web URL Address, and then the last one
04:30will be for a Phone Number.
04:34So now I have got four radio buttons, each one of them choosing a different kind
04:40of SIP, and I will have this one, the default one, checked.
04:45So now I have set up my UI, and what I need to do now is wire up some events so
04:51that when each one of these radio buttons is chosen, the right kind of SIP gets displayed.
04:56So what I will basically do is assign an event to the checked handler for each
05:02radio button, and inside the event handler, we'll see how the SIP gets set.
05:07So let's go ahead and assign some events.
05:11So I'll just select the guy and then over in the Events panel, under Checked,
05:17this one will be setDefaultSIP, and that's good, and then this one here will
05:26be setEmailSIP, all right, and then this one here will be setURLSIP, and then
05:43finally, this guy here will be setPhoneSIP, so we save and save this one, as well.
05:53So now I have all the event handlers wired up.
05:56So now let's go into the code and write the event handlers for each one of these guys.
06:01Since I am going to be setting the SIP each time, what I am going to do is write
06:06a utility function that just sets the SIP type. And I will just define that
06:10function private void setSIPType, and that will take an argument, and the type
06:20of argument that we want to put in here is called an InputScopeNameValue, and
06:30that will be the InputScopeNameValueSIP (isnvSIP).
06:36Now we need to write the body of this function.
06:38So basically, what we need to do is first, we need to check to see if the
06:42text box is not equal to null, textBox1 is not equal to null.
06:52If the text box is null, it means that the page has not finished loading yet,
06:55and we don't want to operate on it.
06:57So check to see if it's not null.
06:59So if it's not null, there is a couple of things we need to do.
07:01We need to create a new InputScope, and then once we've created the new
07:05InputScope, we need to create an InputScope name to go with it and then set the
07:11scope type on that scope, and then assign that InputScope to the text box.
07:16So here is how that looks in code.
07:18We write InputScope, declare a variable equals new InputScope
07:27Now we have that.
07:31We write InputScopeName, and that's the InputScopeName =
07:40new InputScopeName.
07:44I really like IntelliSense.
07:46Now that we have got that, we have to set the InputScope's Name name value
07:51field to the argument that we were passed into this function.
07:58So we are going to set that to isnvSIP.
08:04So we have created a new InputScope, and we set its value to whatever this
08:09function was passed.
08:11Then we tell the InputScope that we created to add to its names array the
08:22InputScopeName we just created. That's this guy right here, and that's the guy
08:27who has had his type set.
08:29So now this TextBox will have the InputScope named whatever the user chose
08:36among the RadioButtons.
08:37And then the last step is to say this.textBox1.InputScope = inps;,
08:51which is this right here.
08:53So we create a new InputScope, created new InputScopeName to go along with it,
08:57set the name value property to the property that we were passed in up here as
09:01an argument, add that into the InputScope's name's list and then set the
09:07InputScope onto the TextBox.
09:09That's the hardest that this is going to get, because now what we are going to
09:13do is fill in the event handlers.
09:14All we are going to do is say setSIPType to InputScopeNameValue.Default.
09:26In the default case, we just set it back to whatever the default is.
09:30For Email, we'll say setSIPType InputScopeNameValue.EmailNameOrAddress.
09:45For the URL, we'll call setSIPType InputScopeNameValue.,
09:55and I believe if I am not mistaken there is a URL in here, yup, there it is right there.
10:01In the last but not least one is the Phone Number, so we'll call setSIPType
10:11InputScopeNameValue.TelephoneNumber. Just so you can see, I mean look at how
10:21many there are, I hit dot and look at it how long this list is.
10:25There are a lot of InputScopes that you can use in your applications.
10:31So that should be all there is to it.
10:33Let's fire up the emulator and see if this works.
10:37So we'll save it, go back to the MainPage, start debugging. Up comes the
10:42emulator, and we'll switch over to it.
10:47Here is my UI, and now what I am going to do is I am going to click in the
10:49TextBox, and you can see here is the default keyboard that came up.
10:53So I have its regular QWERTY style keyboard, all the letters, I can choose
10:57numbers, go back to the keyboard, but that's for the default case.
11:02So now if I choose Email and I click in the TextBox, notice how the keyboard
11:07has changed slightly.
11:09I still get letters and numbers, but now I have an @ symbol and a .com symbol.
11:13So if I was typing someone's Email address, I don't have to switch to another
11:17keyboard to get to the @ symbol.
11:19It's right there for me, as well as the .com suffix.
11:23And if I click and hold on the .com suffix, you will see I get a whole bunch of
11:27options here org, com, edu and net.
11:30Such for the Email case.
11:32Let's try the Web URL case, and I think the Web URL case is actually not that
11:37much different than the Email case. You can see that the @ symbol is gone, but
11:40the .com is still there, and there is also a period over here on this side for
11:45typing periods in Web addresses.
11:48And then last but not least, let's look at the Phone Number case.
11:50I am going to select Phone Number and I click in the TextBox, you'll see I get
11:54a Phone Number pad.
11:55So this is a really great way for making it easy for people to type information
12:00into your applications based upon the type of information that it is.
12:04So using SIP InputScopes, you can customize the way that text fields show their
12:09keyboards to the user.
Collapse this transcript
Consuming web content with the WebBrowser control
00:00Windows Phone 7 also provides an embeddable Web browser control that you can use
00:06in your applications to consume and display rich content from the Internet, and
00:13that's what we're going to do in this lesson.
00:15We're going to build an application that uses the embedded Web browser.
00:18And it's basically the same version of IE that is on the Phone.
00:24All we're going to be doing is wrapping it in an application and then using it
00:27to go to some sites on the Web.
00:30And this is a really neat feature because you can imagine that if you're
00:33building an application that uses HTML content that you've already developed
00:37for other purposes,
00:38let's say you have a Web site that has a whole bunch of content that you want to
00:42reuse in your mobile app,
00:43it's really easy to do so using the embedded Web browser.
00:47The embedded Web browser also provides a way to listen in on events, such as when
00:52a document has completed loading or when it's about to navigate somewhere, and
00:57we will take a look at that in this example.
00:59So let's get started.
01:00I am here in the Visual Studio IDE.
01:04I'm going to open up my Webbrowser Project.
01:08This is in the Exercise Files folder.
01:11You see there is two versions:
01:12There's the Start version and the Final version, and I'm going to be working on the Start version.
01:16So can follow along with me if you'd like, or you can go ahead and jump to the
01:19finished version to see how everything works.
01:21So I'm going to open up my Start folder and open the Webbrowser.sln.
01:27So here we are in the IDE, and we're going to start by putting on some basic UI
01:32we're going to need to tell the browser to navigate.
01:35So, let me make this area a little bit bigger, so it's easy to read.
01:38So first, I'm going to bring out a text box, and the text box is going to be
01:43where we type our URL that tells the browser where to go.
01:47I'm just going to resize this appropriately.
01:52Then I'm going to add a Button to the canvas.
01:56And we'll just call this the Go button.
01:59Make it a little bit larger.
02:02Come down to the Properties and change the Content to just say Go.
02:08Resize it a little bit, so it fits. There we go.
02:16So now we have our UI on the canvas for navigating.
02:19Now let's bring out the actual Webbrowser itself, and that's down here in the
02:22Toolbox at the way bottom.
02:23You see it is right there. It is called the Webbrowser.
02:25I'm just going to drag and drop that onto the canvas.
02:28And you can see that this is the box that the Webbrowser will show up in, and at
02:32design time you just get like a little logo, but at run time this will be
02:36replaced with actual Web content.
02:38So let's just size this up so that it matches the rest of the controls.
02:45That's pretty much all there is to do in laying out the UI.
02:49If we take a look at the XAML code that was produced, you can see down here in
02:54the grid, there's our TextBox, our Go button, and here is our Web browser control
03:00right here, so, pretty straightforward.
03:04So what we need to do now is give the Webbrowser a default place to go to
03:09when the page loads.
03:11So the way that we're going to do that is by selecting it and then scrolling
03:16downion the Properties window until we get to the Source Property.
03:18We're going to have the Webbrowser start off at one of my favorite Web sites,
03:23which happens to be http://www.lynda.com.
03:31Let's take a look back in the XAML code to see what happened.
03:33I will scroll over, and you can see that I've set the initial Source Property to
03:39be the address of this Web site.
03:42And just to make sure that everything is in sync, we'll do the same thing with the TextBox.
03:48So I'll click on the TextBox and set the text to be the same thing:
03:54http://www.lynda.com.
04:02So now we've put the initial Source Property in the Webbrowser.
04:05Now we need to wire things up so that when the user enters a URL and clicks the Go button,
04:12we properly go to the right Web site.
04:15So let's click on the Go button and then over here in the Properties, we're
04:19going to click the Events tab and for the Click Event, we're going to create an
04:25EventHandler named onClickGo.
04:30So that will put us into the Source code for the main page.
04:35So now we need to write the EventHandler.
04:36So what we're going to do is we're going to have a string variable, and
04:38we'll call that site.
04:40And that will be the string that's in the TextBox for the Web site we are about to go to.
04:45We'll write site = textBox1.Text.
04:56So we get the text out of the TextBox, and now all we need to do is navigate the Webbrowser.
05:01So the Webbrowser, which if I'm not mistaken, we'll start typing Web. There it is
05:06right there, Webbrowser1.
05:08The Webbrowser has, surprisingly enough, a method on it called Navigate, and we'll
05:14just scroll down until we see it, and that's right there.
05:17So the Navigate function tells the Webbrowser to go to a URI.
05:23So we're going to write new Uri, and that's going to be the site, the string we
05:30just created and the kind of Uri, UriKind is Absolute.
05:43So we're going to make the user type in http.
05:46Obviously, this is kind of a barebones example.
05:49Well I am not writing any code here that checks to see if the user properly
05:53typed http:// at the start of the string.
05:57You know I could do a whole bunch of error checking here.
05:59I'm not doing that. This is just an example.
06:00But in a real browser, you'd have to do all these kinds of error-checking things.
06:05So that's pretty much all we need to do navigate the Web browser.
06:08So let's save and save the MainPage as well.
06:13And let's run this in the Emulator and see what happens.
06:16I'm going to go ahead and start debugging.
06:17I'm going to scale the Emulator down so that we can see it all.
06:25Okay, so we see that our application has launched, and the Webbrowser is navigating.
06:34It is going to take a minute.
06:35Okay, see there it is, right there. All right.
06:37so you can see that the Webbrowser navigated to the Web site, and it's kind of
06:41zoomed out to give the full view of the Web site.
06:43But the Web browser control has interactivity that knows how to zoom in.
06:49So all I'm going to do for example is double-click on this element right
06:52here, and you can see I can zoom in, and I can pan around the page, and I can
06:56zoom back out again.
06:59Now let's make sure that our EventHandler is working correctly.
07:03So I'm going to write a different address.
07:05I'm going to type in,
07:07let's see, http://www.xbox.com, another one of my favorite sites.
07:13Click Go and after a few seconds we should see that there it is.
07:17So you can see that we've basically built a barebones embedded Web browser using
07:22the Webbrowser control.
07:24But we can do a little bit better.
07:26Let's make it so that it's easier to type in URLs.
07:29And let's explore using some of the events that the Web browser fires off.
07:34So let's stop the Debugger.
07:36Go back to the code.
07:38And let's Stop Debugging.
07:41First thing we're going to do, and if you haven't watched the chapter on using
07:44input scopes you might want to watch that before do this example,
07:47but what I'm going to do is you've probably learned in the input scope lesson to
07:52fix the TextBox so that it's easier to type URLs in.
07:55What I'm going to do is make it easier to type in URLs in tothe TextBox.
08:01So the way that I'm going to do that is go back to the Design View -
08:04this is the MainPage - and I'm going to select the PhoneApplicationPage.
08:12You can see that here in the Events Property panel there is a Loaded Event.
08:17What I'm going to do is create an EventHandler named onPageLoaded.
08:24So when the page loads, we're going to create an input scope, and an input scope
08:29is a way of customizing the little virtual keyboard that pops up to be optimized
08:36for entering certain kinds of information, in this case a URL.
08:39So we're going to customize the keyboard by writing InputScope, input -- my
08:45variables are going to be inputScope;
08:46inputScope = new InputScope.
08:51If you watched the previous lesson on Input Scopes, you'll know what I'm doing here.
08:57So InputScopeName, and we'll call this one inputScopeName = new InputScopeName.
09:11Now we need to make the InputScope a URL type.
09:14So we'll write inputScopeName.NameValue = InputScopeNameValue, you can see the
09:25IntelliSense is helping me out here .NameValue.Url.
09:32Now that we've created the URLInput Scope; we just need to add it to the TextBox.
09:37So we'll say inputScope.Names.Add, and we'll add the InputScope name that we've
09:44just created, inputScopeName. That's that guy right there.
09:49Then finally we'll assign it to the TextBox, textBox1.InputScope = inputScope,
10:00the variable we've just created.
10:02So this will make it easier to enter URLs on the virtual keyboard.
10:05The second thing we're going to do is go back to the MainPage, and let's take a
10:09look at some of the Events that we can listen in on the Webbrowser.
10:14So I'll select the Webbrowser in the Design View, and we'll go over to the
10:17Events panel here, and we'll scroll down. Now a lot of the Events you see
10:21are just basic control events that are supported by pretty much every control
10:25in Windows Phone 7,
10:26but there are couples that are specific to the Web browser, for example, this
10:30one right here LoadCompleted.
10:32This Event fires when the Content in the Webbrowser has finished loading.
10:37There're also a couple of others.
10:38There is this one here, Navigated, which means that the Webbrowser control has
10:46finished navigating to the site.
10:48But this fires before the LoadCompleted Event fires.
10:50So in other words, you type an URL. The Webbrowser goes there, gets the Content,
10:54fires Navigated off and then loads the Content into the Webbrowser and then
10:58fires off the LoadCompleted event.
11:00There is also a companion event for Navigated called Navigating, and this event
11:05is fired right before the Webbrowser is about to navigate somewhere.
11:10When you listen in on this event, you can control whether the browser actually
11:14goes there, or you can make any changes to the URL that you want to that kind of stuff.
11:19So let's just do a simple example.
11:20Let's just listen in on the LoadCompleted Event.
11:25That's this one right here.
11:26So I'm going to write an event called onLoadComplete, and now that will put me
11:33back in the Source Code.
11:34And just to make things simple, we're just going to have a MessageBox show up
11:39that shows us that this did indeed fire correctly.
11:42So we'll write MessageBox.Show, and we'll write something like Webbrowser has
11:52loaded the content, and we'll give it a title like Content Loaded, and we'll
12:03show a MessageBoxButton.OK.
12:09So that's all we need to do now to listen in on the LoadCompleted event. So let's save.
12:14And let's go back to the MainPage and save.
12:16Let's rerun the application and see what happens.
12:23Emulator is running.
12:24You can see that we're navigating off to the lynda.com site.
12:31You can see that as the content is beginning to appear in the Design View, the
12:35LoadComplete Event has fired, and it says the Webbrowser has loaded the
12:39content, which is great.
12:40So let's take a look at our InputScope.
12:43So when I click in here, you can see that the virtual keyboard has now been
12:46customized to show the little .com button here and the period over here.
12:51So if I was using the actual device and typing in an address on the keyboard
12:57rather than using my Emulator here, it would be a lot easier for me type things in.
13:01So for example I can just write xbox and then just type .com, and we'll click Go.
13:09And once again, we should see the LoadCompleted Event fire. There it is.
13:15That's a quick lesson on how to build an embedded Web browser in your application.
13:19So now you know how to use rich Web content in your Windows Phone 7 Apps.
Collapse this transcript
Conclusion
Goodbye
00:01Okay, that brings us to the end of our First Look at Windows Phone 7
00:05Application Development.
00:06I hope that what you've seen and learned in this title has excited and inspired
00:10you to go grab those free tools, and download them, install them, and get a jump
00:15on developing the next generation of great mobile applications.
00:19Thanks for watching!
Collapse this transcript


Suggested courses to watch next:

C# Essential Training (6h 44m)
Joe Marini


Silverlight 3 Essential Training (6h 50m)
Walt Ritscher

Silverlight 4 New Features (3h 0m)
Walt Ritscher


Are you sure you want to delete this bookmark?

cancel

Bookmark this Tutorial

Name

Description

{0} characters left

Tags

Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading
cancel

bookmark this course

{0} characters left Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading

Error:

go to playlists »

Create new playlist

name:
description:
save cancel

You must be a lynda.com member to watch this video.

Every course in the lynda.com library contains free videos that let you assess the quality of our tutorials before you subscribe—just click on the blue links to watch them. Become a member to access all 98,695 instructional videos.

start free trial learn more

If you are already an active lynda.com member, please log in to access the lynda.com library.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Access to lynda.com videos

Your organization has a limited access membership to the lynda.com library that allows access to only a specific, limited selection of courses.

You don't have access to this video.

You're logged in as an account administrator, but your membership is not active.

Contact a Training Solutions Advisor at 1 (888) 335-9632.

How to access this video.

If this course is one of your five classes, then your class currently isn't in session.

If you want to watch this video and it is not part of your class, upgrade your membership for unlimited access to the full library of 1,899 courses anytime, anywhere.

learn more upgrade

You can always watch the free content included in every course.

Questions? Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com.

You don't have access to this video.

You're logged in as an account administrator, but your membership is no longer active. You can still access reports and account information.

To reactivate your account, contact a Training Solutions Advisor at 1 1 (888) 335-9632.

Need help accessing this video?

You can't access this video from your master administrator account.

Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com for help accessing this video.


site feedback

Thanks for signing up.

We’ll send you a confirmation email shortly.


By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked