【问题标题】:Launching from UWP URI Protocol Scheme is Not Navigating从 UWP URI 协议方案启动未导航
【发布时间】:2018-11-27 17:03:36
【问题描述】:

我已按照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 / 协议启动时有没有办法调试这个?

【问题讨论】:

  • 在这种情况下Window.Current.Content 是否为空?
  • 它对我来说不是空的,但这是一件好事。

标签: xamarin.forms uwp url-scheme uwp-navigation


【解决方案1】:

我可以重现您的问题。 @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();
        }
}

【讨论】:

  • 我试过这个解决方案......它对我不起作用。现在应用程序只是崩溃了,而不是在启动屏幕上停止。我会继续努力并报告。
  • 我上面的代码运行良好。如果它对您不起作用,则您的代码中可能存在其他一些问题。在这种情况下很容易调试。 “在Visual Studio中右键单击您的项目-选择属性-选择调试-查看启动操作-选择不启动,但在启动时调试我的代码”。然后开始在本地计算机上进行调试,您的应用程序将无法启动,您键入您的应用在浏览器中的协议以激活您的应用。
  • 哇,在这种情况下,该选项可以挽救生命……非常感谢您的分享。
  • 我错过了Window.Current.Activate(); 行,现在可以使用,谢谢!
猜你喜欢
  • 2019-09-27
  • 2011-02-09
  • 2020-02-09
  • 2014-08-18
  • 1970-01-01
  • 2020-01-14
  • 2023-03-27
  • 1970-01-01
  • 2018-10-22
相关资源
最近更新 更多