Xamarin 有没有办法模拟按钮的点击?
我正在使用此代码创建类似于按钮的东西:Xamarin 有没有办法模拟按钮的点击?,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我正在使用此代码创建类似于按钮的东西: <Grid Grid.Column="0" ColumnSpacing="0" Margin="0,0,10,0"> <Frame Grid.Column="0" CornerRadius="5" OutlineColor="Black"> <Label x:Name="faveIconLabel" Style="{StaticResource mediumIcon}" Margin="0,2,0,0
<Grid Grid.Column="0" ColumnSpacing="0" Margin="0,0,10,0">
<Frame Grid.Column="0" CornerRadius="5" OutlineColor="Black">
<Label x:Name="faveIconLabel" Style="{StaticResource mediumIcon}" Margin="0,2,0,0" HorizontalOptions="Fill" FontFamily="FontAwesome" VerticalOptions="Center" VerticalTextAlignment="Center" />
</Frame>
<Frame Grid.Column="1" CornerRadius="5" OutlineColor="Black">
<Label x:Name="hiddenIconLabel" Style="{StaticResource mediumIcon}" Margin="0,2,0,0" HorizontalOptions="Fill" FontFamily="FontAwesome" VerticalOptions="Center" VerticalTextAlignment="Center" />
</Frame>
</Grid>
是否有一种方法可以模拟单击事件,使其看起来像他们单击标签时实际按下的东西?您可以将TapGestureRecognitor添加到几乎任何VisualElement,包括标签、图像等
faveIconLabel.GestureRecognizers.Add(new TapGestureRecognizer((view) => OnLabelClicked()));
您可以将TapGestureRecognitor添加到几乎任何VisualElement,包括标签、图像等
faveIconLabel.GestureRecognizers.Add(new TapGestureRecognizer((view) => OnLabelClicked()));
如果不想使用自定义渲染器,一种简单的方法是更改
handleClick
中的背景色,然后在几毫秒后将其还原为原始颜色。比如说,
private void handleClick(object sender, EventArgs e) {
var view = (View)sender;
view.BackgroundColor = Color.FromHex("#DD000000");
Device.StartTimer(
TimeSpan.FromMilliseconds(100),
() =>
{
// Revert it back to the original color, whatever it may be.
Device.BeginInvokeOnMainThread(() =>
{
view.BackgroundColor = Color.Transparent;
});
return false; // return false to prevent the timer from calling again
});
}
如果不想使用自定义渲染器,一种简单的方法是更改
handleClick
中的背景色,然后在几毫秒后将其还原为原始颜色。比如说,
private void handleClick(object sender, EventArgs e) {
var view = (View)sender;
view.BackgroundColor = Color.FromHex("#DD000000");
Device.StartTimer(
TimeSpan.FromMilliseconds(100),
() =>
{
// Revert it back to the original color, whatever it may be.
Device.BeginInvokeOnMainThread(() =>
{
view.BackgroundColor = Color.Transparent;
});
return false; // return false to prevent the timer from calling again
});
}