By Denisse Zavala
Microsoft recently revealed the details of the Windows Phone Application Platform. It is built upon Silverlight, XNA Framework and the .NET Compact Framework, and MS rely on this combination to claim that development of rich applications will not have a steep learning curve for developers who are familiar with these technologies.
I wanted to get my hands on this platform so I used my basic Silverlight skills to create a really simple application to manage a To-Do list. Here the details:
First thing to do is get the Windows Phone developer tools CTP, which includes the VS 2010 Express for Windows Phone development tool and an add-on to the VS 2010 RC to enable phone development within the full VS 2010. These tools are not compatible with retail (RTM) release of VS 2010, so we’re stuck with the RC for a few more weeks. Installing the tools is as straightforward as with most MS developer tools, so there’s not much to say about it.
THE PROJECT
After launching the VS 2010 Express for Windows Phone I created a new project using the Windows Phone Application template:
VS opened the design and markup view, and it was no surprise to see the xaml markup, remember: Silverlight is the platform for Windows Phone 7 Series development.
The layout is a grid that holds some TextBlock controls, aTextBox, a Button, and a ListBox:
THE CODE BEHIND
Front end done! When I went to the code behind there was pretty much the same code from a Silverlight Desktop app: a partial class, a constructor that initializes the components, and two event handlers created when I added the markup for the Button and CheckBox Click event.
Two things draw my attention: the PhoneApplicationPage class (inherited by the MainPage class) and the SupportedOrientations property. The first is the equivalent to the Silverlight Page class. The second introduces a new concept to Silverlight: Orientation.
Orientation was never taken into account when building desktop Silverlight applications, however, when it comes to phone development, it is a big deal. The PhoneApplicationPage has two events that relate to orientation –OrientationChanging and OrientationChanged – and a property, SupportedOrientations, which specifies the orientations supported by the page (Landscape or Portrait).
I needed to define a collection to hold the tasks, and as this collection needs to be updated regularly I used an ObservableCollection:
Since Windows Phone 7 does not support SQL Server I decided to use Linq to XML to persist the tasks to a file named Tasks.xml, which must be included in the project in order to be packaged into the .xap.
The structure I used for the xml file is:
To use Linq to XML I needed to add the appropriate reference to the project:
The access to the file system in Windows Phone 7 is restricted, so to store files into the phone we need to rely on the IsolatedStorage. This feature allows isolating data by user and by assembly, or by application domain; data is saved to a compartment that is associated somehow with the code’s identity (Web site, publisher or signature); the compartment is not a specific storage location, instead, consists of one or more isolated storage files, called stores, that contain the actual directory locations where data is stored.
I created a method that uses IsolatedStorage to retrieve saved tasks, fill the Taks collection and then assign this collection to the ListBox ItemsSource property.
Needless is to say that a SaveTasks method was necessary and that it too relies on IsolatedStorage:
Remember those event handlers VS was kind enough to create for me? It is time to define what they will do.
When clicking the button I wanted to display the new task and save it to the xml file. The CheckBox click will cause to delete the related task from the list and save the new list to the xml file, overwriting existing elements.
The only thing left was to call LoadTasks() from the constructor:
RUNNING THE APPLICATION
The Windows Phone 7 tool includes an emulator – the phone operating system running on a virtual machine – that will run our applications just as if we’re using the actual phone. We can choose to deploy the app to the actual device or to the emulator.
I chose to deploy to the emulator, and after waiting for a while, the application loaded as shown below:
Working as expected, when adding tasks they are persisted and even after restarting the app they are loaded; when deleting, a message box is show to confirm the action.
That’s all, I have my To-Do list that persist the tasks in the phone. Quite simple and straightforward, obviously this application uses just basic features and its sole purpose was to introduce to this new platform, however, stepping into development of more complex and sophisticated applications doesn’t seem to be a hard transition for .NET developers.
Thank you for reading this article I made
Regards.
No comments:
Post a Comment