如何通过 URI 激活打开 WinUI Maui?

Mer*_*erg 7 winui-3 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>
Run Code Online (Sandbox Code Playgroud)

当我通过浏览器 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();
    }
Run Code Online (Sandbox Code Playgroud)

因此,我已经开始使用此方法,但 UWPLaunchActivatedEventArg 始终作为 Launch 而不是协议返回。

    protected override void OnLaunched(LaunchActivatedEventArgs args)
{
  var kind =   args.UWPLaunchActivatedEventArgs.Kind;
        base.OnLaunched(e)
}
Run Code Online (Sandbox Code Playgroud)

Ang*_*lru 3

这是我的解决方案:

var activatedEventArgs = Microsoft.Windows.AppLifecycle.AppInstance.GetCurrent().GetActivatedEventArgs();
Run Code Online (Sandbox Code Playgroud)

使用参数运行您的 URI,屏幕截图如下: 在此输入图像描述