我实现了一个简单的模式-
-
在应用程序的激活和停用事件中,我向订阅页发送消息。
-
订阅消息的页面执行数据的序列化/反序列化。
我在用
Laurent Bugnion's excellent MVVMLight library for Windows Phone 7
. 下面是一些演示消息广播的示例代码-
// Ensure that application state is restored appropriately
private void Application_Activated(object sender, ActivatedEventArgs e)
{
Messenger.Default.Send(new NotificationMessage<AppEvent>(AppEvent.Activated, string.Empty));
}
// Ensure that required application state is persisted here.
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
Messenger.Default.Send(new NotificationMessage<AppEvent>(AppEvent.Deactivated, string.Empty));
}
在ViewModel类的构造函数中,我设置了对通知消息的订阅-
// Register for application event notifications
Messenger.Default.Register<NotificationMessage<AppEvent>>(this, n =>
{
switch (n.Content)
{
case AppEvent.Deactivated:
// Save state here
break;
case AppEvent.Activate:
// Restore state here
break;
}
}
我发现使用这个策略,所有与绑定到ViewModel的页面相关的数据都会被正确地保存和恢复。
HTH,印度弗洛莫兹