简体   繁体   English

预发布如何运作?

[英]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.寻找一些可以让我的应用程序启动更快的功能,我在Microsoft 文档中找到了可以帮助更快地打开应用程序的预启动功能。 But even if I register the app to enable prelaunch, the OnLaunched event keeps getting false in e.PrelaunchActivated .但是,即使我注册了应用程序以启用预启动, OnLaunched事件在e.PrelaunchActivated中仍然保持错误。 (I only could test this feature using the VS option 'Debug UWP Prelaunch' in debug mode). (我只能在调试模式下使用 VS 选项“Debug UWP Prelaunch”测试此功能)。

Do I need a Microsoft certificate to use it?我需要 Microsoft 证书才能使用它吗?
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.在我的情况下,Windows 在 Visual Studio 部署应用程序时不使用 Prelaunch,但在从应用商店安装它后,它按预期工作。 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.它是否有效,取决于可用 memory 的数量,但我发现它甚至可以在具有 4Gb 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.如您所见,我在应用程序预启动时添加了一个else语句来完成这项工作。

When testing via the Debug Universal Windows App Prelaunch mode in Visual Studio, the whole process should be like:在 Visual Studio 中通过Debug Universal Windows App Prelaunch模式进行测试时,整个过程应该是这样的:

  1. Choose the Debug Universal Windows App Prelaunch mode, then the app will be prelaunched.选择Debug Universal Windows App Prelaunch模式,则应用程序将被预启动。

  2. The OnLaunched event will be fired and you will go into the else sentence because it is prelaunched. OnLaunched事件将被触发,您将 go 放入else 语句,因为它是预先启动的。

  3. You could do your work in the OnLaunched event for prelaunched您可以在OnLaunched事件中为 prelaunched 完成工作

  4. Then the app will go to suspend status.然后应用程序将 go 进入暂停状态。

  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. OnLaunched事件将再次被触发,这一次它将显示e.PrelaunchActivated == false因为它是由用户启动的,它不是预先启动的。 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 .这里也提到了这一点: 应用程序启动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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