本文介绍了在 XAML 页面之间传递值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 VS 2012 & 构建我的第一个 Windows 8 Metro 应用程序C#.

它由一个简单的 2 页布局组成,第一个是演示文稿 &设置页面,第二个包含游戏本身(测验).

我在 MainPage.xaml.cs 中创建了一个 Player 实例,用于存储玩家姓名、游戏模式(简单、中等、困难)和问题主题(最终).

Player p = new Player();

每当设置值时,我都会使用

导航到 MainGame.xaml

this.Frame.Navigate(typeof(MainGame));

问题是:如何在页面之间传递这些值,以便我可以设置一个文本块,说名称正在播放"?

解决方案

您可以在 FrameNavigate(...) 方法.所以你应该这样写.

MainPage.xaml.cs

Player p = new Player();this.Frame.Navigate(typeof(MainGame), p);

现在可以在 MainGame.xaml.csOnNavigatedTo(...) 方法中获取 Player 的对象.>

MainGame.xaml.cs

protected override void OnNavigatedTo(NavigationEventArgs e){var objPlayer = e.Parameter as Player;}

I'm trying to build my first Windows 8 Metro App using VS 2012 & C#.

It consists of a simple 2 pages layout, the first being the presentation & setup page and the second consists of the game itself (a quiz).

I have created an instance of Player in the MainPage.xaml.cs that stores the Player Name, the game mode (easy, medium, hard) and the subject of the questions (eventually).

Player p = new Player();

Whenever the values are set i navigate to MainGame.xaml using

this.Frame.Navigate(typeof(MainGame));

The question is: how do I pass such values between pages so I can, say, set a Textblock saying "Name is Playing"?

解决方案

You can pass parameter object in Frame's Navigate(...) method. So you should write like this.

MainPage.xaml.cs

Player p = new Player();
this.Frame.Navigate(typeof(MainGame), p);

Now that object of Player can be get in MainGame.xaml.cs's OnNavigatedTo(...) method.

MainGame.xaml.cs

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    var objPlayer = e.Parameter as Player;
}

这篇关于在 XAML 页面之间传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 03:20