当我触发click事件以导航到LocationDetail时:
NavigationService.Navigate(new Uri("/LocationDetails.xaml", UriKind.Relative));
应用程序崩溃和调试程序打开此代码突出显示的App.xaml.cs:
private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
if (Debugger.IsAttached)
{
// A navigation has failed; break into the debugger
Debugger.Break();
}
}
有没有人知道为什么会这样。班级中是否有错误或者为什么会这样做?
导航到的页面的完整类如下:
namespace MyNotes
{
public partial class LocationDetails : PhoneApplicationPage
{
public LocationDetails()
{
InitializeComponent();
}
private void NoteTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
string filename = this.NavigationContext.QueryString["note"];
if (!string.IsNullOrEmpty(filename))
{
using (var store = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
using (var stream = new IsolatedStorageFileStream(filename, FileMode.Open, FileAccess.ReadWrite, store))
{
StreamReader reader = new StreamReader(stream);
this.NoteTextBox.Text = reader.ReadToEnd();
this.FilenameTextBox.Text = filename; reader.Close();
}
}
base.OnNavigatedTo(e);
}
private void SaveButton_Click(object sender, EventArgs e)
{
try
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
using (var stream = new IsolatedStorageFileStream(FilenameTextBox.Text, FileMode.Create, FileAccess.Write, store))
{
StreamWriter writer = new StreamWriter(stream);
writer.Write(NoteTextBox.Text); writer.Close();
}
}
catch (Exception)
{
MessageBox.Show("Error saving the file");
}
}
private void ListButton_Click(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/LocationDetailsList.xaml", UriKind.Relative));
}
}
}
这是导致崩溃的代码:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
/*
string filename = this.NavigationContext.QueryString["note"];
if (!string.IsNullOrEmpty(filename))
{
using (var store = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
using (var stream = new IsolatedStorageFileStream(filename, FileMode.Open, FileAccess.ReadWrite, store))
{
StreamReader reader = new StreamReader(stream);
this.NoteTextBox.Text = reader.ReadToEnd();
this.FilenameTextBox.Text = filename; reader.Close();
}
}
*/
base.OnNavigatedTo(e);
}
答案 0 :(得分:1)
问题的根源通常是页面文件名拼写错误或您导航到的页面包含无效的XAML代码。您可以尝试在XAML页面上注释某些代码,以查看在显示的内容较少时导航是否正确执行。
您还可以调查传递给错误处理程序的NavigationFailedEventArgs
对象并读取e.Exception.Message
属性,该属性应包含有关抛出异常的其他详细信息。
如果可以取消设置QueryString的属性,则必须检查这种情况:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string filename = "";
if ( NavigationContext.QueryString.TryGetValue("note", out filename ) && !string.IsNullOrEmpty(filename))
{
using (var store = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication())
using (var stream = new IsolatedStorageFileStream(filename, FileMode.Open, FileAccess.ReadWrite, store))
{
StreamReader reader = new StreamReader(stream);
this.NoteTextBox.Text = reader.ReadToEnd();
this.FilenameTextBox.Text = filename; reader.Close();
}
}
}