Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass values from one xaml page to another in a Metro style app

I have two XAML pages: Menu.xaml and Main.xaml.

On Menu.xaml, I have 2 buttons (Easy & Hard) and when I click the buttons, I navigate to Main.xaml.

I want to pass some value when I click 'Easy' or 'Hard', so that I can populate Main.xaml appropriately.

1. How can I pass values/arguments in C#?
2. Where do I retrieve these values (e.g. on PageLoad?)

like image 381
nv.snow Avatar asked Sep 28 '12 16:09

nv.snow


2 Answers

Take a look at the Frame.Navigate methods. There is an overload that lets you pass in a parameter.

See this for an example

Typically this would look something like this:

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

To retrieve the navigation parameter look at the LayoutAwarePage (included in the sample templates) LoadState method

like image 81
cordialgerm Avatar answered Nov 13 '22 18:11

cordialgerm


Navigation is pretty simple in a Metro App.

C# Example:

Add this to your click or whatever other event takes you to the main page.

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

Then on the page you're navigating to, you can get the argument in its OnNavigatedTo event.

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    var myDifficulty= e.Parameter; 
    ...    
} 
like image 31
J. Tanner Avatar answered Nov 13 '22 18:11

J. Tanner