Project Description
This library offers an easy way to implement both tombstone and permanent persistence in WP7, WP8 & Windows 8 applications.
The source code includes a demonstration of how to use the library with a normal MVVM pattern and MVVM Light.
Known Obelisk Implementations
The award winning AAWP WP app
AAWP Windows 8 app
Custom GraFix Windows 8 app
Keep Calm WP app
Keep Calm Windows 8 app
Obelisk Checklist
1. Make sure the Persistence Manager control methods are connected to the application event methods in App.xaml.cs:
WP7 / WP8:
private void Application_Launching(object sender, LaunchingEventArgs e)
{
// Notify PersistenceManager of launch
PersistenceManager.Instance.OnLaunching();
}
private void Application_Activated(object sender, ActivatedEventArgs e)
{
// Notify PersistenceManager of activation
PersistenceManager.Instance.OnActivated(e.IsApplicationInstancePreserved);
}
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
// Notify PersistenceManager of deactivation
PersistenceManager.Instance.OnDeactivated();
}
private void Application_Closing(object sender, ClosingEventArgs e)
{
// Notify PersistenceManager of closing
PersistenceManager.Instance.OnClosing();
}
Windows 8:
protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
// Notify PersistenceManager of launch
await PersistenceManager.Instance.OnActivated(false);
}
else
{
// Notify PersistenceManager of launch
await PersistenceManager.Instance.OnLaunching();
}
// Create a Frame to act navigation context and navigate to the first page
var rootFrame = new Frame();
rootFrame.Navigate(typeof(MainPage));
// Place the frame in the current Window and ensure that it is active
Window.Current.Content = rootFrame;
// Register nav
PersistenceManager.Instance.Register("_nav", new NavigationManager(rootFrame));
Window.Current.Activate();
}
protected override async void OnActivated(IActivatedEventArgs args)
{
base.OnActivated(args);
// Notify PersistenceManager of activation
if (args.PreviousExecutionState == ApplicationExecutionState.Suspended
|| args.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
await PersistenceManager.Instance.OnActivated(false);
}
else if (args.PreviousExecutionState == ApplicationExecutionState.ClosedByUser)
{
await PersistenceManager.Instance.OnLaunching();
}
}
private async void OnSuspending(object sender, SuspendingEventArgs e)
{
// Get deferral
var deferral = e.SuspendingOperation.GetDeferral();
// Notify PersistenceManager of deactivation and wait
await PersistenceManager.Instance.OnDeactivated();
// Notify OS we're done
deferral.Complete();
}
2. Make sure the PersistenceManager is attached in the App.axml.cs InitializePhoneApplication method for WP7:
PersistenceManager.Instance.AttachPersistenceManager(new Assembly[] { Assembly.GetExecutingAssembly() });
Or in the constructor for Windows 8:
PersistenceManager.Instance.AttachPersistenceManager(new Assembly[] { typeof(App).GetTypeInfo().Assembly });
3. Everytime a View Model is instantiated, it must be registered:
_main = new MainViewModel();
// Register View Model with PersistenceManager
PersistenceManager.Instance.Register("_main", _main);
4. Data loaded from an external source like a service should be called from the OnInitialised method if it is not persisted from permanent storage:
public override void OnLaunched()
{
// Only get data on launch
if(_data == null)
this._dataService.GetData(context, this.CallbackDispatcher);
}
5. Mark any properties required for persistance with the Persist attribute:
[Persist(PersistMode.Both)]
public Person Person
{
get { return this._person; }
set
{
if (this._person != value)
{
this._person = value;
base.RaisePropertyChanged("Person");
}
}
}
6. Check any collections or lists have an alternate simple array property so that it can easily be serialised and deserialised.
http://geoffwebbercross.blogspot.com/