Search code examples
c#wpfuwpwindows-10desktop-bridge

How to handle URI activation in a Windows Application Packaging Project?


I have a WPF application and I'm making a UWP app using the desktop bridge. Basically I created a new Windows App Packaging Project and I added to it my WPF project as a reference. I added a Protocol declaration in the app manifest with a custom URI, but I can't implement/override the OnActivated() method, because I have no classes where to do it and it isn't even possible to add classes to this type of project. So, any ideas how can I handle my custom URI calls in this environment?

I already create another UWP project, which just handles these requests and I also added this project to the main packaging project as a reference. Basically it's working, but if I create the app package and I install it (the appxbundle), then the URI handler won't be called at all.


Solution

  • There are two ways to do this:

    1) The protocol activation arguments are passed to your app's main() function. However, since you are using WPF you need to first understand how to get to your main() function. This is explained in this answer:
    No Main() in WPF?

    2) A better approach is available with the current Windows SDK (1809 and later): You can call this new method to the get activation arguments for your packaged WPF app (or any packaged Win32 app):
    https://learn.microsoft.com/en-us/uwp/api/windows.applicationmodel.appinstance.getactivatedeventargs#Windows_ApplicationModel_AppInstance_GetActivatedEventArgs