AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.34k stars 2.2k forks source link

Unable to find suitable setter or adder for property Click of type Avalonia.Controls #3898

Closed steskalja closed 2 years ago

steskalja commented 4 years ago

I am new to AvaloniaUI and I am trying to create a simple code-behind app, not mvvm. When I try to bind a button to Click as in the example documentation I cannot. I get the following error: Severity Code Description Project File Line Suppression State Error Unable to find suitable setter or adder for property Click of type Avalonia.Controls:Avalonia.Controls.Button for argument System.Private.CoreLib:System.String, available setter parameter lists are: System.EventHandler`1[[Avalonia.Interactivity.RoutedEventArgs, Avalonia.Interactivity, Version=0.9.0.0, Culture=neutral, PublicKeyToken=null]] (line 24 position 25) Line 24, position 25.

steskalja commented 4 years ago

Here is a screen shot showing the Click method is missing from the tag Click Missing

kekekeks commented 4 years ago

You need your event handler to have exactly the required signature

public void OnButtonClick(object sender, RoutedEventArgs args)
steskalja commented 4 years ago

I've attempted that and it does not work. Attached is my sample code ArsAdd.zip

obskyr commented 2 years ago

I have a similar issue using Command in a MVVM app! This XAML:

      <Button Content="Add 1"
              Margin="0,10,0,0"
              HorizontalContentAlignment="Center"
              HorizontalAlignment="Stretch"
              Command="{Binding ButtonAddClicked}"/>

Gives me the error:

Unable to find suitable setter or adder for property Command of type Avalonia.Controls:Avalonia.Controls.Button for argument System.Private.CoreLib:System.String, available setter parameter lists are: Avalonia.UnsetValueType Avalonia.Data.IBinding System.Windows.Input.ICommand

However, it compiles fine. This error only shows up in Visual Studio's error list while editing, but doesn't actually obstruct compilation. Is there a way to avoid this?

maxkatz6 commented 2 years ago

@obskyr you need to rebuild project, so xaml editor can see new changes in your assemblies.

maxkatz6 commented 2 years ago

Original issue was answered. Sample code works with both 0.9.12 (issue is that old) and 0.10.12

obskyr commented 2 years ago

@maxkatz6 Thank you kindly!

omarisai commented 1 year ago

I am still seeing this issue in the last version, it would be better to adjust the documentation to a way that actually works.

maxkatz6 commented 1 year ago

@omarisai what documentation page and what adjustments do you mean?

omarisai commented 1 year ago

Hi @maxkatz6; thanks for responding!

I was referring to Controls: Buttons documentation section Binding to Events, in the second code block.

I discovered my error 😅, my apologies, I was wrong, I was declaring the event in the ModelView file instead of the View file. After moving the event to the View file it worked.

timunie commented 1 year ago

@omarisai if you think there is something that can be done to improve the docs, feel free to send a PR for this.

Mihawk2022 commented 8 months ago

found some problem in tutorial: https://docs.avaloniaui.net/docs/get-started/test-drive/respond-to-an-event

ButtonClicked function should be written in MainView.axaml.cs, not in MainWindow.axaml.cs

timunie commented 8 months ago

@Mihawk2022 that's not 100% true. If could be MainWindow or MainView, depending on the template used. But a PR to clarify this is welcome.

gschlitt commented 6 months ago

Following the tutorial https://docs.avaloniaui.net/docs/get-started/test-drive/respond-to-an-event definitely requires the placement of ButtonClicked eventhandler in MainView.axaml.cs

zbarrier commented 4 months ago

Ran into the button click issue today while following the tutorial. This really needs to be fixed, very frustrating.

timunie commented 4 months ago

@zbarrier a PR to the docs is really welcome 🤗

zbarrier commented 4 months ago

@zbarrier a PR to the docs is really welcome 🤗

I didn't have the time earlier when going through the tutorial. I will try to create PR with the change later. I really like Avalonia so far and understand it can be difficult to maintain the docs as they grow and things change.

reelthymeos commented 3 months ago

found some problem in tutorial: https://docs.avaloniaui.net/docs/get-started/test-drive/respond-to-an-event

ButtonClicked function should be written in MainView.axaml.cs, not in MainWindow.axaml.cs

This is still an issue in the documentation. Someone please update it.

timunie commented 3 months ago

@reelthymeos I don't see why it is wrong? You have to add the event handler to the control you are working on.

If you think this can be make more clear, feel free to file a PR proposal to the docs.

cornerbowlsoftware commented 2 months ago

I have defined a ContentControl that includes the exact code found in the documentation:

public void ClickHandler(object sender, RoutedEventArgs args)

The axaml includes the following inside the ControlTemplate definition:

<Button Click="ClickHandler">Press Me!</Button>

The application fails to compile with the above referenced error message.

maxkatz6 commented 2 months ago

@cornerbowlsoftware

ControlTemplate

Control template is basically a static factory method, that won't have access to your instance-bound handler.

cornerbowlsoftware commented 2 months ago

Thanks, I figured that out a little later. I figured it out using someone else's post.

protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
    var btn = e.NameScope.Find<Button>("PART_btn");
    if (btn != null)
    {
        btn.Click += ViewPswd;
    }
}
maxkatz6 commented 2 months ago

@cornerbowlsoftware yeah, OnApplyTemplate is a preferred approach.

sedwick2048 commented 1 month ago

In my OnApplyTemplate, btn was null. I had success with OnInit:

<UserControl ... Initialized="OnInit">

  private void OnInit(object sender, EventArgs e) {
    PART_btn.Click += ViewPswd;
  }
reelthymeos commented 3 weeks ago

Hi,

I am having this same issue. I have a button in my view:

<Button Grid.Row="1" Grid.Column="2" HorizontalAlignment="Left" Click="SendCommand_Click">Send</Button>

and in my view model:

public void SendCommand_Click(object sender, RoutedEventArgs args)
{
    //do some stuff
}

When I try to compile I get the above error. People on this thread are talking about use of some template to resolve this. Can someone explain what that is and how to use it? I am somewhat new to this and not familiar with templates and their usage.

timunie commented 3 weeks ago

Click is an event, not a command. It belongs into code behind not your viewmodel. You can also bind to commands which I prefer. Check Avalonia.Samples for a tutorial on that topic

workgroupengineering commented 3 weeks ago

Hi,

I am having this same issue. I have a button in my view:

<Button Grid.Row="1" Grid.Column="2" HorizontalAlignment="Left" Click="SendCommand_Click">Send</Button>

and in my view model:

public void SendCommand_Click(object sender, RoutedEventArgs args)
{
  //do some stuff
}

When I try to compile I get the above error. People on this thread are talking about use of some template to resolve this. Can someone explain what that is and how to use it? I am somewhat new to this and not familiar with templates and their usage.

Move this code in code-behind

public void SendCommand_Click(object sender, RoutedEventArgs args)
{
    //do some stuff
}

es: MyView.axaml.cs

If you need comunicate with VW using should use Command