如何使用事件处理程序在毛伊岛创建自定义控件?

Tho*_*ton 4 maui

我正在尝试通过两个按钮在毛伊岛创建自定义控件。

每当开发人员想要在页面中插入控件时,就会显示两个按钮,他可以添加一些特定的操作作为处理程序。

public partial class BottomButtons : Grid
{
    public BottomButtons()
    {
        InitializeComponent();
    }

    public BindableProperty LeftButtonTextProperty = BindableProperty.Create(propertyName: nameof(LeftButtonText),
                                                                    returnType: typeof(String),
                                                                    defaultValue: "Cancel",
                                                                    defaultBindingMode: BindingMode.OneWay,
                                                                    declaringType: typeof(BottomButtons)
                                                                    );
    public String LeftButtonText
    {
        get => (String)GetValue(LeftButtonTextProperty);
        set { SetValue(LeftButtonTextProperty, value); }
    }


    public BindableProperty RightButtonTextProperty = BindableProperty.Create(propertyName: nameof(RightButtonText),
                                                                    returnType: typeof(String),
                                                                    defaultValue: "Save",
                                                                    defaultBindingMode: BindingMode.OneWay,
                                                                    declaringType: typeof(BottomButtons)
                                                                    );

    public String RightButtonText
    {
        get => (String)GetValue(RightButtonTextProperty);
        set { SetValue(RightButtonTextProperty, value); }
    }

    public BindableProperty ClickActionProperty = BindableProperty.Create(propertyName: nameof(ClickAction),
                                                                    returnType: typeof(EventHandler),
                                                                    defaultValue: null,
                                                                    defaultBindingMode: BindingMode.OneWay,
                                                                    declaringType: typeof(BottomButtons)
                                                                    );
    public EventHandler ClickAction
    {
        get => (EventHandler)GetValue(ClickActionProperty);
        set {SetValue(ClickActionProperty, value); }
    }

}
Run Code Online (Sandbox Code Playgroud)

XAML代码:

<Grid xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Resources.Controls.BottomButtons"
             ColumnDefinitions="*,*" Padding="10" ColumnSpacing="20"
             x:Name="this">

    <Button Text="{Binding Source={x:Reference this},Path=RightButtonText}" Grid.Column="1" HorizontalOptions="Fill"></Button>
    <Button Text="{Binding Source={x:Reference this},Path=LeftButtonText}" Grid.Column="0" HorizontalOptions="Fill"></Button>

</Grid>
Run Code Online (Sandbox Code Playgroud)

现在我将自定义控件插入页面中,如下所示:

<?xml version="1.0" encoding="utf-8" ?>  
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
               xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"               
               x:Class="Pages.SeancesSelector"
               xmlns:viewmodel="clr-namespace:ViewModels" xmlns:VerticalStackLayout="clr-namespace:Resources.Controls">

        <!-- Something... -->
        <BottomButtons ClickAction="MyEventHandler"/>  <!-- Custom control inserted here -->
        
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

以及相应的 C# 代码:

public partial class SeancesSelector : ContentPage //Popup
{

    public SeancesSelector()
    {
        InitializeComponent();          
    }       

    public void MyEventHandler(Object Sender, EventArgs E)
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

但这样做之后,我收到编译错误:

找不到“ClickAction”的属性、BindableProperty 或事件,或者值和属性之间的类型不匹配。

我猜想该错误与以下事实有关:为属性 ClickAction 提供的值被解释为字符串而不是 EventHandler 对象。

我该如何解决这个问题?

小智 6

我遇到了同样的问题并通过执行以下操作解决了。

由于事件不是属性,请删除 get 和 set 并添加事件关键字。此外,您还必须按照其他人的说法删除 BindableProperty 。

只需在你的控制代码后面添加:

public event EventHandler ClickAction;
Run Code Online (Sandbox Code Playgroud)

当用户通过按任何按钮与控件交互时,不要忘记通过将以下调用添加到控件后面的代码来引发事件。

ClickAction?.Invoke(this, e);
Run Code Online (Sandbox Code Playgroud)

然后你的页面将监听事件MyEventHandler。

希望能帮助任何人访问此页面。