Notitie
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen u aan te melden of de directory te wijzigen.
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen de mappen te wijzigen.
Met protocolactivering (ook wel deep linking of URI-activering genoemd) kan een andere app, een browser of de opdrachtregel uw app starten door naar een URI te navigeren, zoals myapp://action?param=value.
Dit artikel bevat code specifiek voor een WPF-app. Zie voor volledige richtlijnen het hoofdartikel Handle URI-activering. Zie Rich activation with the app lifecycle API voor meer informatie over uitgebreide activering met de Windows App SDK.
Registreren voor protocolactivering
U moet uw app registreren om protocolactivering af te handelen. Voor een uitgepakte app registreert u zich in code. Voor een verpakte app registreert u zich in het app-manifest.
Uitgepakte app
Voor een uitgepakte .NET-app (de standaardinstelling WPF/WinForms) registreert u uw protocol bij het opstarten met ActivationRegistrationManager. Registraties zijn per gebruiker en blijven behouden, dus het is veilig om dit bij elke lancering aan te roepen.
In App.xaml.cs, overschrijven van 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);
}
Als u de registratie wilt opschonen (bijvoorbeeld in een verwijderingsstap), roept u ActivationRegistrationManager.UnregisterForProtocolActivation("myapp", "") aan.
Verpakte app
Declareer voor een verpakte .NET-app het protocol in Package.appxmanifest onder het element <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>
Zorg ervoor dat de uap XML-naamruimte is gedeclareerd op het Package element: xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10".
De activering afhandelen
Haal de activeringsargumenten op met AppInstance.GetCurrent().GetActivatedEventArgs. Het volgende voorbeeld bevat code voor een uitgepakte WPF-app, die RegisterForProtocolActivation aanroept bij het opstarten. Verpakte apps ontvangen activering via de manifestregistratie, zodat ze de RegisterForProtocolActivation oproep kunnen overslaan.
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.
}
Opmerking
WPF- en Windows Forms-apps moet worden aangeroepen AppInstance.GetCurrent().GetActivatedEventArgs() om URI-activeringsgegevens op te halen. In tegenstelling tot C++ Win32-apps, ontvangen .NET apps geen activeringsargumenten via een opstartinvoerpuntparameter.
Single-instantieomleiding verwerken
Als uw app slechts één exemplaar tegelijk mag uitvoeren, gebruikt u AppInstance.FindOrRegisterForKey om volgende URI-starts om te leiden naar het lopende exemplaar.
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();
});
}
Zie App-instancing met de levenscyclus-API van de app voor meer informatie over app-instancing.
Verwante inhoud
Windows developer