gorokizu
gorokizu

Reputation: 2588

Passing values between XAML pages

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"?

Upvotes: 1

Views: 6183

Answers (1)

Farhan Ghumra
Farhan Ghumra

Reputation: 15296

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; 
}

Upvotes: 9

Related Questions