用LaunchUriAsync启动的UWP应用程序未显示主页

时间:2018-07-20 15:26:53

标签: uwp

我已经用LaunchUriAsync启动了一个uwp应用程序,但是该应用程序未正确加载(未显示应用程序的主页),它显示了默认的蓝屏

 public MainPage()
        {
            this.InitializeComponent();
            callUri();


        }

        private async void callUri()
        {
            var uriBing = new Uri((@"testapptolaunch://"));

            // Launch the URI
            var success = await Windows.System.Launcher.LaunchUriAsync(uriBing);

        }

并在app.xaml.cs中添加了以下代码

protected override void OnActivated(IActivatedEventArgs args)
        {
            if (args.Kind == ActivationKind.Protocol)
            {
                ProtocolActivatedEventArgs eventArgs = args as ProtocolActivatedEventArgs;
                // Navigate to a view 
                Frame rootFrame = Window.Current.Content as Frame;
                if (rootFrame == null)
                {
                    rootFrame = new Frame();
                    Window.Current.Content = rootFrame;
                }
                // assuming you wanna go to MainPage when activated via protocol
                rootFrame.Navigate(typeof(MainPage), eventArgs);

            }

        }

1 个答案:

答案 0 :(得分:1)

  

但是应用程序未正确加载(未显示应用程序的主页),它显示了默认的蓝屏

问题是您尚未在OnActivated覆盖函数中调用Window.Current.Activate();方法。请使用以下内容替换您的内容。

protected override void OnActivated(IActivatedEventArgs args)
{

    if (args.Kind == ActivationKind.Protocol)
    {
        ProtocolActivatedEventArgs eventArgs = args as ProtocolActivatedEventArgs;
        // Navigate to a view 
        Frame rootFrame = Window.Current.Content as Frame;
        if (rootFrame == null)
        {
            rootFrame = new Frame();
            Window.Current.Content = rootFrame;
        }
        // assuming you wanna go to MainPage when activated via protocol
        rootFrame.Navigate(typeof(MainPage), eventArgs);

    }
    Window.Current.Activate();
}