Windows Phone 7: Tombstoning with a URI?

I am creating a wp7 application in Silverlight. All my application state is stored in NavigationContext.QueryString. If this could be saved when the application was deactivated, and this page switched to reactivating the application, this would take care of my requirements for the tomb.

However, I'm not quite sure how to do this. I was thinking about saving NavigationContext.QueryStringto a dictionary Statein App.xaml.cs::Application_Deactivated(), but this code does not have access to NavigationContext.QueryString. Is there any other way I can do this?

I assume that I can just save the query string in the State dictionary every time I navigate, and then restore it when the application is reactivated. Or is there a better approach?

Refresh . Based on the answer from indyfromoz I would like to implement the following

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

To reduce redundancy, I decided to implement this in a class that inherits from PhoneApplicationPage, and then all the rest of my pages inherit from this class. However, I get the problem that all page classes are partialbecause they are also defined in the generated code. I do not want to change the generated code, because reinstalling it every time it is restored will be a huge pain.

Is there a better way to do this?

Update 2 . Here is what I’m hacking on the main page of my application (the one that is at 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 is ugly.

3. , wp7 . , , , .

, , . , , "", "". "...", , , . - ? , , ""?

+5
1

PhoneApplicationService. OnNavigatedFrom OnNavigatedTo . URI OnNavigatedFrom, , OnNavigatedTo, Tombstoning

HTH, indyfromoz

+5

All Articles