0

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);
        }
Brian Var
  • 6,029
  • 25
  • 114
  • 212

1 Answers1

1

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

    }
Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
  • I just updated my answer with one additional suggestion, so try that one too in case of problems. – Martin Zikmund Mar 09 '14 at 16:08
  • I rebuilt the xaml and its working now but my the code I'm using for saving a file is causing it to crash.I have posted the code above,could you take a look at it and see how it could be causing the crash? – Brian Var Mar 09 '14 at 16:45
  • Can you step through the problematic code snippet and find out where exactly is the exception thrown? What comes to my mind now is that accessing the QueryString if the parameter is not present maybe could cause the exception - try to use an if statement like this: `if (NavigationContext.QueryString.TryGetValue("note", out navigationMessage))` The condition is false in case the parameter is not present. – Martin Zikmund Mar 10 '14 at 08:40
  • Ifound the problem to this,the locationDetails page is being called from my main menu, so when it is navigated to there is no query string at this stage..Could I fix this by putting ` base.OnNavigatedTo(e); ` before the file opening code or would an if else statement work? – Brian Var Mar 10 '14 at 13:23
  • I have updated my answer with a code that should resolve this problem, can you try it :-) ? – Martin Zikmund Mar 10 '14 at 21:54