This project is read-only.
In addition to the instructions provided here, the source code includes a sample application to show how the library is used, so use it as a resource to find details or answers to questions that the instructions below do not provide.

Step 1: Add a reference to WP7AppLifecycleService.dll
In your project references, add a reference to WP7AppLifecycleService.dll.

Step 2: implement interface WP7AppLifecycleService.IWP7Application in your application class

The actual implementation of the class requires accessing the RootFrame of the application, so your application class must implement an interface called WP7AppLifecycleService.IWP7Application. A sample implementation can be seen below:

#region interface WP7AppLifecycleService.IWP7Application

PhoneApplicationFrame rootFrame;

/// <summary>
/// Provides easy access to the root frame of the Phone Application.
/// </summary>
/// <returns>The root frame of the Phone Application.</returns>
public PhoneApplicationFrame RootFrame 
{ 
    get { return rootFrame; }
    private set
    {
        if (value != rootFrame)
        {
            rootFrame = value;
            NotifyPropertyChanged("RootFrame");
        }
    }
}

public event PropertyChangedEventHandler PropertyChanged;

protected void NotifyPropertyChanged(String propertyName)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (null != handler)
    {
        handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

#endregion interface WP7AppLifecycleService.IWP7Application


Step 3: Add the Service to your application

In order for the library and service to handle the tombstoning logic for you, add a new service object to the ApplicationLifetimeObjects instance.

/// <summary>
/// Constructor for the Application object.
/// </summary>
public App()
{
     #region Add WP7AppLifecycleService to applicationLifetimeObjects

    this.ApplicationLifetimeObjects.Add(WP7AppLifecycleService.WP7AppLifecycleApplicationService.Current);

    #endregion Add WP7AppLifecycleService to applicationLifetimeObjects

    // rest of App ctor code here
}


*Step 4: Implement WP7AppLifecycleService.IApplicationStateHandler and WP7AppLifecycleService.IPageStateHandler

It´s not mandatory to implement both, but if your application needs to save and restore both application state and page state, an implementation for both will be required.
  • WP7AppLifecycleService.IApplicationStateHandler will usually be implemented by the ViewModel
  • WP7AppLifecycleService.IPageStateHandler will usually be implemented by the ModelView

A sample implementation for a page handler would be

#region interface IPageStateHandler

public void SavePageState(System.Collections.Generic.IDictionary<string, object> state)
{
    state["StartPage.selectedItem"] = listBox.SelectedItem;
}

public void RestorePageState(System.Collections.Generic.IDictionary<string, object> state)
{
    if (state.ContainsKey("StartPage.selectedItem"))
        listBox.SelectedItem = state["StartPage.selectedItem"];
}

public void DeletePageState(System.Collections.Generic.IDictionary<string, object> state)
{
    if (state.ContainsKey("StartPage.selectedItem"))
        state.Remove("StartPage.selectedItem");
}

#endregion interface IPageStateHandler


A sample implementation for a page handler could be:

#region interface IApplicationStateHandler

public void SaveApplicationState()
{
    PhoneApplicationService.Current.State["viewModel.text"] = this.Text;
}

public void RestoreApplicationState()
{
    object value = null;
    if (PhoneApplicationService.Current.State.TryGetValue("viewModel.text", out value))
    {
        this.Text = value as string;
    }
}

public bool IsThereApplicationStateSaved
{
    get
    {
        return PhoneApplicationService.Current.State.Keys.Contains("viewModel.text");
    }
}

public void DeleteApplicationState()
{
    PhoneApplicationService.Current.State.Remove("viewModel.text");
}

#endregion interface IGestorstateAplicacion


Step 5: Register handlers for specific pages

For each page you want state to be saved at tombstone time (only page state, only application state, or both), you must register the actual handlers for the specific page by calling:
  • WP7AppLifecycleService.WP7AppLifecycleApplicationService.Current.RegisterPageStateHandler to register the page state handler
  • WP7AppLifecycleService.WP7AppLifecycleApplicationService.Current.RegisterApplicationStateHandler to register the application state handler.

Last edited Jul 3, 2011 at 12:35 PM by david_carballo, version 3

Comments

No comments yet.