简体   繁体   English

Template10在应用暂停后恢复运行服务

[英]Template10 Resume running services after app suspension

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. 我正在开发一个利用Microsoft Band的UWP template 10(Hamburger)应用程序,我想我已经完成了很多工作,因为我将所有服务都绑定到Band(通过一个类库项目),并且值在屏幕完美。 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. 所以我在MainPageViewModel OnNavigatedFromAsync方法中添加了代码,以停止所有服务没有问题。 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. 但是后来我发现,在恢复OnNavigatedToAsync方法时不会被触发,因此我无法解决如何重新启动所有服务的问题。

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. 我还尝试在app.xaml.cs中添加OnResuming和OnSuspendingAsync的替代项,但是后来无法弄清楚如何从那里调用MainPageViewModel中的方法。 Is there a proper way to handle things like this using Template10? 是否有使用Template10处理此类事情的正确方法?

I'm assuming you are using Simulation Dashboard controls in Visual Studio to suspend and then resume your app. 我假设您正在Visual Studio中使用Simulation Dashboard控件来挂起然后恢复您的应用程序。 Well, then don't use the resume button. 好吧,那就不要使用恢复按钮。 It doesn't work as expected. 它没有按预期工作。 To test resuming of your app, use the suspend button and then open it from the Band's interface (instead of using the resume button). 要测试您的应用是否恢复,请使用“暂停”按钮,然后从Band的界面中打开它(而不是使用“恢复”按钮)。 The OnNavigatedToAsync method should then fire just fine. 然后,OnNavigatedToAsync方法应该可以正常启动。

Update : Based on the discussion following this answer, I am providing an updated answer on the following lines. 更新 :基于此答案的讨论,我在以下几行提供更新的答案。

Set up a static viewmodel property in the App class, like this: App类中设置一个静态viewmodel属性,如下所示:

public static TypeOfMyViewModel MyViewModel;

Then, in the TypeOfMyViewModel constructor, add the following line to set value to the property: 然后,在TypeOfMyViewModel构造函数中,添加以下行以将值设置为属性:

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

And, finally, in the OnResuming method just call a method from the ViewModel which will resume your services, like this: 最后,在OnResuming方法中,只需从ViewModel调用一个方法即可恢复服务,如下所示:

public override void OnResuming(object s, object e, AppExecutionState previousExecutionState)
{
    MyViewModel.ResumeServices();
}

When you Suspend and Resume your app either through the Windows platform or through Visual Studio, the Resume operation is super-fast because it's just a memory swap. 通过Windows平台或Visual Studio挂起和恢复应用程序时,“恢复”操作非常快,因为它只是内存交换。 Your application does not generally even know your application was suspended in this case. 您的应用程序通常甚至不知道您的应用程序在这种情况下被暂停。 AN example of this operation might be that your user gets a phone call and then returns to your app. 此操作的一个示例可能是您的用户接到电话,然后返回到您的应用程序。 With Template 10, your view-model INav methods are certainly not called because your app's state has not changed. 使用模板10,肯定不会调用视图模型的INav方法,因为您的应用程序的状态没有更改。 If you must know it was suspended and is resuming, then you can tap into the OnResuming override in your app's Application/Bootstrapper. 如果您必须知道它已被暂停并正在恢复,则可以在应用程序的Application / Bootstrapper中使用OnResuming替代。 Depending on what you need to accomplish, you might need to expose this operation through a global static event so your view-models can handle it somehow. 根据您需要完成的工作,您可能需要通过全局静态事件公开此操作,以便您的视图模型可以以某种方式处理它。

In Visual Studio using Debug Location, you can then Suspend and Shutdown which will also suspend your app, but will change it's resume state from PreviousExecutionState=Running to Terminated. 然后在Visual Studio中使用“调试位置”,您可以Suspend and Shutdown ,这也将挂起您的应用程序,但是会将其恢复状态从PreviousExecutionState = Running更改为Terminated。 In this case, your app is certainly not still in memory and certainly not still in the same state. 在这种情况下,您的应用程序肯定不在内存中,并且肯定不会处于同一状态。 It is getting re-started, and Template 10 kicks in during this transition to restore your navigation state, settings, and everything else. 它正在重新启动,并且在过渡期间将启动Template 10,以恢复导航状态,设置以及所有其他内容。 It will also call your view-model's INav overrides like NavTo and NavFrom. 它还将调用视图模型的INav替代,例如NavTo和NavFrom。

But beware. 但是要当心。 The Bootstrapper's OnResuming will also be called during this operation. 在此操作过程中还将调用引导程序的OnResuming。 Fortunately for you, the previous state is passed to this override and you can handle this unique case seamlessly in your calling code. 对您来说幸运的是,先前的状态将传递给此替代,您可以在调用代码中无缝处理此唯一的情况。

Make sense? 说得通?

Best of luck. 祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM