Windows 应用 SDK桌面应用的应用激活

当系统或其他应用启动应用时,它将 激活 它。 根据应用的启动方式,激活类型会有所不同。 本文介绍如何在Windows 应用 SDK桌面应用中处理不同的激活方案。

激活概述

Windows 应用 SDK 通过 Microsoft.Windows.AppLifecycle 命名空间提供了丰富的激活模型。 使用 AppInstance.GetActivatedEventArgs 随时检索激活详细信息。

常见的激活类型包括:

激活类型 情景
Launch 用户选择应用磁贴或快捷方式
File 用户打开与应用关联的文件
Protocol 已向应用注册的 URI 方案被调用
StartupTask 应用配置为在用户登录时启动
PushNotification 应用由 AppNotification (通过 AppNotificationManager) 激活

Important

Windows 应用 SDK 桌面应用通过 Microsoft.Windows.AppLifecycle API 实现丰富激活。 此方法 Microsoft.UI.Xaml.Application.OnLaunched 仍针对基于 XAML 的应用调用,但它不提供所有激活类型。 查看 AppInstance.GetActivatedEventArgs() 以了解完整的激活上下文。

处理启动激活

最常见的激活是标准启动。 在您的App.xaml.cs中替换OnLaunched

using Microsoft.UI.Xaml;

public partial class App : Application
{
    private Window? m_window;

    protected override void OnLaunched(LaunchActivatedEventArgs args)
    {
        m_window = new MainWindow();
        m_window.Activate();
    }
}

// MainWindow is the window class generated by the WinUI 3 project
// template (MainWindow.xaml / MainWindow.xaml.cs).
public partial class MainWindow : Window
{
}

处理丰富的激活类型

若要响应文件、协议或其他激活类型,请尽早在应用启动中检查 AppInstance.GetActivatedEventArgs() 。 一种典型的方法是在 OnLaunched 或你的 Main 方法中进行检查:

using Microsoft.UI.Xaml;
using Microsoft.Windows.AppLifecycle;

public partial class App : Application
{
    private Window? m_window;

    protected override void OnLaunched(LaunchActivatedEventArgs args)
    {
        var activatedArgs = AppInstance.GetCurrent().GetActivatedEventArgs();

        m_window = new MainWindow();

        switch (activatedArgs.Kind)
        {
            case ExtendedActivationKind.File:
                var fileArgs = activatedArgs.Data as
                    Windows.ApplicationModel.Activation.IFileActivatedEventArgs;
                if (fileArgs != null)
                {
                    // Navigate to a page that handles the file
                    // fileArgs.Files contains the list of activated files
                }
                break;

            case ExtendedActivationKind.Protocol:
                var protocolArgs = activatedArgs.Data as
                    Windows.ApplicationModel.Activation.IProtocolActivatedEventArgs;
                if (protocolArgs != null)
                {
                    // Navigate based on the URI
                    // protocolArgs.Uri contains the activation URI
                }
                break;

            default:
                // Standard launch — navigate to home page
                break;
        }

        m_window.Activate();
    }
}

// MainWindow is the window class generated by the WinUI 3 project
// template (MainWindow.xaml / MainWindow.xaml.cs).
public partial class MainWindow : Window
{
}

注册激活类型

注册文件激活或协议激活的方式取决于应用是已打包 (MSIX) 还是未打包。

打包的应用

在应用清单中注册关联:

文件类型关联 (in Package.appxmanifest):

<Extensions>
  <uap:Extension Category="windows.fileTypeAssociation">
    <uap:FileTypeAssociation Name="myfiletype">
      <uap:SupportedFileTypes>
        <uap:FileType>.myext</uap:FileType>
      </uap:SupportedFileTypes>
    </uap:FileTypeAssociation>
  </uap:Extension>
</Extensions>

协议关联

<Extensions>
  <uap:Extension Category="windows.protocol">
    <uap:Protocol Name="myapp" />
  </uap:Extension>
</Extensions>

有关启动时激活,请参阅 将应用配置为登录时启动

未打包的应用

未打包的应用(例如,使用Windows 应用 SDK的普通WPF或 Win32 应用)没有清单,因此在启动时使用静态方法Microsoft.Windows.AppLifecycle.ActivationRegistrationManager在代码中注册相同的激活类型。 注册按用户进行,并在每次启动时都保留,因此每次启动应用时都可以安全地调用此注册。

using Microsoft.Windows.AppLifecycle;

// Register a protocol (URI scheme)
ActivationRegistrationManager.RegisterForProtocolActivation(
    "myapp",            // scheme
    string.Empty,       // logo (optional)
    "My App",           // display name
    string.Empty);      // exePath — empty registers the current executable

// Register a file type
ActivationRegistrationManager.RegisterForFileTypeActivation(
    new[] { ".myext" },                 // supported file extensions
    string.Empty,                       // logo (optional)
    "My App",                           // display name
    new[] { "open" },                   // supported verbs
    string.Empty);                      // exePath — empty registers the current executable

若要删除注册(例如卸载期间),请调用匹配 UnregisterForProtocolActivationUnregisterForFileTypeActivation 方法。

打包应用和未打包应用都使用 AppInstance.GetCurrent().GetActivatedEventArgs() 以相同方式获取激活参数,如本文前面所示。 有关详细信息,请参阅 通过应用生命周期 API 实现丰富激活功能

在正在运行的实例中处理激活

如果应用已在第二次激活时运行,则可以使用多实例 API 将其重定向到现有实例。 有关注册密钥和重定向激活的详细信息,请参阅 多实例应用

与 UWP 激活的差异

功能 UWP Windows 应用 SDK(桌面版)
激活入口 OnActivatedOnFileActivatedOnLaunched 覆盖项 AppInstance.GetActivatedEventArgs() + OnLaunched
以前的执行状态 LaunchActivatedEventArgs.PreviousExecutionState 系统未提供——请自行实现
预启动支持 是(选择加入)
命名空间 Windows.ApplicationModel.Activation Microsoft.Windows.AppLifecycle (包含 Windows.ApplicationModel.Activation 数据类型)