0

I am developing a UWP template 10 (Hamburger) app which makes use of the Microsoft Band, I thought I had pretty much finished as I have all my services tied up to the Band (through a class library project) and the values are updating on screen perfectly. I then started testing app resume and came across a problem. When I restart an app the user is taken back to the values page but the values no longer update. Basically the connection to the band seems to still be valid but the readingchanged voids are no longer working.

So I added code in the MainPageViewModel OnNavigatedFromAsync method to stop all of the services no problem. But then I found that when resuming the OnNavigatedToAsync method does not get fired so I can't work out how to restart all the services.

I also tried adding the overrides for OnResuming and OnSuspendingAsync in app.xaml.cs but then can't work out how to call methods in the MainPageViewModel from there. Is there a proper way to handle things like this using Template10?

4

2 回答 2

1

当您通过 Windows 平台或通过 Visual Studio 暂停和恢复您的应用程序时,恢复操作非常快,因为它只是一个内存交换。在这种情况下,您的应用程序通常甚至不知道您的应用程序已暂停。此操作的一个示例可能是您的用户接到电话,然后返回到您的应用程序。使用模板 10,您的视图模型 INav 方法肯定不会被调用,因为您的应用程序的状态没有改变。如果您必须知道它已暂停并正在恢复,那么您可以在应用程序的应用程序/引导程序中利用 OnResuming 覆盖。根据您需要完成的工作,您可能需要通过全局静态事件公开此操作,以便您的视图模型可以以某种方式处理它。

在使用调试位置的 Visual Studio 中,您可以然后Suspend and Shutdown暂停您的应用程序,但会将其恢复状态从 PreviousExecutionState=Running 更改为 Terminated。在这种情况下,您的应用程序肯定不会仍在内存中,也肯定不会仍处于相同状态。它正在重新启动,并且模板 10 在此过渡期间启动,以恢复您的导航状态、设置和其他所有内容。它还将调用您的视图模型的 INav 覆盖,例如 NavTo 和 NavFrom。

但要小心。Bootstrapper 的 OnResuming 也会在此操作期间被调用。对您来说幸运的是,先前的状态被传递给此覆盖,您可以在调用代码中无缝处理这种独特情况。

说得通?

祝你好运。

于 2016-03-06T21:39:04.117 回答
1

我假设您正在使用 Visual Studio 中的 Simulation Dashboard 控件来暂停然后恢复您的应用程序。好吧,那就不要使用恢复按钮。它没有按预期工作。要测试您的应用程序的恢复,请使用暂停按钮,然后从乐队的界面打开它(而不是使用恢复按钮)。然后 OnNavigatedToAsync 方法应该可以正常触发。

更新:基于此答案之后的讨论,我将在以下几行中提供更新的答案。

在类中设置静态视图模型属性App,如下所示:

public static TypeOfMyViewModel MyViewModel;

然后,在TypeOfMyViewModel构造函数中,添加以下行来设置属性的值:

public TypeOfMyViewModel()
{
    App.MyViewModel = this;
}

最后,在该OnResuming方法中,只需从 ViewModel 调用一个方法,该方法将恢复您的服务,如下所示:

public override void OnResuming(object s, object e, AppExecutionState previousExecutionState)
{
    MyViewModel.ResumeServices();
}
于 2016-03-06T13:40:26.677 回答