Join Michael Lehman for an in-depth discussion in this video Understanding the SDK, part of Windows Phone SDK Essential Training.
As we get ready to dig in and write real applications let's take a quick overview of the Windows Phone environment from a code perspective. We'll look a little bit at the foundations that are provided by the SDK. We'll look at some of the built-in features from a code point of view. We'll look at the options for persistence for saving data on the device and off. We'll look at some of the sensors that you can use to create a multidimensional application for your users. And we'll look at some of the latest enhanced user experience features of Windows Phone.
First, every Windows Phone app has three fundamental pieces: an application class which is what the operating system actually starts up when your app is launched, one or more pages where the content of your app goes, and an optional application bar as we saw when we were looking at the device that allows you to have icons and some additional menu items. This diagram which came from Microsoft's Developer Network shows the relationship of these things. You'll see the Frame is the entire screen, the Page is the piece where your content actually goes, and the Content Area which is inside that page is where your controls live.
The Frame contains the page and other elements such as the system tray at the top which shows your wireless connectivity and whether you're connected by Wi-Fi and your battery status. Of course, as we saw, the application bar can contain up to four iconic menu items plus up to five textual menu items. Now let's move from the abstract to the concrete by looking at the implementation of the Units Converter app we're going to be building shortly. First, if we look at the Emulator, you can see it doesn't quite fit the entire screen.
Even though we click the Fit to Screen, sometimes you have to come down and select an absolute size yourself to make it as large as it can fit onto your screen. So the text that's being rendered is as crisp as possible so that you can see all the pixels. In our application here, The stuff that's inside this black box that I'm showing with the mouse is the frame. You've got your system tray up here which doesn't show on the Emulator. You've got your application bar down here. And the page is everything that's sitting inside here, which in this particular case consists of our two text blocks for our labels and our two text boxes for our application.
This application is built with two pages, the conversion page we see here, and if we click on the application bar to bring up the menu, we'll see a caption underneath the icon, and we can click the about menu item and see the second page of our application. In addition to the application class, and whatever individual pages you have and the application bar, you can build applications that have multi-page controls such as the pivot or panorama. You can see an example of the pivot control in the Sonnets app which we're going to see in just a moment.
You can see an example of the panorama control in the People Hub on your phone. In addition, there's two kinds of message boxes which you can use to interact with your users, one that comes with the Silverlight built-in API that allows you to have a message and one or two buttons but the buttons will have to be labeled OK or Cancel, and another when that comes in the XNA GamerServices in which case you can not only specify the message, you can specify for the two buttons and whether you want to have a sound played or not.
So let's take a look at these in the concrete. In the Sonnets Plus application which we're going to be building, we use the pivot control to show two different views of the same list. Here we have all 154 of Shakespeare's sonnets, and here we have a list of the ones that we think are our favorites. Right now, that list is empty. We'll select one of the sonnets, click on the icon to say make that a favorite. You can see how a little gold star shows up there. And when we go back to the list now of favorites, there's our sonnet that we've chosen.
To demonstrate the two kinds of message boxes, we're going to look at the Take-a-Note application which we're going to be building in this course. If you attempt, for example, to try to play something before you've recorded anything, we use the Silverlight MessageBox that simply says No Sound Has Been Recorded Yet and has an ok button. As I mentioned, this particular kind of MessageBox can only have an OK or an OK and Cancel button. If we record something, like this, and you can see, the recording is happening, we've got the time elapsing here in our little meter bar, it plays back like this, and you can see the recording is happening, we've got -- and then if we come to our list of recordings, and we click on one of them and attempt to delete it, you see we use the GamerServices message box in which we're able to use yes and no as our captions, add a title, and add the sound.
As a final step in our exploration of foundations let's build an empty sample application and look at the code in the code behind for the App.xaml that Microsoft provides where you'll find methods that you're going to interact with as you flesh out your application from a sample into an actual app so that you can handle things like interruptions by the user getting a phone call. So we'll just pick Windows Phone Application template, once again, go through this dialog box.
In this case, we're not actually going to look at the XAML there. We're going to look at the App.xaml. If we look at the XAML for App.xaml, it's simply standardized templating things that Microsoft provides that says which class the application launches with. But what's more interesting is to look at the C# behind that and take a look at what methods are being provided that control your application lifecycle. First, there's this root frame--and this is what gets filled in by that information in the XAML that says which is the root frame which page does your applications start with.
There's some standard code here which initializes Silverlight, initializes the phone environment. This is interesting here this section because when you're debugging, these things get executed which is different than when your user is running your application outside of the debugger. This line here is what controls that frame rate counter we saw on the right-hand side of the Emulator, the vertical set of numbers. If you're running your app in the Emulator, and you want to take screenshots, and you don't want to have the frame rate counter, you can comment this out.
In addition, this line right here disables the idle time lock because normally when you have your phone connected via the USB cable to your development environment, you don't want the screen-locking when you're in the middle of debugging. But again, if you're trying to test what happens with your application when screen lock comes on, for example, in the Take-a-Note application, we wanted to make sure that we could record when the screen locks, you want to comment this out so that you can use your own control over whether the screen gets locked or not even if you are debugging.
If you want to have some code that runs when your application begins even before, everything else gets done. You put it in here in Application_Launching, you can put here if you want code that happens when your application gets restored from being suspended such as after a phone call. Here this is the code that happens when your application gets the phone call in the first place so that you can store your state. And here is what happens when the user clicks back that takes you out of your application but not because your application was interrupted.
The RootFrame_NavigationFailed message here is included so that when you're developing your application, if you attempt to navigate to a page that you haven't written yet, instead of crashing in a way that you can't figure out where it is because this back stack is gone. Again, here it says if the debugger is attached meaning you're running in the Visual Studio environment, it will stop so you can go look at the stack trace and figure out which page you actually wanted to go to, and you haven't implement yet. And again finally, another thing here to handle unhandled exceptions, by default, if you're running in the debugger, it will simply break into the debugger so that you can see what's going on.
In a production environment, you might want to put something in here to format up the exception information and send it off to a server for QA so that you can figure out when application crashes happen and see what's going on in your application. Now we've seen the basic nuts and bolts that are in every single Windows Phone 7 app. You've got an application class, you've got one or more pages, you're potentially going to have multipage controls, and an optional application bar.
- Downloading and installing the tools
- Understanding the SDK
- Designing the user experience (UX)
- Implementing commands
- Data binding with Silverlight
- Exploring the Model-View-ViewModel (MVVM) pattern
- Loading external data
- Capturing data from the camera, GPS, compass, and accelerometer
- Recording and playing sound
- Implementing persistence with isolated storage
- Leveraging built-in tasks, choosers, and launchers
- Expanding the available controls using the Silverlight and Coding4Fun toolkits
- Cloning a Windows Phone app
- Learning how to publish your app