C# 将不同的按钮指向同一页面,但显示不同的内容

C# 将不同的按钮指向同一页面,但显示不同的内容,c#,windows-phone-8,visual-studio-2013,C#,Windows Phone 8,Visual Studio 2013,我正在为Windows8手机编写一个应用程序。我想将许多不同的按钮指向同一页面,我正在努力解决的问题是,当按下按钮时,页面会显示不同的内容。这可能吗?如果可能,如何执行此操作?是的,您可以在导航到其他页面时发送不同的查询字符串参数,例如: 从第1页到目标页 NavigationService.Navigate(new Uri("/SecondPage.xaml?msg=" + "From Page1", UriKind

我正在为Windows8手机编写一个应用程序。我想将许多不同的按钮指向同一页面,我正在努力解决的问题是,当按下按钮时,页面会显示不同的内容。这可能吗?如果可能,如何执行此操作?

是的,您可以在导航到其他页面时发送不同的查询字符串参数,例如: 从第1页到目标页

NavigationService.Navigate(new Uri("/SecondPage.xaml?msg=" + 
                                      "From Page1", UriKind.Relative));
从第2页到目标页

NavigationService.Navigate(new Uri("/SecondPage.xaml?msg=" + 
                                      "From Page2", UriKind.Relative));
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            string msg = "";

            if (NavigationContext.QueryString.TryGetValue("msg", out msg))

                textBlock1.Text = msg;           
        }
在TargetPage中

NavigationService.Navigate(new Uri("/SecondPage.xaml?msg=" + 
                                      "From Page2", UriKind.Relative));
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            string msg = "";

            if (NavigationContext.QueryString.TryGetValue("msg", out msg))

                textBlock1.Text = msg;           
        }


这是一种方法,或者您可以在
App
类中拥有一些公共对象,这些对象可以在整个应用程序中访问

正如AD.Net所写,您需要做的是在页面之间传递参数

(没有时间测试代码,只是在脑子里写了出来,这样可能会有错误,如果有人看到了,请把它们删掉)

让我们用一个例子来说明这一点: 我的页面上有三个按钮,每次单击其中一个按钮时,我都会导航到第二个页面,但每个按钮都会以不同的背景呈现页面:

  • 让我们创建一个应用程序并创建两个页面,例如Page1.xamlPage2.xaml

  • 现在,在Page1上,使用designer添加三个按钮,并为这些按钮提供其事件处理程序,用于单击事件

    <Button x:Name="btn1" Click="btn1_Click" />
    <Button x:Name="btn2" Click="btn2_Click" />
    <Button x:Name="btn3" Click="btn3_Click" />
    
  • 差不多了……在Page2.xaml上,我将转到codebehind(Page2.xaml.cs文件),并添加这个有趣的小东西:

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        if (NavigationContext.QueryString.ContainsKey("parameter"))
        {
              string val = NavigationContext.QueryString["parameter"];
              switch(val)
              {
                  case "Red":
                       this.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Red);
                       break;
                  case "Blue":
                       this.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Blue);
                       break;
                  case "Green":
                       this.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Green);
                       break;
                  default:
                       this.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Black);
                       break;
              }
        }
    }
    

  • 就在那里!有一件事叫做在页面之间传递参数。有很多方法可以实现这一点。如果你想传递更复杂的信息,这种方法是行不通的。您可以使用的方法是在页面之间传递对象。当然,您也可以根据MVVM模式构建应用程序(推荐)或使用单例(不推荐,但可以使用)。网上有很多关于这三个人的教程。只需查找“在页面之间传递对象windows phone 8”、“模型视图视图模型windows phone 8”或“单一模式”,或者您可以在页面之间传递对象和变量。。。。使用override of OnNavigatedFrom方法…或者您可以使用MVVM模式…或者将它们组合在一起谢谢,基本上我希望每次按下其中一个按钮时,页面顶部会显示不同的消息,如何在我希望显示信息的页面上对其进行排序?