UWP-帧导航仅在传递参数时崩溃 [英] UWP - Frame Navigate only crashes when passing a parameter
问题描述
我试图将clicked元素的ID作为字符串传递给新页面,但是该应用程序始终崩溃.我不完全确定如何找出问题的原因,因为我发现的每个示例都使用相同的方法,并且对他们来说效果很好.
I'm trying to pass the id of the clicked element as a string to a new page, however the app always crashes. I'm not entirely sure how to find out the cause of the problem as every example I've found uses the same method and it works just fine for them.
发送者事件:
private void MovieBox_OnTapped(object sender, TappedRoutedEventArgs e)
{
var id = (sender as StackPanel).Tag as string;
Debug.WriteLine(id); //the correct id in string
Frame.Navigate(typeof(MovieDetailsPage),id);
}
OnNavigatedTo事件
The OnNavigatedTo event
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string text = e.Parameter as string;
Debug.WriteLine(text);
await Task.CompletedTask;
}
该应用程序似乎在其间的某个位置崩溃了.在第一个事件上放置一个断点会显示发送正确的ID,但是它永远不会到达OnNavigatedTo.
The app crashes somewhere in between it seems like. Putting a breakpoint on the first event shows the correct id being sent, however it never reaches OnNavigatedTo.
我不确定是什么问题,因为如果删除该参数,第二页加载就很好了.
I'm not sure what the problem is, as the second page loads just fine if I remove the parameter.
我从App.g.i.cs文件中写入了异常:
I wrote the exception from the App.g.i.cs file:
Exception thrown: 'Newtonsoft.Json.JsonReaderException' in MovieLibraryApp.exe
An exception of type 'Newtonsoft.Json.JsonReaderException' occurred in
MovieLibraryApp.exe but was not handled in user code
Unexpected character encountered while parsing value: m. Path '', line 0, position 0.
这是xaml(有点混乱的atm): https://pastebin.com/GkXkxYDS
And here is the xaml (it's kinda a mess atm): https://pastebin.com/GkXkxYDS
也是这样:
Your parameter must be serializable. If it isn't, then use SessionState
整个.cs页面,尽管我在这里还没有做任何逻辑,因为我没有使参数起作用. MovieDetailsPage.xaml.cs:
The entire .cs page, though I haven't done any logic here yet as I never got the parameter to work. MovieDetailsPage.xaml.cs:
public sealed partial class MovieDetailsPage
{
public MovieDetailsPage()
{
InitializeComponent();
NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (e.Parameter != null)
{
string text = Convert.ToString(e.Parameter);
}
}
}
推荐答案
Questioner使用的是Template10(在聊天过程中了解它),因此以下是实现Navigation的最终答案.
Questioner was using Template10 (go to know about it during chat) so following is the final and updated answer for implementing Navigation.
对于Template10:
way-1
using Template10.Services.SerializationService;
...
string id = (string)(sender as StackPanel).Tag;
string str=SerializationService.Json.Serialize(id);
Frame.Navigate(typeof(MovieDetailPage), str);
方法2
var NavService = NavigationService.GetForFrame(this.Frame);
NavService.Navigate(typeof(MovieDetailPage), id);
这篇关于UWP-帧导航仅在传递参数时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!