Handling suspend, resume, and activation in windows 10 UWP

Deadly 提交于 2019-12-04 20:08:47

问题


In the windows 8.1 universal apps, the suspend/resume modes were handled using the NavigationHelper.cs ans SuspensionManager.cs classes included in the APP template. These classes doesn't seem to be there in the windows 10 UWP apps. Is there a way by which we can handle the suspend/resume states?


回答1:


There's an interesting framework being 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 am not sure that there's an exact example of doing suspension/resuming yet with Template10, but the idea seems to be to make App.xaml.cs inherit from this Bootstrapper class so you can easily override the methods I mentioned.

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



回答2:


The above solution will only work for people who install Template10. The generic solution is,

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

        this.LeavingBackground += App_LeavingBackground;

        this.Resuming += App_Resuming;

It will look like this

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

        this.Resuming += App_Resuming;
    }

These are the methods, although you can press TAB and they will autogenerate.

    private void App_LeavingBackground(object sender, LeavingBackgroundEventArgs e)
    {

    }

    private void App_Resuming(object sender, object e)
    {

    }

The methods LeavingBackground and the one not mentioned here EnteredBackground are newly added to uwp.

Before these methods we would use resuming and suspending to save and restore ui, but now the recommended place to do that work is here.Also these are the last places to perform work before the app is resumed. So the work on these methods should be small ui or other stuff like remaking values which are stale as a long held method here will affect app startup time while resuming.

Source Windows dev material , Windoes dev material 2

Thanks , and have a good day.



来源:https://stackoverflow.com/questions/31647827/handling-suspend-resume-and-activation-in-windows-10-uwp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!