• Windows Phone 7: Saving data - Persistant storage

    Oct 25, 2010

    So now we have our application page data bound we can save the actual data using our application's store.

    Within our page's xaml.cs we'll have a save function that'll be called by clicking a save button. Function will contain:


    So you can see here how we can easily save the data in our application's store. Retrieving this is a little trickier, however. My example will be cautious, perhaps overly so. Within the Application_Launching event we'll add this code, which looks for our saved data and retrieves it then binds it to our page.



    As you can see I frequently check that the data we're retrieving is what we're expecting. If the file doesn't exist we bind it to an fresh class to allow for saving. Provided that our data bindings are correct we can re-save/load with ease.

  • Windows Phone 7: Saving data - DataBinding

    Oct 23, 2010

    In my last post I gave an introduction to the Windows Phone 7: Application Lifecyle. This one is a bit about binding your user interface in order to store persistent and transient data on the device.

    So Windows Phone 7 isn't just a phone, it's a pretty awesome gadget and work tool. [I want one - accepting donations ;)]. Users will, no doubt, be making heavy use of the applications available on the market place daily. But there will be many distractions and needs to exit your application. Although you can override the back button press you can't stop this from happening so if you want to keep data you'll need a place to store it.

    Thankfully, WP7 lets you do this easily with the methods fired on application events. It just requires a little know-how and you'll be away persisting and using data in your application.

    Firstly a bit about how to link your interface to a class using your data. You'll need a class for the data items on the screen you wish to save if you're saving user inputs. Here's an example from a basic application:


    From this you can see that the private members NotifyPropertyChanged(Name) any time they are changed. This is a private function, which we've made that notifies your application that a field has been updated. For your application to incorporate this the XAML for the text attribute of your input field should be bound to this object.

    To do this open up App.xaml.cs and instantiate your class within the Application_Launching method. Then set to be your data context:
    RootFrame.DataContext = yourClass;
    Then in your page's XAML file under the Text attribute add {Binding Persistant, Mode TwoWay}. Now when you run your application your class is bound to the user interface. Ready to start saving. Being TwoWay allows you to update the field programatically as well as the user having being allowed to do so.

  • Windows Phone 7: Application Lifecycle

    Oct 21, 2010

    This week's release of Windows Phone 7 has boosted my enthusiasm to develop for the platform. If you're a student you may want to do the same - especially as it's free, for you, thanks to Microsoft's Dreamspark. You can put up to 5 free applications on the Windows Phone Marketplace and start making a name for yourself; at no cost.

    What Microsoft are doing with WP7 seems to be great and well thought out. People want responsive, feature rich devices. Developers want to be able to utilise all of the platform, without having to worry about what the user is running. The way running in the background is (or isn't) handled is better than I initially thought. WP7 allows developers to have a near guaranteed set of resources for their application, which allows feature and graphically rich applications to run at full tilt while preserve battery life.

    This, however, means as a developer you need to be aware of these interruptions and how you want to handle them in your application. The file that these methods are stored in is App.xaml.cs. Inside this there are 4 methods to utilise, however, first let's take a look at the application life-cycle for WP7. There are a number of states that an application can go through so here's a diagram (click for full size):

    A little tricky at first glance, but with a bit of explanation it's fairly simple. The top row is essentially a run-to-exit flow of processes. From the running state it becomes a little more interesting. An application can be deactivated in a number of ways; text message, phone call, user hitting start, user searching. Unless you're lucky enough to partner with Microsoft to allow running in the background, your process is killed. It doesn't run in the background and, unless you've handled it, the transient data on screen is gone too. BUT the screen is saved on the 'Application Back Stack'. So if the user then hits the back button on the phone, they'll return to the last screen they were on in your application.

    Beware: There is no guarantee of the user doing this so your application will have lost any volatile data. The dotted line is just to show that it may happen.

    It is possible to save this 'transient' data just before your application is tombstoned during the 'Application_Deactivated' event. I'm sure you'll have noticed that some of the stages are red. These stages have fire methods, within App.xaml.cs. If you open the file, and take a look the methods, there are some very useful comments describing when each is fired. Restoring the transient data from a back key press the logic needs to be performed within the 'Application_Activated' event.

    To save persistent data you can either save on a user-triggered event or within the 'Application_Closing' method. To load this add your logic to the 'Application_Launching' event.

    I recommend getting a feel for these events for yourself so you fully understand them. An easy way to do this is to have a play with a Hello World on the phone, but have something within each event, for example:
    Debug.WriteLine("Hello World : Application Launching...");
    A problem I have found with the emulator is re-activating an application too quickly can cause the emulator to freeze/crash. Just wait to see the threads are terminated before pressing the back button to avoid this.

  • #techin8 - What's it about?

    Oct 7, 2010

    This is a short one. If you're a follower of @MsUkStudents on twitter, then you'll no doubt be seeing some #techin8 tags floating around, with 8 words. These words are on a weekly/bi-weekly theme. The current theme is
    "IE9 and HTML5"
    Why not save the search and follow the tag to see what others have to say?
    It's only reading 8 words after all... Have your say!