Closed steskalja closed 2 years ago
Here is a screen shot showing the Click method is missing from the tag
You need your event handler to have exactly the required signature
public void OnButtonClick(object sender, RoutedEventArgs args)
I've attempted that and it does not work. Attached is my sample code ArsAdd.zip
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 typeAvalonia.Controls:Avalonia.Controls.Button
for argumentSystem.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?
@obskyr you need to rebuild project, so xaml editor can see new changes in your assemblies.
Original issue was answered. Sample code works with both 0.9.12 (issue is that old) and 0.10.12
@maxkatz6 Thank you kindly!
I am still seeing this issue in the last version, it would be better to adjust the documentation to a way that actually works.
@omarisai what documentation page and what adjustments do you mean?
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.
@omarisai if you think there is something that can be done to improve the docs, feel free to send a PR for this.
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
@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.
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
Ran into the button click issue today while following the tutorial. This really needs to be fixed, very frustrating.
@zbarrier a PR to the docs is really welcome 🤗
@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.
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.
@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.
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.
@cornerbowlsoftware
ControlTemplate
Control template is basically a static factory method, that won't have access to your instance-bound handler.
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;
}
}
@cornerbowlsoftware yeah, OnApplyTemplate is a preferred approach.
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;
}
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.
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
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
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.