从 UWP URI 协议方案启动不导航

Launching from UWP URI Protocol Scheme is Not Navigating

我已按照找到的说明进行操作 in the documentation;然而,当我使用指定的协议 my-protocol://(在网络浏览器中输入)启动我的应用程序时,该应用程序将启动,但随后它只停留在初始屏幕上,就好像导航无法执行任何操作一样:

代码示例:

// 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);
      }
   }

有什么明显的地方我做错了吗?也许有更好的方法来处理导航?还是我忽略了什么?

编辑

这特别难以解决,因为我不能 运行 在 visual studio 中进行调试。为了测试它,我实际上必须从未连接到调试器的 my-protocol:// 启动它。

从 url / 协议启动时是否有调试方法?

我可以重现你的问题。 @kennyzx 的建议是正确的。导航前需要先做判断。

请参考以下代码示例以供参考。

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();
        }
}