[英]UWP UserActivity in Timeline not navigating to correct page on click
我正在开发要与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();
}
在CreateUserActivityAsync
方法中创建UserActivity时,配置的Uri格式错误,因此OnActivated
方法中的uriArgs.Uri.Host
将为""
并且没有任何字符串值与字符串“ secondpage”相匹配。 请修改您的Uri为:
// Set deep-link and properties.
activity.ActivationUri = new Uri("myapp://secondpage?xxxxxx");
然后,您可以在OnActivated
方法中匹配uriArgs.Uri.Host == "secondpage"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.