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)還是未打包。

已封裝的應用程式

在應用程式資訊清單中註冊這些關聯:

檔案類型關聯(在 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 啟動機制的差異

Feature UWP Windows 應用程式 SDK(桌面版)
啟動入口點 OnActivatedOnFileActivatedOnLaunched 覆寫 AppInstance.GetActivatedEventArgs() + OnLaunched
先前執行狀態 LaunchActivatedEventArgs.PreviousExecutionState 系統未提供——請自行實作
啟動前支援 是(選擇加入) No
命名空間 Windows.ApplicationModel.Activation Microsoft.Windows.AppLifecycle (含 Windows.ApplicationModel.Activation 資料型別)