Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Phone 7: Tombstoning with URIs?

I'm building a wp7 app in Silverlight. All of my app's state is stored in NavigationContext.QueryString. If this could be saved on application deactivation, and that page navigated to on application reactivation, that would take care of my requirements for tombstoning.

However, I'm not quite sure how to do this. I was thinking about saving NavigationContext.QueryString to the State dictionary in App.xaml.cs::Application_Deactivated(), but that code doesn't have access to NavigationContext.QueryString. Is there another way I can do this?

I suppose I could just save the query string to the State dictionary every time I navigate, then restore that when the app is re-activated. Or is there a better approach?

Update: Based on indyfromoz's answer, I'd like to implement the following

OnNavigatedToHandler()
{
     // save NavigationContext.QueryString in the State dictionary
}

To reduce redundancy, I thought I'd implement this in a class that inherits from PhoneApplicationPage, then have all the rest of my pages inherit from that class. However, I then get the problem that all the page classes are partial because they are also defined in generated code. I don't want to change the generated code, because rechanging it every time it gets regenerated would be a huge pain.

Is there a better way to do this?

Update 2: Here is what I am hacking together now in the main page of my app (the one that is navigated to on startup):

public partial class MainPivot : PhoneApplicationPage
{
    public MainPivot()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(MainPivot_Loaded);
        PhoneApplicationService.Current.Deactivated += new EventHandler<DeactivatedEventArgs>(App_Deactivated);

        MessageBox.Show("launching main pivot (state count: " + PhoneApplicationService.Current.State.Count + ")");
        if (PhoneApplicationService.Current.State.Count != 0)
        {
            Debug.Assert(PhoneApplicationService.Current.State.ContainsKey(QueryStringKey), 
                "State is initialized, but contains no value for the query string");

            string oldQueryString = (string)PhoneApplicationService.Current.State[QueryStringKey];
            MessageBox.Show("Old query string: " + oldQueryString);
            NavigationService.Navigate(new Uri(oldQueryString));
        }
    }

    public readonly string QueryStringKey = "queryString";

    void App_Deactivated(object sender, DeactivatedEventArgs e)
    {
        PhoneApplicationService.Current.State[QueryStringKey] = NavigationService.Source;
    }

    // ...

It works (sorta) but it's ugly.

Update 3: Looks like the wp7 OS will automatically reload the correct page in a page-based app. I am in fact using pages, so perhaps there's not that much work I need to do here.

However, it doesn't seem to be working. I launch the app, go to a page, hit "Start", then hit "Back". The screen says "Resuming..." but seems to hang there. Is my code supposed to be responding in some way at this point? Is there a way I can keep the debugger attached even after hitting "Start"?

like image 542
Nick Heiner Avatar asked Nov 04 '10 18:11

Nick Heiner


1 Answers

Transient data is usually stored in the State dictionary provided by the PhoneApplicationService class. The data is stored in the OnNavigatedFrom event of the page and restored from the OnNavigatedTo event of the page. If you stored the parameters from the URI of the page in the state dictionary within the OnNavigatedFrom event that is available in every page of your application, you can implement the logic to read the parameters in the OnNavigatedTo event, thereby taking care of Tombstoning

HTH, indyfromoz

like image 142
indyfromoz Avatar answered Nov 07 '22 20:11

indyfromoz