Windows Phone 7:确定在 Application_Activated 事件期间激活哪个页面

发布于 2024-10-02 18:36:26 字数 393 浏览 0 评论 0原文

当重新激活逻辑删除的应用程序时,我遵循恢复持久和非持久状态和对象的一般最佳实践原则。 中找到

可以在这篇非常好的微软文章此处

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

或者是恢复所有 ViewModel 的最佳实践还是有其他设计模式?

I am following the general best practice principles of restoring my persistent and none persistent state and objects when a tombstoned app is re-activated. Which can be found in this very good Microsoft article

here

The samples only show a simple re-activation of the main page of an app. However as my application has multiple pages (any of which could be tombstoned and therfore re-activated) and each one is binding to a different ViewModel object. I would like to know how to ascertain which page is ultimately going to be activated so that I can selectivly deserialize and recover the correct ViewModel object for that page.

Or is the best practice to restore all ViewModels or is there another design pattern for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小情绪 2024-10-09 18:36:26

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

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

我正在使用 Laurent Bugnion 出色的适用于 Windows Phone 7 的 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

I have implemented a simple pattern that is best described as -

  1. In the application's Activated and Deactivated event, I send a message to subscribing pages.
  2. The pages that subscribe to the message do the serialization/deserialization of data.

I am using Laurent Bugnion's excellent MVVMLight library for Windows Phone 7. Here is some sample code illustrating the message broadcast -

// 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));
}

Within the constructor of a ViewModel class, I setup the subscription to the notification messages -

// 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;
   }
}

I found that with this strategy, all the data relevant to the page that is bound to a ViewModel is saved and restored properly.

HTH, indyfromoz

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文