システムまたは別のアプリがアプリを起動すると、アプリ がアクティブになります 。 アプリの起動方法に応じて、アクティブ化の種類が異なります。 この記事では、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
登録を削除するには (アンインストール中など)、一致する UnregisterForProtocolActivation または UnregisterForFileTypeActivation メソッドを呼び出します。
パッケージ化されたアプリとパッケージ化されていないアプリの両方で、この記事で前述したように、 AppInstance.GetCurrent().GetActivatedEventArgs()を使用して、アクティブ化引数を同じ方法で取得します。 詳細については、 アプリ ライフサイクル API を使用したリッチ アクティブ化に関する説明を参照してください。
実行中のインスタンスでのアクティブ化の処理
2 回目のアクティブ化が発生したときにアプリが既に実行されている場合は、マルチインスタンス API を使用して既存のインスタンスにリダイレクトできます。 キーの登録とアクティブ化のリダイレクトの詳細については、 マルチインスタンス アプリ を参照してください。
UWP アクティブ化との違い
| 特徴 | UWP | Windows アプリ SDK (デスクトップ) |
|---|---|---|
| アクティベーション エントリ ポイント |
OnActivated、OnFileActivated、OnLaunched オーバーライド |
AppInstance.GetActivatedEventArgs() + OnLaunched |
| 以前の実行状態 | LaunchActivatedEventArgs.PreviousExecutionState |
システムによって提供されない — 独自の実装 |
| 事前起動のサポート | はい (オプトイン) | いいえ |
| 名前空間 | Windows.ApplicationModel.Activation |
Microsoft.Windows.AppLifecycle ( Windows.ApplicationModel.Activation データ型を使用) |
関連するコンテンツ
Windows developer