时间轴中的UWP UserActivity无法导航到单击的正确页面

时间:2018-08-19 22:09:19

标签: c# uwp

我正在开发要与Windows时间轴集成的UWP应用。但是,当我在“时间轴”中单击适用于我的应用程序的自适应卡时,该卡始终会导航到首页,而不是SecondPage。我怀疑App.xaml.cs中的Shell或我的URI协议中的OnActivated中存在错误,但是我不确定。任何建议将不胜感激。

App.xaml.cs

    protected override void OnActivated(IActivatedEventArgs e)
    {
        if (!(Window.Current.Content is Shell shell))
        {
            shell = new Shell();

            Window.Current.Content = shell;

            if (e.Kind == ActivationKind.Protocol)
            {
                var uriArgs = e as ProtocolActivatedEventArgs;
                if (uriArgs != null)
                {
                    if (uriArgs.Uri.Host == "secondpage")
                    {
                        shell.RootFrame.Navigate(typeof(SecondPage), uriArgs);
                    }
                }
            }
            Window.Current.Activate();
        }
    }

SecondPage.xaml.cs

protected override async void OnNavigatedTo(NavigationEventArgs e)
    {
        // If we were activated from a URI, say so.
        // A real app would use the information in the URI to restore state.

            await CreateUserActivityAsync();
    }

    async Task CreateUserActivityAsync()
    {
        // Get channel and create activity.
        UserActivityChannel channel = UserActivityChannel.GetDefault();
        UserActivity activity = await channel.GetOrCreateUserActivityAsync("MyActivityName");

        // Set deep-link and properties.
        activity.ActivationUri = new Uri("myapp:page?secondpage");
        activity.VisualElements.DisplayText = "Second Page";

        // Save to activity feed.
        await activity.SaveAsync();

        // Create a session, which indicates that the user is engaged
        // in the activity.
        _currentSession = activity.CreateSession();
    }

    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        // Dispose the session, which indicates that the user is no longer
        // engaged in the activity.
        _currentSession?.Dispose();
    }

1 个答案:

答案 0 :(得分:0)

在您的CreateUserActivityAsync方法中创建UserActivity时,您配置了错误的格式Uri,因此uriArgs.Uri.Host方法中的OnActivated将为{{1} },没有任何字符串值与您的字符串“ secondpage”匹配。请修改您的Uri为:

""

然后,您可以在 // Set deep-link and properties. activity.ActivationUri = new Uri("myapp://secondpage?xxxxxx"); 方法中匹配uriArgs.Uri.Host == "secondpage"