简体   繁体   中英

How prelaunch works?

Looking for some feature that can make my app start up even fast, I found the prelaunch feature in the Microsoft documentation that would help open the app faster. But even if I register the app to enable prelaunch, the OnLaunched event keeps getting false in e.PrelaunchActivated . (I only could test this feature using the VS option 'Debug UWP Prelaunch' in debug mode).

Do I need a Microsoft certificate to use it?
How long does the OS take to understand that my app is eligible to prelaunch?
Does this influence the fact that I'm getting false at the event?

In my case, Windows does not use Prelaunch when app is deployed by Visual Studio, but after installing it from the Store it works as expected. User must run an app at least one time and next time you will see it in the Task Manager. Will it work or not, depends on the amount of available memory, but I found it working even on a device with 4Gb of RAM.

I made a test and the prelaunch works correctly. I'll post my code here and explain how the prelaunch works. Hope it will help you solve your question.

Here is the sample code that I used:

  protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
        bool canEnablePrelaunch = Windows.Foundation.Metadata.ApiInformation.IsMethodPresent("Windows.ApplicationModel.Core.CoreApplication", "EnablePrelaunch");

        // ********
        // some other code here
        // *********

        //check if it is prelaunched
        if (e.PrelaunchActivated == false)
        {
            // On Windows 10 version 1607 or later, this code signals that this app wants to participate in prelaunch
            if (canEnablePrelaunch)
            {
                TryEnablePrelaunch();
            }
            // ******
            // normal code
            // ******
        }
        else
        {
            //prelaunched
            // do some logic that don't require UI thread.
            if (rootFrame.Content == null)
            {
                // When the navigation stack isn't restored navigate to the first page,
                // configuring the new page by passing required information as a navigation
                // parameter
                rootFrame.Navigate(typeof(MainPage), "PreLaunched");
            }
            // Ensure the current window is active
            Window.Current.Activate();
        }
    }

As you could see, I add an else sentence to do the work when the app is prelaunched.

When testing via the Debug Universal Windows App Prelaunch mode in Visual Studio, the whole process should be like:

  1. Choose the Debug Universal Windows App Prelaunch mode, then the app will be prelaunched.

  2. The OnLaunched event will be fired and you will go into the else sentence because it is prelaunched.

  3. You could do your work in the OnLaunched event for prelaunched

  4. Then the app will go to suspend status.

  5. Now, the user launches the app from Start Menu.

  6. The OnLaunched event will be fired again and this time it will show e.PrelaunchActivated == false because it is launched by the user , it is not prelaunched. I suspect this is the behavior that you are getting.

So this is the whole process about how the prelaunch works. This is also mentioned here: App launch .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM