プロトコルのアクティブ化 ( ディープ リンク または URI のアクティブ化とも呼ばれます) を使用すると、別のアプリ、ブラウザー、またはコマンド ラインで、 myapp://action?param=valueなどの URI に移動してアプリを起動できます。
この記事では、WPF アプリ専用のコードを示します。 完全なガイダンスについては、メインの ハンドル URI アクティブ化 に関する記事を参照してください。 Windows アプリ SDKを使用したリッチ アクティブ化の詳細については、「アプリ ライフサイクル API を使用したリッチ アクティブ化を参照してください。
プロトコルのアクティブ化に登録する
プロトコルのアクティブ化を処理するには、アプリを登録する必要があります。 パッケージ化されていないアプリの場合は、コードで登録します。 パッケージ アプリの場合は、アプリ マニフェストに登録します。
非パッケージ アプリ
パッケージ化されていない.NET アプリ (既定の WPF/WinForms セットアップ) の場合は、起動時に ActivationRegistrationManager を使用してプロトコルを登録します。 登録はユーザーごとに保持されるため、起動時に呼び出しても安全です。
App.xaml.csで、OnStartupをオーバーライドします。
using Microsoft.Windows.AppLifecycle;
protected override void OnStartup(StartupEventArgs e)
{
// Register the URI scheme "myapp://" for this app.
// For the logo, pass the exe path + resource index (or "" to use the default icon).
string exePath = System.Diagnostics.Process.GetCurrentProcess().MainModule?.FileName ?? "";
string logo = string.IsNullOrEmpty(exePath) ? "" : exePath + ",0";
ActivationRegistrationManager.RegisterForProtocolActivation(
"myapp", // URI scheme (no "://")
logo, // logo: exe path + resource index, or "" for default icon
"My App", // display name for the protocol
exePath); // path of this EXE; pass "" to default to the current process
base.OnStartup(e);
}
登録をクリーンアップするには (アンインストール手順など)、 ActivationRegistrationManager.UnregisterForProtocolActivation("myapp", "")を呼び出します。
パッケージ アプリ
パッケージ化された.NET アプリの場合は、Package.appxmanifest 要素の下の <Applications><Application> でプロトコルを宣言します。
<Applications>
<Application ...>
<Extensions>
<uap:Extension Category="windows.protocol">
<uap:Protocol Name="myapp">
<uap:DisplayName>My App</uap:DisplayName>
</uap:Protocol>
</uap:Extension>
</Extensions>
</Application>
</Applications>
uap XML 名前空間が Package 要素 (xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10") で宣言されていることを確認します。
アクティブ化を処理する
AppInstance.GetCurrent() を使用してアクティブ化引数を取得します。GetActivatedEventArgs。 次の例には、起動時に RegisterForProtocolActivation を呼び出す、パッケージ化されていないWPF アプリのコードが含まれています。 パッケージ アプリはマニフェスト登録を通じてアクティブ化を受け取るので、 RegisterForProtocolActivation 呼び出しをスキップできます。
using Microsoft.Windows.AppLifecycle;
using Windows.ApplicationModel.Activation;
protected override void OnStartup(StartupEventArgs e)
{
// Unpackaged apps only: register the protocol at startup.
// Packaged apps (MSIX): skip these lines — the manifest handles registration.
string exePath = System.Diagnostics.Process.GetCurrentProcess().MainModule?.FileName ?? "";
string logo = string.IsNullOrEmpty(exePath) ? "" : exePath + ",0";
ActivationRegistrationManager.RegisterForProtocolActivation(
"myapp", logo, "My App", exePath);
// Get the activation args for this specific launch.
AppActivationArguments args = AppInstance.GetCurrent().GetActivatedEventArgs();
if (args?.Kind == ExtendedActivationKind.Protocol)
{
var protocolArgs = (ProtocolActivatedEventArgs)args.Data;
HandleProtocolActivation(protocolArgs.Uri);
}
base.OnStartup(e);
}
private void HandleProtocolActivation(Uri uri)
{
// Navigate to or open content based on uri.AbsolutePath or uri.Query.
}
Note
WPFおよびWindows フォームのアプリは必要があるAppInstance.GetCurrent().GetActivatedEventArgs()を呼び出してURIアクティブ化データを取得しなければなりません。 C++ Win32 アプリとは異なり、.NET アプリはスタートアップ エントリ ポイント パラメーターを介してアクティブ化引数を受け取りません。
単一インスタンス のリダイレクトを処理する
アプリで一度に 1 つのインスタンスのみを実行する必要がある場合は、 AppInstance.FindOrRegisterForKey を使用して、後続の URI 起動を実行中のインスタンスにリダイレクトします。
protected override void OnStartup(StartupEventArgs e)
{
string exePath = System.Diagnostics.Process.GetCurrentProcess().MainModule?.FileName ?? "";
string logo = string.IsNullOrEmpty(exePath) ? "" : exePath + ",0";
ActivationRegistrationManager.RegisterForProtocolActivation(
"myapp", logo, "My App", exePath);
// Try to claim the "main" key. If another instance already has it, redirect and exit.
AppInstance currentInstance = AppInstance.FindOrRegisterForKey("main");
if (!currentInstance.IsCurrent)
{
var activationArgs = AppInstance.GetCurrent().GetActivatedEventArgs();
// Run the async redirect on a thread-pool thread to avoid a potential deadlock
// with the WPF SynchronizationContext. Signal completion via an event so that
// this code path exits cleanly without re-entering the STA message pump.
var redirectCompleted = new System.Threading.ManualResetEventSlim(false);
System.Threading.Tasks.Task.Run(async () =>
{
await currentInstance.RedirectActivationToAsync(activationArgs);
redirectCompleted.Set();
});
redirectCompleted.Wait();
Shutdown();
return;
}
// This is the first instance. Subscribe to future activations.
currentInstance.Activated += OnActivated;
base.OnStartup(e);
}
private void OnActivated(object sender, AppActivationArguments args)
{
Dispatcher.Invoke(() =>
{
if (args.Kind == ExtendedActivationKind.Protocol)
{
var protocolArgs = (ProtocolActivatedEventArgs)args.Data;
HandleProtocolActivation(protocolArgs.Uri);
}
MainWindow?.Activate();
});
}
アプリのインスタンス化の詳細については、「 アプリ ライフサイクル API を使用したアプリのインスタンス化」を参照してください。
関連するコンテンツ
Windows developer