0

下午好,我有一个自定义控件,它有两个按钮,我想知道我是否可以将单个单击与实现中的每个单击区分开来。但在那之前,单击仅适用于 stackLayout,并且按钮没有触发命令

-我的 HeaderPage.xaml

<ContentView.Content>
    <Grid ColumnDefinitions="10*, 80*, 10*">
        <ImageButton Source="back" HeightRequest="40" WidthRequest="40" BackgroundColor="LightSkyBlue"/>
        
        <Label Text="Folha de Pagamento" Grid.Column="1" BackgroundColor="LightCoral"
               VerticalTextAlignment="Center" HorizontalTextAlignment="Center"/>

        <ImageButton Source="more" HeightRequest="30" WidthRequest="40" BackgroundColor="LightSkyBlue"
                     Grid.Column="2"/>
    </Grid>
</ContentView.Content>

-HeaderPage.xaml.cs

public partial class HeaderPage : ContentView
{
    public event EventHandler Clicked;
    public HeaderPage()
    {
        InitializeComponent();
        CriarComando();
    }

    private void CriarComando()
    {
        this.GestureRecognizers.Add(new TapGestureRecognizer
        {
            Command = new Command(() =>
            {
                //if (frame.IsVisible)
                //{
                Clicked?.Invoke(this, EventArgs.Empty);

                if (Command != null)
                {
                    if (Command.CanExecute(CommandParameter))
                        Command.Execute(CommandParameter);
                }
                //}
            })
        });
    }

    #region Command
    public static readonly BindableProperty CommandProperty = BindableProperty.Create
        (
            propertyName: "Command",
            typeof(ICommand),
            typeof(HeaderPage),
            null
        );

    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }
    #endregion

    #region CommandParameter
    public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create
        (
            propertyName: "CommandParameter",
            typeof(object),
            typeof(HeaderPage),
            null
        );

    public object CommandParameter
    {
        get { return (object)GetValue(CommandParameterProperty); }
        set { SetValue(CommandParameterProperty, value); }
    }
    #endregion
}

-控制实施

<ContentPage.Content>
    <StackLayout>
        <local:HeaderPage Command="{Binding MyCommand}"
                          CommandParameter="1"/>
    </StackLayout>
</ContentPage.Content>
4

1 回答 1

0

我有一个自定义控件,它有两个按钮,我想知道我是否可以将单个单击与实现中的每个单击区分开来。

你的意思是你创建自定义控件,有两个imageButton,点击两个imagebutton时触发不同的方法,对吗?

如果是,请查看以下代码:

自定义控件:

 <ContentView.Content>
    <StackLayout>
        <ImageButton
            x:Name="button1"
            Command="{Binding ImageButton1}"
            HeightRequest="40"
            Source="check.png"
            WidthRequest="100" />
        <Label Text="test" />
        <ImageButton
            x:Name="button2"
            Command="{Binding ImageButton2}"
            HeightRequest="40"
            Source="icaon.png"
            WidthRequest="100" />
    </StackLayout>
</ContentView.Content>

然后创建两个 BindableProperty 命令进行绑定:

public partial class View1 : ContentView
{

    public static readonly BindableProperty ImageButton1Property = BindableProperty.Create("ImageButton1", typeof(ICommand), typeof(View1), null,propertyChanged:ImageButton1Propertychanged
        );

    private static void ImageButton1Propertychanged(BindableObject bindable, object oldValue, object newValue)
    {
        var control = (View1)bindable;
        control.button1.Command = (System.Windows.Input.ICommand)newValue;
    }

    public ICommand ImageButton1
    {
        get { return (ICommand)GetValue(ImageButton1Property); }
        set { SetValue(ImageButton1Property, value); }
    }

    public static readonly BindableProperty ImageButton2Property = BindableProperty.Create("ImageButton2", typeof(ICommand), typeof(View1), null,propertyChanged:ImageButton2Propertychanged);

    private static void ImageButton2Propertychanged(BindableObject bindable, object oldValue, object newValue)
    {
        var control = (View1)bindable;
        control.button2.Command = (System.Windows.Input.ICommand)newValue;
    }

    public ICommand ImageButton2
    {
        get { return (ICommand)GetValue(ImageButton2Property); }
        set { SetValue(ImageButton2Property, value); }
    }
    public View1()
    {
        InitializeComponent();
    }
}

最后,在 Page 中使用这个自定义控件。

 <customcontrol:View1 ImageButton1="{Binding image1}" ImageButton2="{Binding image2}" />

 public partial class Page2 : ContentPage
{
   
    public Command image1 { get; set; }

    public Command image2 { get; set; }
    public Page2()
    {
        InitializeComponent();

       
        image1 = new Command(imagebutton1);
        image2 = new Command(imagebutton2);
        

        this.BindingContext = this;

       
    }
    private void imagebutton1()
    {
        Console.WriteLine("the imagebutton1 click");
    }
    private void imagebutton2()
    {
        Console.WriteLine("the imagebutton2 click");
    }
于 2020-10-26T06:12:07.037 回答