Windows Phone 7 – building for the future
A little over a month ago I went to a meeting at Trifork about Windows Phone 7, where my initial expectations got challanged. As an enthusiastic .NET developer the prospect of Microsoft “getting in to the game” of phone development is incredibly interesting. I did however leave the meeting feeling a little subdued, because quite of bit of talk ended up revolving around missing features and features that are not available in Denmark.
Copy/paste, danish dictionary, danish keyboard and tethering are features that are expected. Along with that we we given no clue about when there will opened for services like market, XBox Live and Zune market in Denmark.
On the positive side the speaker did really seem excited about the experience of using the phone and the developer tools. This along with the update feature that makes it possible for Microsoft to push out updates quickly sparked some hope. A few days later i recieved an invite for the Windows Phone 7 codecamps both in Silverlight and XNA – so I signed up so I could give it a fair shot.
Codecamp day one – starting with Silverlight
We kicked off the codecamp by writing a simple travel plan app that went against the danish site rejseplanen.dk. The basic idea was to use the WebClient class to call the rejseplanen website, and do some screen scraping to find the result that came back. So a simple enough task, so we could focus on the tools and have time to play around. We put the logic into a WCF service, so we could also see that using the service worked just as we are used to from Silverlight – so shortly put, you just need to do it asynchronously.
Project templates and basic navigation
With the tools installed you have the choice between 5 project templates: regular app, databound app, class library, panorama app and pivot app. They are all pretty much straight forward Silverlight projects that give you a starting point.
The only new things here is the panorama and pivot controls used by their corresponding project templates. They both provide ways of handeling navigation with in the app. Panorama giving the illusion of a larger canvas that you scroll horizontally, and pivot giving you a number of pages that you switch between by swiping horizontally. So it might sound similar, which it is to some extent, but there are differences – eg. with pivot the developer can change between the pages, which you cannot when using panorama.
One of the things we quickly ran into was that we would like to be able to use the pickers and choosers that you see used eg. in the calendar on the phone. These and a bunch of other nice controls are found in the Silverlight toolkit. Again it was easy peasy and smooth sailing all the way.
The last task of the day was to rewrite the app to use the MVVM pattern, where the instructor recommended MVVM Light. I decided to take piece of advice from a colleague and gave Caliburn Micro a shot instead. To be honest I am really happy with MVC being convention based, so when he told me that this was similar I was already sold.
Caliburn Micro was also pretty simple to get started with, and it quickly gave me that warm fuzzy feeling that you get when you can see that the app decouples nicely and becomes testable.
One of the things that we also talked about were tombstoning model. This simply means that no two apps are really open at the same time (in order to save resoures and power usage) but it is possible to fake regular app switching. Shortly put an application has events that notify the developer when apps are activated and deactivated, so they can maintain state through eg. isolated storage.
The pros of this model are that it is possible to limit how many processes are running, and therefore the use of resources. On the downside it makes it hard or even impossible to do certain types of applications because you might need the application to continue loading/playing in the background.
Silverlight is Silverlight is apparently not Silverlight
One of the important features of Silverlight for me is that it, as its original name WPF/Everywhere implies, should be able to run anywhere. Because WP7 was developed at the same time as Silverlight 4 it is running its own special version of Silverlight called Silverlight 3.7 – which naturally means compatibility issues. On top of this Silverlight can only be used to build apps on WP7 – not to be run from websites through the browser. Oh how sweet it would be if only Silverlight was just Silverlight as the name originally implied.
Hardware and design requirements
Microsoft have come up with a pretty specific list of requirements for the hardware on the phones that will be running Windows Phone 7. This is good news because it will make it easier to make apps that work across devices, and give a consistent experience. On top of this they have written a set of design guidelines that must be followed in order to be able to publish an app to market.
Codecamp day two – learning XNA from scratch
The second day I expected to be a big challenge, because I am completely new to XNA, so I really had no idea if we were able to get a game up and running in just one day. The task was to write a game called HitIt, which most people probably know. The idea is simply that some objects, in this case fish, fall from the top of the screen and they you have to tap them before they fall out at the bottom.
Building that kind of game in a day turned out to be very doable. It was a lot of fun to play around with and not at all as painfull as a thought it might have been.
Project template and content
There is a basic XNA Phone game template in the toolkit, which gives what you need to start. When you create a new solution it gives you two projects – one for the game itself, and one for content.
So step 1 was to find some images, sounds and fonts to include in the content project. We were provided with what we needed, which was a few wave files, some jpeg files and two spritefonts. A spritefont is just a little piece of XML that describes a font to be used.
The basic lifecycle and structure of a game
Building a game requires you to know a few concepts. Basically the game runs in what is called a gameloop, where the entrypoint class which inherits from Game which has Initialize, LoadContent, Update and Draw methods. These methods are called in turn and with the last two being called over and over again as long as the game is running.
In the LoadContent method you, as the name emplies, load the content which can be types like Texture2D, SoundEffect and SpriteFont. Update is then where you calculations and logic to control the flow of the game.
The Draw method handles drawing the elements. This is done using an object called a SpriteBatch, which has Begin and End methods that are called before and after all the content is written. This way everything between the Begin and End is written in one batch. So to draw content you simply use either Draw or DrawString on the SpriteBatch class. These have a bunch of overloads that take the content objects as well as paramaters that describe where content should be rendered, which color to use, rotation etc. Placing elements is done using Vector2D objects or Rectangles – so for complex games this is where the math begins to hurt you 🙂
Thoughts based on my first experiences
My enthusiasm about the Windows Phone 7 series has been ignited enough for me to order one as soon as the HTC Mozart is available in Denmark. I believe the platform is pretty solid because of the feedback people give on the user experience, and because it seems Microsoft have made some good design choices. Starting out they will need to play catch up on some areas, but it seems like Microsoft will dedicate the resources needed to do this.
As a developer it has been a lot of fun from day one. The tools and the emulator are easy to get started with, and it quickly gives you a ton of ideas as to where this could provide value to customers. I for one can’t wait to get my phone, and get started on a real project…
Christian Holm Nielsen