简体   繁体   中英

Is there a way to simulate the clicking of a button?

I'm using this code to create something looking like 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>

Is there a way that I can simulate the click event to make it look like something actually got pressed when they click on the label?

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

faveIconLabel.GestureRecognizers.Add(new TapGestureRecognizer((view) => OnLabelClicked()));

If you don't want to use a custom renderer, a simple way would be to change the background color in the handleClick , and then revert it back to its original color after a few milliseconds. For example,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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