How to open WinUI Maui through URI Activation?

Viewed 254

I've created a Maui windows application. I'm looking to activate the application through a URI and pass query parameters to the app.

I've added the windows protocol for calling the app via uri in the package manifest:

  <Extensions>
            <uap:Extension Category="windows.protocol">
              <uap:Protocol Name="my-app">
                <uap:DisplayName>My App</uap:DisplayName>
              </uap:Protocol>
            </uap:Extension>
      </Extensions>

When I activate the application via the browser my-app://foo.com?user=123456 the app launches, but it launches as a cold start. Within my Win UI app I've overrode the onLaunched method, but regardless of how I've launched the app I cannot get access to the protocol. I'm trying to recreate the following code from my UWP Application:

protected override void OnActivated(IActivatedEventArgs args)
    {
        if (args.Kind == ActivationKind.Protocol)
        {

            ProtocolActivatedEventArgs eventArgs = args as ProtocolActivatedEventArgs;
            var queryStr = eventArgs.Uri.Query;
            App.UserId = System.Web.HttpUtility.ParseQueryString(queryStr).Get("user");

            // Navigate to a view
            Frame rootFrame = Window.Current.Content as Frame;
            if (rootFrame == null)
            {
                rootFrame = new Frame();
                Xamarin.Forms.Forms.Init(args);
                Window.Current.Content = rootFrame;
            }

            rootFrame.Navigate(typeof(MainPage), eventArgs);
        }

        Window.Current.Activate();
    }

So I've started with this, but UWPLaunchActivatedEventArg is consistently being returned as Launch instead of protocol.

    protected override void OnLaunched(LaunchActivatedEventArgs args)
{
  var kind =   args.UWPLaunchActivatedEventArgs.Kind;
        base.OnLaunched(e)
}
2 Answers

Here is my solution:

var activatedEventArgs = Microsoft.Windows.AppLifecycle.AppInstance.GetCurrent().GetActivatedEventArgs();

run your URI with parameters and here is the screenshot: enter image description here

It took me a while to understand what the other answer meant -- got to "cast" the object.

var actEventArgs = AppInstance.GetCurrent().GetActivatedEventArgs();
if ( actEventArgs.Kind == ExtendedActivationKind.Protocol) {
    var d = actEventArgs.Data as IProtocolActivatedEventArgs;
    if (d != null) {
        var uri = d.Uri;
        var uriString = uri.AbsoluteUri;
        // do something...
    }
}

This gets me the URI string. BUT -- I get a new window created. I want to dispatch the URI to an already created window (if it exists, otherwise create).

I've done with with Xamarin.Forms UWP, by catching the OnActivated event. Haven't got that figured for MAUI yet. Bob

Related