Alan2
Alan2

Reputation: 24592

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?

Upvotes: 0

Views: 324

Answers (2)

sme
sme

Reputation: 4163

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

Upvotes: 2

Idev Dev
Idev Dev

Reputation: 184

You can add a TapGestureRecognizer to virtually any VisualElement, including Label, Image, etc.

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

Upvotes: 2

Related Questions