software solutions for a mobile world

Perst Database for Windows Phone 7: Demo Updated to RTM

Get the source code for Perst database on Windows Phone 7 and demo app here.

I’ve been a bit slow in doing this, but thanks to a number of requests from interested people, I have updated the Perst demo so that it works with the final version of the Windows Phone 7 libraries and tools. Click the link at the bottom of this post to download all the source code – includes the Perst database and the demo app.

The demo app is much expanded. Most importantly, it now fully supports tombstoning, which was a pretty significant omission from the previous version.

The demo app displays a list of contact records on the main page, and when the user selects a record in the list, the app navigates to a new page to show the details of the contact. This page now uses a Pivot control, so not only do we see the contact, but a swipe to left or right now displays the Leads for that contact.The best way of supporting tombstoning with a page displaying the details view of a single record in the database, is to save the object ID that every record in a Perst database has (inherited from the Persistent base class) when the app is being tombstoned, like this:

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
    base.OnNavigatedFrom(e);

    // Save OID of the current record we are viewing/editing
    State["DataContextObjectID"] = ((Contact)this.DataContext).Oid;
}

Then, when the app is being reactivated, we can do a simple database lookup to pull the required record out again:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    // For tombstoning, save the oid of a particular record in the State object on OnNavigatedFrom
    // Restore it in OnNavigatedTo and use the Database.Select<T> method to extract the correct one from the database
    if (State.ContainsKey("DataContextObjectID"))
    {
        int oid = (int)State["DataContextObjectID"];
        this.DataContext = Database.Select<Contact>("oid = " + oid.ToString()).FirstOrDefault();
        State.Remove("DataContextObjectID");
    }

    // Have we come here by navigating from the MainPage after selecting a record in the main list?
    if (this.NavigationContext.QueryString.ContainsKey("oid"))
    {
        this.DataContext = Database.Select<Contact>("oid = " + this.NavigationContext.QueryString["oid"]).FirstOrDefault();
    }
}

There are two ways for the details page to be activated: when the app is being reactivated after tombstoning and this page is the current page in the app, or when the user selects a record from the list on the main page. In the latter case, the demo app puts the Oid of the selected record into the query string. This is why the OnNavigatedTo method includes code to look for the Oid either from the State object (used to save context while tombstoning) or from the query string.

 

The previous version of the demo app included code to generate thousands of contacts. Each contact has a collection of Leads, and each lead has a collection of Activities. Lead and Activity objects were generated by the app before, but there was no UI to show them. Now there is:

imageimage imageimage

I haven’t included code to add, edit, delete Leads and Activities – but that is essentially the same kind of code that is included for performing those operations on a Contact. That’s left as an exercise for the interested reader Smile

Enjoy!


Posted Nov 09 2010, 04:36 AM by Andy Wigley

Comments

Dew Drop – November 9, 2010 | Alvin Ashcraft's Morning Dew wrote Dew Drop &ndash; November 9, 2010 | Alvin Ashcraft&#039;s Morning Dew
on 11-09-2010 8:28

Pingback from  Dew Drop – November 9, 2010 | Alvin Ashcraft's Morning Dew

Twitter Trackbacks for Perst Database for Windows Phone 7: Demo Updated to RTM - Andy Wigley - APPA Mundi [appamundi.com] on Topsy.com wrote Twitter Trackbacks for Perst Database for Windows Phone 7: Demo Updated to RTM - Andy Wigley - APPA Mundi [appamundi.com] on Topsy.com
on 11-11-2010 11:46

Pingback from  Twitter Trackbacks for                 Perst Database for Windows Phone 7: Demo Updated to RTM - Andy Wigley - APPA Mundi         [appamundi.com]        on Topsy.com

Copyright © 2011 APPA Mundi Limited. All Rights Reserved. Terms of Use and Privacy Policy. OrcsWeb's Windows Cloud Server Hosting