如何解决如何区分点击自定义控件
下午好,我有一个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>
-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),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>
解决方法
我有一个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>
然后创建两个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",propertyChanged:ImageButton2Propertychanged);
private static void ImageButton2Propertychanged(BindableObject bindable,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");
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。