Building your first Silverlight for Windows Phone Application
Join the DZone community and get the full member experience.Join For Free
silverlight for windows phone is one of the most exciting things to come along since the original release of silverlight. now i can take my same silverlight/wpf skills and use them not only on the web, and on the desktop, but also on the windows phone. silverlight for windows phone is an out-of-browser, chromeless, implementation of silverlight designed as the premier application development tool for windows phone. applications written in silverlight for windows phone are developed using visual studio and expression blend, and sold/deployed using the marketplace.
along with silverlight, we also have xna 4.0, targeted more towards game developers. i'll talk more about that in another post.
you've installed the bits and now you want to build your first application. here's a walkthrough of building a "hello world" application using silverlight for windows phone
note: everything here is based on pre-released bits from around the mix10 timeframe. details below subject to change in later releases.
when you open visual studio, you'll see three new project types under the silverlight for windows phone group (you'll also see a new xna game studio 4.0 group if you didn't already have that installed)
- windows phone application
- windows phone list application
- windows phone library
the three default templates are fairly straight-forward. select the windows phone application template if you want blank slate to provide your own experience. select windows phone list application if you want the main page of your application to be based around a list, typically a menu. select the windows phone library application to create a reusable library.
i'm going to start with a windows phone list application.
creating the windows phone list application
at this time, i'd like to thank the product team for producing a template which includes viewmodels by default. i've advocated for quite some time that even a simple viewmodel implementation, such as is used here, is beneficial over sticking all your code in the code-behind. if you want a more robust implementation, incorporating interfaces, ioc or other common patterns, you are free to build upon this to get there.
happily, the templates even use the same function and variable names i'd use (like notifypropertychanged(string propertyname). one difference in my typical implementation is the pages are at the root. i'll usually keep them in a views folder, although that does make for uglier navigation uris.
when you create a silverlight for windows phone project, you get a number of new references by default.
let's take a brief look at them.
this is split between the microsoft.phone.controls and microsoft.phone.controls.navigation libraries.
in the navigation library, you'll find phoneapplicationpage and phoneapplicationframe. the former is the base class for all pages. if you open up mainpage.xaml, you'll see it is derived from phoneapplicationpage. the latter is a phone-optimized version of the navigation frame control.
also in this library, you'll find the pageorientation enum. the phone includes an accelerometer which provides orientation information to the application.
this contains support for the customized phone navigation. specifically, you'll find in here the navigation related classes, controls and event args. this is also where system.windows.navigation resides.
this is all shell goodness. we'll cover shell and application bar integration in a future post.
running the application for the first time
the template creates an application that you can run immediately. it doesn't do much, but does let you baseline the app and emulator experience.
click on any of the items to display the details page. click the arrow at the bottom right to return to the main page.
my laptop has a touch screen, so the experience is like the world's largest windows phone. :)
i should also note that my laptop is fairly low-powered. it has an integrated graphics chip, and a relatively slow dual-core mobile processor. i'm running windows 7 64 bit with 5gig ram. the emulator is smooth as silk, with no stuttering or anything. considering what you're seeing here is a real emulation of the actual windows phone product, i'm impressed. now i just need to make my c64 emulator work as well on this class of machine :)
leave the emulator running so you don't hit the startup time again. end your silverlight application from within visual studio.
how the navigation menu works
navigation actually happens inside a couple functions on the main page.
private void listboxone_mouseleftbuttonup(object sender, mousebuttoneventargs e)
// capture selected item data
_selecteditem = (sender as listbox).selecteditem;
// start page transition animation
private void pagetransitionlist_completed(object sender, eventargs e)
// set datacontext of details page to selected listbox item
navigationservice.navigate(new uri("/detailspage.xaml", urikind.relative));
frameworkelement root = application.current.rootvisual as frameworkelement;
root.datacontext = _selecteditem;
the page itself is hard-coded to be detailspage.xaml. you could either put the url for each item inside your viewmodel, or you have something else in here which decides what page to navigate to based on the viewmodel content for that item.
in addition, the detail page's back button support is handled via a coupld handlers on the detail page. note that the default navigation support is overridden so that we can have an animated transition.
// handle navigating back to content in the two frames
private void phoneapplicationpage_backkeypress(object sender, system.componentmodel.canceleventargs e)
// cancel default navigation
e.cancel = true;
// do page transition animation
void pagetransitiondetails_completed(object sender, eventargs e)
// reset root frame to mainpage.xaml
phoneapplicationframe root = (phoneapplicationframe)application.current.rootvisual;
the backkeypress event is fired when you click the back button on the phone. but what's that goback bit, and why is the root visual a navigation frame? simple, it's all defined in app.xaml:
<!--rootframe points to and loads the first page of your application-->
<phonenavigation:phoneapplicationframe x:name="rootframe" source="/mainpage.xaml"/>
this way you don't need to have a main page that contains just a frame and is otherwise empty.
if you poke around in app.xaml, you'll also find all the style resources that make a windows phone app look like a windows phone app. nothing magic here, just good old fashioned styles and control templates.
responding to orientation changes
orientation is something new to silverlight. desktop silverlight applications have never really had to respond to orientation changes before. the phone is a different story: the form factor means orientation is likely to change pretty regularly.
the page class has two orientation-related events. the first is orientationchanging, which lets you perform layout or other tasks while the user is turning the device. the second one, which you will likely use more often, is orientationchanged.
you can also tell the runtime what page orientations your application will support. this is done by setting the supportedorientations property of the page, on a per-page basis. in the template, this is done in the constructor. possible values are :
as it happens, the value of portraitorlandscape (3) is equal to landscape | portrait. while the templates do relay on that at the moment, i'm more comfortable recommending that you use the actual enum rather than counting on the underling values. it's not actually defined as a bitfield, so boolean ops seem a little dubious.
let's modify the application to support both modes of layout. first we'll set the supported orientations for the main page in the constructor:
supportedorientations = supportedpageorientation.portraitorlandscape;
next we'll handle the orientationchanged event. the event handler wire-up goes in the constructor:
orientationchanged += new eventhandler<orientationchangedeventargs>(mainpage_orientationchanged);
the event handler itself just makes a change to the title of the page. ideally, you'd perform any layout changes here, such as moving some actions from the bottom, to the right, for example. you may invoke states using vsm as well.
void mainpage_orientationchanged(object sender, orientationchangedeventargs e)
textblockpagetitle.text = "hello world - landscape";
textblockpagetitle.text = "hello world - landscapeleft";
textblockpagetitle.text = "hello world - landscaperight";
textblockpagetitle.text = "hello world - portrait";
textblockpagetitle.text = "hello world - portraitdown";
textblockpagetitle.text = "hello world - portraitup";
here are screenshots in various orientations. look at the label on the page to see the result of the switch statement.
i wasn't able to get the emulator to turn upside down. that's probably a, uh, feature of this pre-release version :) note also that the enum values for portrait landscape (without left/right up/down) are never hit. tbd if that is something in the pre-release bits, or a function of the emulator.
the rest is, well, mainly silverlight. you'll be able to reuse your existing silverlight and wpf skills to create applications for the windows phone. i can't wait to see what you all come up with :)
Published at DZone with permission of Pete Brown. See the original article here.
Opinions expressed by DZone contributors are their own.