One of the key point in the WinRT application development is that we are required to handle the application state transitions, as in Windows Phone, in order to correctly manage the fact that the operating system at some point can suspend/terminate our application while it is in background.

Obviously this must be completely transparent to the user thus when the user comes back to our application he expects to find the application in the same exact state were she left.

We have to take care of 2 main concern:

  1. The navigation stack;
  2. The application state in term of “current data”;

Obviously if we use Radical for WinRT (codenamed Radical Chic and available on Nuget as pre-release) we can do something like:

sealed partial class App : Application
{
ApplicationBootstrapper bootstrapper;

public App()
{
this.InitializeComponent();

this.Suspending += OnAppSuspending;

this.bootstrapper = new PuzzleApplicationBootstrapper<Presentation.MainView>();
}

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

await this.bootstrapper.OnSuspending();

deferral.Complete();
}
}

What are we doing?

We are setting up the Radical Presentation infrastructure using the built-in application bootstrapper and, much more important we are handling the “Suspending” event delegating the execution to the bootstrapper itself.

Back to our problems…

The Navigation Stack…

…is not a problem, since we are using the Radical infrastructure we can leverage the power of the built-in navigation service and forget the whole stuff. The navigation service will:

  • keep track of the navigation stack;
  • take note of the the user is in the stack;
  • persist the stack when the application is suspended;
  • resume the stack when the application is resumed;
  • if the application is resumed restore the navigation stack bringing the user back to were he left;

Application state: easy peasy

the navigation infrastructure gives you something I call “INavigationScopedStorage” that can be used in the following manner:

class MainViewModel : AbstractViewModel, IExpectNavigatedToCallback, IExpectNavigatingAwayCallback
{
void IExpectNavigatedToCallback.OnNavigatedTo( NavigationEventArgs e )
{
if ( e.Mode == NavigationMode.Resume )
{
var data = e.Storage.GetData<String>( "myData" );
}
}

void IExpectNavigatingAwayCallback.OnNavigatingAway( NavigatingAwayEventArgs e )
{
e.Storage.SetData( "myData", "this is the data", StorageLocation.Local );
}
}

As you can see we have a simple ViewModel that implements 2 interfaces in order to get involved in the navigation pipeline. In the navigation event arguments we have we can use the Storage property (that is an instance of INavigationScopedStorage) to store data that are scoped the that view.

So…each time we navigate away we simply store what we may need if the application will be suspended/terminated and when we navigate to a view we can detect, using the NavigationMode, if we are getting here due to a restore and correctly handling the scenario.

.m