Handling suspend, resume, and activation in windows 10 UWP

前端 未结 2 1933
终归单人心
终归单人心 2021-02-15 17:30

In the windows 8.1 universal apps, the suspend/resume modes were handled using the NavigationHelper.cs ans SuspensionManager.cs classes included in the

2条回答
  •  再見小時候
    2021-02-15 17:49

    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.

提交回复
热议问题