【问题标题】:Avalonia button click event is not workingAvalonia 按钮单击事件不起作用
【发布时间】:2020-05-16 23:00:01
【问题描述】:

我有一个简单的 Avalonia 表单:

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="AvaloniaExperiment.MainWindow"
        Title="AvaloniaExperiment">
  <StackPanel>
    <TextBlock>Welcome to Avalonia!</TextBlock>
    <Button Name="btn" Click="btn_OnClick">Fred!</Button>
  </StackPanel>
</Window>

以及后面代码中的一个方法(我想这样做,直到我熟悉 Avalonia,然后也许我会尝试 MVVM):

private void btn_OnClick()
{
    btn.Text = "Ginger";
}

但是我得到了这些编译错误:

名称 btn 在当前上下文中不存在(在后面的代码中)

无法为参数 System.Private.CoreLib:System.String 的 Avalonia.Controls:Avalonia.Controls.Button 类型的属性 Click 找到合适的 setter 或 adder,可用的 setter 参数列表有: System.EventHandler`1[[Avalonia.Interactivity.RoutedEventArgs, Avalonia.Interactivity, Version=0.9.0.0, Culture=neutral, PublicKeyToken=null]](在 XAML 中)

无法为参数 System.Runtime:System.String 类型的 Avalonia.Controls:Avalonia.Controls.Button 属性命令找到合适的设置器或加法器,可用的设置器参数列表有: Avalonia.UnsetValueType Avalonia.Data.IBinding System.Windows.Input.ICommand(也在 XAML 中)

在连接这个事件处理程序时我做错了什么?

【问题讨论】:

    标签: c# xaml avalonia


    【解决方案1】:

    你试过了吗……

    public void btn_OnClick(object sender, RoutedEventArgs e)
    {
        btn.Text = "Ginger";
    }
    

    【讨论】:

    • 现在试过了,但我仍然得到上面列出的第二个错误。实际上,我必须将 btn 替换为对 FindControl 的调用才能按名称查找它,但我仍然收到该错误。
    • 之前好像有人在github issue中遇到过这个问题
    • Noepe,我仍然收到错误,即使我将其更改为 Click="{Binding btn_OnClick}"...
    【解决方案2】:

    您应该在父控件构造函数中添加一个 ControlLink 像这样:

    public class AnyParentControl
    {
        Button btn; // for the class
        public AnyParentControl() // constructor
        {
            InitializeComponent(); // necessary method for Avalonia
    
            btn = this.Find<Button>("The Control Name on XAML File");
            btn.Click += Cbtn_Click; // event link
        }
    }
    

    来自秘鲁的问候:D

    【讨论】:

      【解决方案3】:

      按钮没有Text 属性。它确实有Content

      btn.Content = "Ginger";
      

      【讨论】:

      • 哦,是的,我确实注意到了,不过感谢您指出!
      【解决方案4】:

      发件人您刚刚单击的按钮,因此将发件人类型转换为 Button 并将其 Content 属性(不是 Text)设置为您想要的任何内容。

      public void btn_OnClick( object? sender, RoutedEventArgs args )
      {
          ( sender as Button )!.Content = "Ginger";
      }
      

      无需在树或其他任何地方查找它,这样您就可以为所有按钮重用相同的代码,例如,根据它是哪个按钮,设置不同的名称或样式或其他属性等。

      更高级:

      public void btn_OnClick( object? sender, RoutedEventArgs args )
      {
          var button = ( sender as Button )!;
          switch ( button.Name )
          {
              case "btn":
              {
                  button.Content = "Ginger";
              }
              break;
              case "otherBtn":
              {
                  button.Content = "Ale";
              }
              break;
              default:
              {
                  button.Content = "No clue which Button you are!";
              }
              break;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2018-09-18
        • 2014-09-26
        • 1970-01-01
        • 1970-01-01
        • 2013-06-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-31
        • 2020-10-10
        相关资源
        最近更新 更多