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 有没有办法模拟按钮的点击?_Xamarin_Xamarin.forms - Fatal编程技术网

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