Event on returning to app, after exiting by Window

2019-06-11 07:48发布

问题:

I have big trouble finding right event for me - user during browsing files in my app [UWP Windows 10 Mobile app] can tap on it and then I launch it in default app by

Windows.System.Launcher.LaunchFileAsync

my app is 'minimzed' (just like by pressing Windows key) and user can interact with file in whatever app he wants. Now by pressing back-key he is returning to my app. Do you know any event which is trigerred now? I want to update the file (if it was changed) but I cannot find any event to check it.

回答1:

Take a look at App lifecycle, I think you should register event handler for both OnLaunched and Resuming, one for switching back from terminated state, another for from suspended state.

When the user switches back to a suspended app that has been terminated, the app should restore its application data in its OnLaunched method.

If an app registered an event handler for the Application.Resuming event, it is called when the app is resumed from the Suspended state. You can refresh your app content and data using this event handler.



回答2:

It is important to understand the UWP app lifecycle. If you want to have control on what is happening while application is Launched first time, Suspended or Resumed you should refer to below guideline:

https://msdn.microsoft.com/en-us/windows/uwp/launch-resume/app-lifecycle

Also in the App.xaml.cs file you can manage this cycle. For instance you can control what to do when application is Resumed from the background:

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

    private void App_Resuming(object sender, object e)
    {
       //Code to execute while resuming the app...
    }