【问题标题】:How to fix a navigation service failed error? Windows phone 8如何修复导航服务失败错误?视窗电话 8
【发布时间】: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


【解决方案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();
            }
        }

    }

【讨论】:

  • 我刚刚更新了我的答案,增加了一项建议,所以如果出现问题,也请尝试一下。
  • 我重建了 xaml 并且它现在可以工作,但是我用于保存文件的代码导致它崩溃。我已经发布了上面的代码,你能看看它看看它是如何导致崩溃的?
  • 您能否单步调试有问题的代码 sn-p 并找出究竟是哪里抛出了异常?我现在想到的是,如果参数不存在访问 QueryString 可能会导致异常 - 尝试使用这样的 if 语句:if (NavigationContext.QueryString.TryGetValue("note", out navigationMessage)) 如果参数不存在,则条件为假。
  • 我发现了这个问题,从我的主菜单调用 locationDetails 页面,所以当它导航到这个阶段没有查询字符串时。我可以通过放置`base. OnNavigatedTo(e); ` 在文件打开代码之前还是 if else 语句有效?
  • 我已经用一个可以解决这个问题的代码更新了我的答案,你可以试试:-) 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-06
  • 1970-01-01
相关资源
最近更新 更多