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 に答える