问题
I have a WPF
application where I want to handle file activation. I found solutions where adding specific values to the registry solves the issue. The problem is that the final app should be a UWP
app and I'm using the desktop bridge to do this. If the app is running as UWP
, it can't reach the registry to set these specific values. Is there any other way to handle file activation without using the registry?
I also tried to create a UWP
project, because in UWP is very easy to handle file activation and to launch somehow my WPF
application from this project and pass the content of the file.
I tried the Launcher.LaunchUriAsync(...)
, but I hadn't really find an example how to build the URI. If this way is viable can you provide me an example?
Then I also tried to communicate by Windows.ApplicationModel.AppService
but it's also needed to start the app if it's not running. So this isn't a good way.
I'm open for any other approaches, too.
回答1:
You can implement own custom Main
method and check the values of the command-line arguments that get passed to it when you call the Launcher.LaunchUriAsync
API.
If you package your WPF application and target Windows 10 version 1809 (build 17763) or later, you can use the AppInstance.GetActivatedEventArgs
method to get the actual IActivatedEventArgs
that is passed in to the OnActivated
method of a UWP app:
[STAThread]
static void Main()
{
App application = new App();
application.InitializeComponent();
application.OnActivated(Windows.ApplicationModel.AppInstance.GetActivatedEventArgs());
application.Run();
}
You can then handle it in your WPF application:
public partial class App : Application
{
public void OnProtocolActivated(IActivatedEventArgs args)
{
switch (args.Kind)
{
case ActivationKind.File:
//handle file activation
break;
}
}
}
Please refer to this blog post and the accompanying code sample for more details.
来源:https://stackoverflow.com/questions/57755792/how-can-i-handle-file-activation-from-a-wpf-app-which-is-running-as-uwp