下午好,我有一个自定义控件,它有两个按钮,我想知道我是否可以将单个单击与实现中的每个单击区分开来。但在那之前,单击仅适用于 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>