Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#/XAML Error No overload for 'button_Click' matches delegate 'Windows.UI.Xaml.RoutedEventHandler'

I am trying to switch between two previously defined styles in App.xaml for multiple buttons each time a button gets clicked. I tried a generic event handler but it will display the error No overload for 'button_Click' matches delegate 'Windows.UI.Xaml.RoutedEventHandler'. I believe it has to do with the signature but can't quite fix it. Here's the method implemented in MainPage.xaml.cs

public void button_Click(object sender, EventArgs e)
    {
        var button = (Button)sender;
        if (button.Style != myButtonStyle)
            button.Style = myButtonStylePressed;
        else
            button.Style = myButtonStyle;
    }

And this is how it is implemented in MainPage.xaml in one button sample.

<Button Name="Row5Col4"
            Grid.Row ="5"
            Grid.Column ="4"
            Margin="10, 10, 10, 10"
            Height="10"
            Width="10" 
            HorizontalAlignment="Left"
            BorderThickness="0"
            Style="{StaticResource myButtonStyle}"
            Click="button_Click"
            >
    </Button>

IMPORTANT: some people have recommended using MouseClick instead of Click, however this won't work since it is a WP application. I appreciate you stopping by.

The line that, according to Visual Studio causes the bug is this one Click="button_Click"

like image 208
Katie Arriaga M Avatar asked Dec 15 '22 13:12

Katie Arriaga M


1 Answers

Button's Click event doesn't have a parameter type of EventArgs.According to MSDN page , it has to be RoutedEventArgs

So your code must be :

public void button_Click(object sender, RoutedEventArgs e)
{
    var button = (Button)sender;
    if (button.Style != myButtonStyle)
        button.Style = myButtonStylePressed;
    else
        button.Style = myButtonStyle;
}
like image 121
Burak Kaan Köse Avatar answered Apr 27 '23 07:04

Burak Kaan Köse