Handling Suspend, Resume, and Activate in Windows 10 UWP

In generic Windows 8.1 applications, suspend / resume modes were handled using the NavigationHelper.cs ans SuspensionManager.cs classes included in the APP template. These classes do not seem to be present in Windows 10 UWP applications. Is there a way we can handle suspend / resume states?

+8
c # windows-10 windows-store-apps
source share
2 answers

There is an interesting structure developed by the community (but mostly I think Jerry Nixon, Andy Wigley, etc.) called Template10. Template10 has a Bootstrapper class with OnSuspending and OnResuming virtual methods that you can override. I'm not sure there is an exact example of pausing / resuming work with Template10, but the idea is to make App.xaml.cs inherit from this Bootstrapper so that you can easily override the methods I talked about.

 sealed partial class App : Common.BootStrapper { public App() { InitializeComponent(); this.SplashFactory = (e) => null; } public override Task OnStartAsync(StartKind startKind, IActivatedEventArgs args) { // start the user experience NavigationService.Navigate(typeof(Views.MainPage), "123"); return Task.FromResult<object>(null); } public override Task OnSuspendingAsync(object s, SuspendingEventArgs e) { // handle suspending } public override void OnResuming(object s, object e) { // handle resuming } } 
+4
source share

The above solution will only work for people who install Template10. Common decision -

paste these lines into the constructor of App.xaml.cs

  this.LeavingBackground += App_LeavingBackground; this.Resuming += App_Resuming; 

It will look like

  public App() { this.InitializeComponent(); this.Suspending += OnSuspending; this.LeavingBackground += App_LeavingBackground; this.Resuming += App_Resuming; } 

These are methods, although you can press TAB and they will be automatically generated.

  private void App_LeavingBackground(object sender, LeavingBackgroundEventArgs e) { } private void App_Resuming(object sender, object e) { } 

LeavingBackground methods and one that is not mentioned here, EnteredBackground are again added to uwp.

Before these methods, we will use resume and pause saving and restoring ui, but now the recommended place for this work is here. These are also the last places to do work before resuming the application. Thus, the work on these methods should be small ui or other things, such as altering values ​​that are deprecated, since a long held method here will affect the application’s startup time when resuming.

Source Windows dev material , Windoes dev material 2

Thank you, and we have a good day.

+2
source share

All Articles