【问题标题】:Navigation_Failed when pass string value between pages in windows phone C#在Windows Phone C#中的页面之间传递字符串值时Navigation_Failed
【发布时间】:2014-07-26 20:39:55
【问题描述】:

我想将字符串值从 setting.xaml 传递给 MainPage.xaml

这是我的代码 settings.xaml

Public settings(){
.....
string url = string.Format("/MainPage.xaml?msg1={0}", updateSignature.Text);
        NavigationService.Navigate(new Uri(url, UriKind.Relative));}

我的 update.Signature 是一个文本块

这是我在 MainPage.xaml

中的代码
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            string msg1 = "";
            if (NavigationContext.QueryString.TryGetValue("msg1", out msg1))
            {
                textBlock1.Text = String.Format("Signature Anda :{0}", msg1);
            }

当我构建它并移至设置页面时,出现 Navigation_Failed 错误。我该怎么办?

【问题讨论】:

  • 您的意思是 NavigationFailed 事件还是异常?如果是例外,请提供例外的详细信息。如果是事件,请在事件参数上提供 Exception 属性的详细信息。没有这些,我们只能猜测问题——这对您没有特别的帮助。
  • 每当我进入设置页面时,我都会抛出 RootFrame_NavigationFailed。但现在,幸运的是,我已经解决了这个问题。我没有注意到我没有以适当的方式使用 NavigationService。顺便说一句,谢谢

标签: c# windows windows-phone-8


【解决方案1】:

您在 SettingsPage.xaml 中的代码似乎在构造函数中。这不是这样做的正确方法。我无法了解您的问题到底是什么,但我可以以简单的方式为您提供解决方案。我在设置 page.xaml 中的代码是,

    <StackPanel>
        <TextBlock Name="updateSignature" Text="textinSettingsPage" FontSize="30"/>
        <Button Content="GotoMainPage" Click="Button_Click"/>
    </StackPanel>

我使用按钮单击处理了这个问题。这是我在 SettingsPage.xaml.cs 文件中的代码,

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        string url = string.Format("/MainPage.xaml?msg1={0}", updateSignature.Text);
        NavigationService.Navigate(new Uri(url, UriKind.Relative));
    }

这是 MainPage.xaml 中的代码,

    <StackPanel>
        <TextBlock Name="textBlock1" FontSize="30"/>
        <Button Content="Go to settings page" Click="settings_Click" Name="settings"/>
    </StackPanel>

最后我的代码是 MainPage.xaml.cs,

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        string msg1 = "";
        if (NavigationContext.QueryString.TryGetValue("msg1", out msg1))
        {
            textBlock1.Text = String.Format("Signature Anda :{0}", msg1);
        }
    }

    private void settings_Click(object sender, RoutedEventArgs e)
    {
        NavigationService.Navigate(new Uri("/SettingsPage.xaml", UriKind.RelativeOrAbsolute));
    }

【讨论】:

  • 天哪,我把 NavigationService 放在那个构造函数中的错误,现在我有 Home 按钮和 backPressed 事件处理来处理那个 NavigationService。现在,它的工作!哦,我的下一个问题,如何在我的 MainPage 的其他方法中使用该新字符串值?
  • 谢谢,我已经解决了上面的所有问题,现在适合我的场景。
  • 很高兴它有帮助。如果对您有帮助,请采纳答案。
猜你喜欢
  • 2013-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-24
  • 2014-09-16
  • 1970-01-01
相关资源
最近更新 更多