繁体   English   中英

预发布如何运作?

[英]How prelaunch works?

寻找一些可以让我的应用程序启动更快的功能,我在Microsoft 文档中找到了可以帮助更快地打开应用程序的预启动功能。 但是,即使我注册了应用程序以启用预启动, OnLaunched事件在e.PrelaunchActivated中仍然保持错误。 (我只能在调试模式下使用 VS 选项“Debug UWP Prelaunch”测试此功能)。

我需要 Microsoft 证书才能使用它吗?
操作系统需要多长时间才能了解我的应用程序有资格预启动?
这会影响我在活动中变得虚假的事实吗?

在我的情况下,Windows 在 Visual Studio 部署应用程序时不使用 Prelaunch,但在从应用商店安装它后,它按预期工作。 用户必须至少运行一次应用程序,下次您将在任务管理器中看到它。 它是否有效,取决于可用 memory 的数量,但我发现它甚至可以在具有 4Gb RAM 的设备上工作。

我做了一个测试,预启动工作正常。 我将在这里发布我的代码并解释预启动的工作原理。 希望它能帮助你解决你的问题。

这是我使用的示例代码:

  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();
        }
    }

如您所见,我在应用程序预启动时添加了一个else语句来完成这项工作。

在 Visual Studio 中通过Debug Universal Windows App Prelaunch模式进行测试时,整个过程应该是这样的:

  1. 选择Debug Universal Windows App Prelaunch模式,则应用程序将被预启动。

  2. OnLaunched事件将被触发,您将 go 放入else 语句,因为它是预先启动的。

  3. 您可以在OnLaunched事件中为 prelaunched 完成工作

  4. 然后应用程序将 go 进入暂停状态。

  5. 现在,用户从开始菜单启动应用程序。

  6. OnLaunched事件将再次被触发,这一次它将显示e.PrelaunchActivated == false因为它是由用户启动的,它不是预先启动的。 我怀疑这是你得到的行为。

这就是预发布工作的整个过程。 这里也提到了这一点: 应用程序启动

暂无
暂无

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

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