2

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.

Attila Szász
  • 707
  • 4
  • 22
  • https://learn.microsoft.com/en-us/windows/uwp/launch-resume/handle-file-activation – pix Sep 02 '19 at 11:22
  • Yes, I've already could implement file activation in the uwp project, the question on this thread is to pass the content of the file to the other app – Attila Szász Sep 02 '19 at 11:44

1 Answers1

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.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • One thing is not clear for me: so, I have a WPF project, which will be packaged with desktop bridge, containing this `Main()` method. Then I have a UWP project which handles the file activation. I receive the activation overriding the `OnActivated()` method and from this method how can I launch the WPF project? I suppose I call the `Launcher.LaunchUriAsync`, but what kind of Uri should I pass as parameter? – Attila Szász Sep 02 '19 at 13:44
  • It's the WPF application that handles the event. Do you have two apps or where does the UWP app come in? If the packaged WPF app handles the URI, it can be activated using `Launcher.LaunchUriAsync` just like you would activate a UWP app. – mm8 Sep 02 '19 at 13:45
  • There are two projects in a solution. One is the app what I'm developing (WPF) and I should create a UWP project to be able to register the file activation. It'd very good if I could solve the issue without the second UWP project. As I understand from your answer this is possible... I just now observed that I missed that registering the file activation should be added to the packaging project, not to another project... – Attila Szász Sep 02 '19 at 13:54
  • There should be two projects. The WPF app to be packaged and a Windows Application Packaging Project that references the WPF app. No UWP app. – mm8 Sep 02 '19 at 13:55
  • I am getting an error on application.OnActivated(... - This is showing that OnActivated is protected. Any advice – Anthony Nichols Aug 25 '23 at 15:50