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?)
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
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;
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With