0

当重新激活墓碑应用程序时,我遵循恢复我的持久和非持久状态和对象的一般最佳实践原则。可以在这篇非常好的 Microsoft 文章中找到

这里

示例仅显示了应用程序主页的简单重新激活。但是,由于我的应用程序有多个页面(其中任何一个都可以被删除并因此重新激活),并且每个页面都绑定到不同的 ViewModel 对象。我想知道如何确定最终将激活哪个页面,以便我可以选择性地反序列化并恢复该页面的正确 ViewModel 对象。

或者是恢复所有 ViewModel 的最佳实践,还是有另一种设计模式?

4

1 回答 1

1

我已经实现了一个简单的模式,最好描述为 -

  1. 在应用程序的激活和停用事件中,我向订阅页面发送消息。
  2. 订阅消息的页面进行数据的序列化/反序列化。

我正在为 Windows Phone 7 使用 Laurent Bugnion 的优秀 MVVMLight 库。这是一些说明消息广播的示例代码 -

// 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,indyfromoz

于 2010-11-15T21:07:06.677 回答