0

I have a application built for both windows 8.1 and windows phone 8.1. There is a non static class with static members in a portable class library which is being shared by both tablet and phone projects. In phone, on tombstoning the static properties are lost. How do i store the static class members of a portable class library in deactivated event and restore(to the current instance of the non-static class inside the portable class library) in the activated event of the phone life cycle so that the after tombstoning the application continues to run without any crash.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880

1 Answers1

0

It depends on the access level of those static members.

If they're public and you can access them directly from your Windows Phone application, then you can just save and restore them as you normally would with your app's state in the appropriate application lifecycle handlers (or with the SuspensionManager class, if that's what you're using). This means that your portable class library doesn't care about "application lifecycle" or "saving/restoring state" or anything like that; instead it is the responsibility of your Windows Phone application to provide this behavior and your portable class library can remain independent of such Windows Phone-specific requirements.

If they're non-public, then your portable class library will need to provide some way of saving and restoring its internal state. You could expose static methods to do this which you would call in the appropriate application lifecycle handlers of your Windows Phone application. For example, you might have a method in your portable class library like:

public static void SaveState(Dictionary<string, object> stateDict)
{
    // store state of static members in the dict
    stateDict["Foo"] = SomeClass.FooStaticMember;
    stateDict["Bar"] = SomeClass.BarStaticMember;
    ...
}

You'd call this method in the suspending handler for your app, for example:

public App()
{
    this.InitializeComponent();
    this.Suspending += this.OnSuspending;
}

private async void OnSuspending(object sender, SuspendingEventArgs e)
{
    var deferral = e.SuspendingOperation.GetDeferral();

    // save PCL state here
    MyPortableClassLibrary.SaveState(SuspensionManager.SessionState);

    await SuspensionManager.SaveAsync();
    deferral.Complete();
}

Similarly, you would load the state in your App.OnLaunched override method when e.PreviousExecutionState == ApplicationExecutionState.Terminated. The Pivot App project template (or similar) contains skeleton code for application lifecycle events that you can work from.

How do i store the static class members of a portable class library in deactivated event and restore (to the current instance of the non-static class inside the portable class library) ...

The fact that your class is non-static doesn't matter; you have static members in that class which are shared amongst all instances of that class. You can't restore the state of static members of just one instance of your class; that doesn't make any sense.

Decade Moon
  • 32,968
  • 8
  • 81
  • 101