Search code examples
xamarin.formsuwpurl-schemeuwp-navigation

Launching from UWP URI Protocol Scheme is Not Navigating


I have followed the instructions as found in the documentation; however, when I launch my app using the specified protocol my-protocol:// (typed into a web browser), the app will launch but then it just stays on the splash screen, as if the navigation fails to do anything:

Code Example:

// MyApp.UWP/App.xaml.cs
protected override void OnActivated(IActivatedEventArgs args)
  {
      if (args.Kind == ActivationKind.Protocol)
      {
         ProtocolActivatedEventArgs eventArgs = 
             args as ProtocolActivatedEventArgs;

         // TODO: Decide where to navigate, but for now just go to main page

         Frame rootFrame = Window.Current.Content as Frame;
         rootFrame.Navigate(typeof(MainPage), args);
      }
   }

Is there something obvious that I am doing wrong? Perhaps there is a better way to handle navigation? Or perhaps there is something that I overlooked?

Edit

This is particularly hard to troubleshoot, since I can't just run with debug in visual studio. To test it out I actually have to launch it from the my-protocol://, which is not connected to the debugger.

Is there a way to debug this when launched from the url / protocol?


Solution

  • I could reproduce your issue. @kennyzx's suggestion was correct. You would first need to do judgement before navigating.

    Please refer to the following code sample for reference.

    protected override void OnActivated(IActivatedEventArgs args)
    {
            base.OnActivated(args);
            if (args.Kind == ActivationKind.Protocol)
            {
                ProtocolActivatedEventArgs eventArgs =
                    args as ProtocolActivatedEventArgs;
    
                // TODO: Decide where to navigate, but for now just go to main page
    
                Frame rootFrame = Window.Current.Content as Frame;
                if (rootFrame == null)
                {
                    rootFrame = new Frame();
                }
                Window.Current.Content = rootFrame;
                rootFrame.Navigate(typeof(MainPage), args);
                Window.Current.Activate();
            }
    }