Reputation: 325
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?
Upvotes: 0
Views: 602
Reputation: 31841
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. 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. If you must know it was suspended and is resuming, then you can tap into the OnResuming override in your app's Application/Bootstrapper. 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. 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. It will also call your view-model's INav overrides like NavTo and NavFrom.
But beware. The Bootstrapper's OnResuming will also be called during this operation. 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.
Upvotes: 1
Reputation: 5922
I'm assuming you are using Simulation Dashboard controls in Visual Studio to suspend and then resume your app. 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). The OnNavigatedToAsync method should then fire just fine.
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:
public static TypeOfMyViewModel MyViewModel;
Then, in the TypeOfMyViewModel
constructor, add the following line to set value to the property:
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:
public override void OnResuming(object s, object e, AppExecutionState previousExecutionState)
{
MyViewModel.ResumeServices();
}
Upvotes: 1