I'm reading through Windows Phone 7.5 Unleashed, and there is a lot of code that looks like this (in the code-behind for a page):
bool loaded;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (!loaded)
{
DataContext = new SomePageViewModel(State);
loaded = true;
}
((SomePageViewModel)DataContext).LoadTransientState();
...
}
...
The idea is that loaded
will be false when resuming from a tombstoned state, so we know that we want to rebuild the view model.
My question is: Why load the transient state outside of the if
block? If our memory has not been thrown away (i.e., the application has not been tombstoned), can't we just keep using the old view model without reloading its state?
Should transient state always be loaded when a page is navigated to, or just when resuming from a tombstoned state?