Windows Phone-use the sterling database to achieve tombstone (tombstone) [Zhuan]

Source: Internet
Author: User

If your WP application already uses the Sterling database, use Sterling to generate a tombstone (Tombtsone. Jeremy Likness's blog details how to use Sterling to implement Tombstone in the MVVM architecture. There are several main points:

 

1. Define a Tombstone data class. The Singletone Serialization mentioned in the Sterling User Guide is used.

 

 
public class TombstoneModel
{
    public TombstoneModel()
    {
        State = new Dictionary<string, object>();
    }
    public Dictionary<string, object> State { get; set; }
    public T TryGet<T>(string key, T defaultValue)
    {
        if (State.ContainsKey(key))
        {
            return (T)State[key];
        }
        return defaultValue;
    }
}


 

2. Define an ITombstoneable interface. The Active and Deactive methods are called in the NavigatedTo method of view and the NavigatedFrom method.

Store the Tombstone data class in the Deactive Method to the database, and retrieve the Tombstone data in the Active method to restore the view model status.

 

3. implement this interface on the view model that requires Tombstone

 

In many cases, the View Model can work only after the View Loaded. Jeremy uses an anonymous event to handle the event and runs the Active method after Loaded.

 

public static void ActivatePage(this PhoneApplicationPage phonePage, IViewModel viewModel)
{
    RoutedEventHandler loaded = null;
    loaded = (o, e) =>
                    {                             
                        ((PhoneApplicationPage) o).Loaded -= loaded;
                        if (viewModel is ITombstoneFriendly)
                        {
                            ((ITombstoneFriendly) viewModel).Activate();
                        }
                    };
    phonePage.Loaded += loaded;
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    this.ActivatePage(GlobalManager.GetViewModel<IMainViewModel>());
    base.OnNavigatedTo(e);
}

In addition, Sterling currently does not support IList serialization. After being Active, it will find that the items in this set are null. This problem is mentioned in the discussion group and can be solved by using List instead.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.