【问题标题】:How to open WinUI Maui through URI Activation?如何通过URI激活打开WinUI Maui?
【发布时间】:2022-08-03 15:19:02
【问题描述】:

我创建了一个 Maui windows 应用程序。我希望通过 URI 激活应用程序并将查询参数传递给应用程序。

我在包清单中添加了用于通过 uri 调用应用程序的 windows 协议:

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

当我通过浏览器 my-app://foo.com?user=123456 激活应用程序时,应用程序启动,但它作为冷启动启动。在我的 Win UI 应用程序中,我覆盖了 onLaunched 方法,但无论我如何启动应用程序,我都无法访问该协议。我正在尝试从我的 UWP 应用程序重新创建以下代码:

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

所以我从这个开始,但 UWPLaunchActivatedEventArg 始终作为 Launch 而不是协议返回。

    protected override void OnLaunched(LaunchActivatedEventArgs args)
{
  var kind =   args.UWPLaunchActivatedEventArgs.Kind;
        base.OnLaunched(e)
}
  • OnLaunched 和 OnActivated 事件中 Kind 的值可能不同,但 MAUI 不会在 App 类中暴露 OnActivated,您可以尝试通过在 MauiProgram 类中添加它来拦截事件,参见 docs here 。
  • 我可以通过 onLaunched 和 OnActivated 拦截操作,但问题是我无法从 URI 中获取参数。

标签: maui winui-3


【解决方案1】:

这是我的解决方案:

protected override void OnLaunched(LaunchActivatedEventArgs args)
  {
      var activatedEventArgs = Microsoft.Windows.AppLifecycle.AppInstance.GetCurrent().GetActivatedEventArgs();

      base.OnLaunched(arguments);
  }

使用参数运行您的 URI,这是屏幕截图:

【讨论】:

    猜你喜欢
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    相关资源
    最近更新 更多