I am thinking if the windows phone 7 button event is similar to ASP.NET development with C#, something like in the button, I set value to commandparameter in the XAML, and in the code behind, I get the commandparameter and redirect the page.

I haven't found any good examples for button event handling, any suggestions?

Thank you.

2

There are 2 answers

3
Teemu Tapanila On

For the xaml:

<Button Tag="pageAddress" Click="Button_Click" />

And then on the code-behind:

private void Button_Click(object sender, RoutedEventArgs e)
{
     Button _button = (Button)sender;
     NavigationService.Navigate(new System.Uri(_button.Tag.ToString()));
}
0
Edward On

I would recommend you use a command parameter as you mentioned. So in your xaml do something like this:

<Button x:name="myButton" CommandParameter="{Binding Title}" Click="myButton_Click"/>

And in your C# code something like this:

private void myButton_Click(object sender, RoutedEventArgs e)
{
    Button _myButton = (Button)sender;
    string value = _myButton.CommandParameter.ToString();
}

Really it's pretty similar to Teemu's answer although I must admit I haven't used the Tag element before. According to the documentation on MSDN, the Tag element should work pretty nicely as it can store custom information that you can access in your code behind (or viewmodel).