admin管理员组

文章数量:1123188

I have a custom control as follows:

public partial class MyControl : ContentView
{
    public event EventHandler? MyClicked;
    public MyControl()
    {
        InitializeComponent();
        InternalCommand = new Command(
            execute: () =>
            {
                MyClicked?.Invoke(this, EventArgs.Empty);//  breakpoint is added here.
            },
            canExecute: () => true
        );
    }

    public ICommand InternalCommand { get; set; }
}
<ContentView
    ...
    x:Name="This">
    <Button
        Command="{Binding InternalCommand,Source={Reference This}}"
        Text="Click Me" />
</ContentView>

A page can subscribe to the control's clicked event as follows:

public partial class MyPage : ContentPage
{
    public MyPage()
    {
        InitializeComponent();
    }

    private void MyControl_MyClicked(object sender, EventArgs e)
    {
       DisplayAlert("MyControl_MyClicked", "Clicked", "Ok");
    }
}
<ContentPage
    ...
    xmlns:loc="clr-namespace:MyProject"
    >
    <loc:MyControl
        MyClicked="MyControl_MyClicked" />
</ContentPage>

In debug mode the breakpoint never gets reached when I click the button. What is the culprit?

本文标签: mauiWhat causes the ICommand of the ContentView to never be invokedStack Overflow