【问题标题】:How to pass values from one xaml page to another in a Metro style app如何在 Metro 风格应用程序中将值从一个 xaml 页面传递到另一个页面
【发布时间】:2012-09-20 13:44:45
【问题描述】:

我有两个 XAML 页面:Menu.xaml 和 Main.xaml。

在 Menu.xaml 上,我有 2 个按钮(简单和困难),当我单击按钮时,我导航到 Main.xaml。

当我点击“简单”或“困难”时,我想传递一些值, 这样我就可以适当地填充 Main.xaml。

1. 如何在 C# 中传递值/参数?
2. 我在哪里检索这些值(例如在 PageLoad 上?)

【问题讨论】:

    标签: c# xaml windows-8 microsoft-metro winrt-xaml


    【解决方案1】:

    看看Frame.Navigate 方法。有一个重载可以让你传入一个参数。

    See this for an example

    通常看起来像这样:

    private void OnButtonClick(object sender, EventArgs args)
    {
        if (sender == easyButton)
          NavigateToDifficulty(DifficultyLevel.Easy);
        else
          NavigateToDifficulty(DifficultyLevel.Hard);
    }
    
    private void NavigateToDifficulty(DifficultyLevel difficulty)
    {
       this.Frame.Navigate(typeof(DifficultyPage), difficulty)
    }
    

    要检索导航参数,请查看LayoutAwarePage(包含在示例模板中)LoadState 方法

    【讨论】:

      【解决方案2】:

      在 Metro 应用程序中导航非常简单。

      C# 示例:

      将此添加到您的点击或任何其他将您带到主页的事件。

      this.Frame.Navigate(typeof(Main),myDifficulty); 
      

      然后在您要导航到的页面上,您可以在其 OnNavigatedTo 事件中获取参数。

      protected override void OnNavigatedTo(NavigationEventArgs e) 
      { 
          var myDifficulty= e.Parameter; 
          ...    
      } 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-18
        • 2015-04-04
        相关资源
        最近更新 更多