繁体   English   中英

有没有一种模拟按钮单击的方法?

[英]Is there a way to simulate the clicking of a button?

我正在使用以下代码创建类似于按钮的内容:

 <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>

有没有一种方法可以模拟click事件,使其看起来像当他们单击标签时实际按下的东西?

您可以将TapGestureRecognizer几乎添加到任何VisualElement中,包括Label,Image等。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM