Frage

When I trigger the click event to navigate to LocationDetail like this:

NavigationService.Navigate(new Uri("/LocationDetails.xaml", UriKind.Relative));

The app crashes and debugger opens App.xaml.cs highlighted at this code:

private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            if (Debugger.IsAttached)
            {
                // A navigation has failed; break into the debugger
                Debugger.Break();
            }
        }

Does anyone have an idea as to why this is happening. Is there an error in the class or a reason why it would do this?

The complete class for the page being navigated to is below:

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


    }
}

This is the code that I causing the crash:

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);
        }
War es hilfreich?

Lösung

The source of the problem is usually the fact that the filename of the page is misspelled or that the page you are navigating to has invalid XAML code. You can try to comment some code on the XAML page to see if the navigation is performed correctly if less content is displayed.

You can also investigate the NavigationFailedEventArgs object passed to the error handler and read the e.Exception.Message property, which should contain additional details about the exception thrown.

In case the QueryString's property can be unset, you will have to check for this situation:

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

    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top