【发布时间】:2014-03-09 16:02:51
【问题描述】:
当我触发点击事件导航到 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);
}
【问题讨论】:
-
可能是您的“LocationDetails.xaml”(xaml) 或导航路径中的问题
-
@Brian J 也许是 base.OnNavigatedTo(e);应该在所有代码执行之前出现??
标签: c# windows-phone-8 app.xaml navigationservice