0

当我触发点击事件导航到 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);
        }
4

1 回答 1

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();
            }
        }

    }
于 2014-03-09T16:05:56.210 回答