Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin 如何区分单击customcontrol_Xamarin_Xamarin.forms - Fatal编程技术网

Xamarin 如何区分单击customcontrol

Xamarin 如何区分单击customcontrol,xamarin,xamarin.forms,Xamarin,Xamarin.forms,下午好,我有一个customcontrol,它有两个按钮,我想知道我是否有可能在实现中区分单个单击和每个单击。但是在此之前,单击只在stackLayout上起作用,按钮不会触发命令 -我的HeaderPage.xaml <ContentView.Content> <Grid ColumnDefinitions="10*, 80*, 10*"> <ImageButton Source="back" Hei

下午好,我有一个customcontrol,它有两个按钮,我想知道我是否有可能在实现中区分单个单击和每个单击。但是在此之前,单击只在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>
-控制实现

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

我有一个customcontrol,它有两个按钮,我想知道我是否可以区分实现中的每个单击

你的意思是你创建了自定义控件,有两个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>
最后,在页面中使用此自定义控件

 <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");
    }

公共部分类第2页:内容页
{
公共命令image1{get;set;}
公共命令image2{get;set;}
公共页2()
{
初始化组件();
image1=新命令(imagebutton1);
image2=新命令(imagebutton2);
this.BindingContext=this;
}
私有void imagebutton1()
{
Console.WriteLine(“图像按钮1单击”);
}
私有void imagebutton2()
{
Console.WriteLine(“图像按钮2点击”);
}

每个按钮都有自己的点击/命令行为。如果您想识别每个按钮,为什么要在整个控件中添加一个点击手势?我承认我不知道如何正确操作,您能帮我吗?一个具有类似要求和问题的相关问题,请参阅问题和答案,了解如何正确操作修改你的控件:你传递给控件的命令在标题xaml中分配到哪里?@Gabriel Ribeiro Rossi如果我的回答解决了你的问题,请记住将其标记为答案,这对其他面临相同问题的人是有益的,谢谢。
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();
    }
}
 <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");
    }