Forum Xamarin.Forms

Display image on label click

User_101User_101 Member ✭✭✭

I want to display an image upon clicking on a label. Any help?

I know how to make a label clickable but what would be the approach to display an image thereafter?

Best Answer

  • ColeXColeX Member, Xamarin Team Xamurai
    Accepted Answer

    Add TapGestureRecognizer on the Label , and manipulate image's IsVisible to true in the tap event.

    Xaml

    <ContentPage.Content>
        <StackLayout>
            <Label Text="Welcome to Xamarin.Forms!"
                VerticalOptions="CenterAndExpand" 
                HorizontalOptions="CenterAndExpand" >
                <Label.GestureRecognizers>
                    <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
                </Label.GestureRecognizers>
            </Label>
            <Image x:Name="image" Source="dog2.png" IsVisible="False"/>
        </StackLayout>
    </ContentPage.Content>
    

    Code

    private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
        {
            image.IsVisible = true;
        }
    

Answers

  • VIVEKNEGIVIVEKNEGI USMember ✭✭✭

    set IsVisible property of the image.

  • User_101User_101 Member ✭✭✭
    edited December 2019

    This is what I have tried:

    xaml:

     <Label Text="Click Me to display Image"
               VerticalOptions="Center"
               HorizontalOptions="Center"
               x:Name="imgClick ">
     </Label>
    <Image x:Name="imgDisp"> </Image>
    

    .cs:

     public void imgClickEvent()
    {
         imgClick.GestureRecognizers.Add(new TapGestureRecognizer()
            {
                Command = new Command(() =>
                {
                    imgDisp.Source = "sampleimage.jpg";
                                    })          
            });
     }
    
  • ColeXColeX Member, Xamarin Team Xamurai
    Accepted Answer

    Add TapGestureRecognizer on the Label , and manipulate image's IsVisible to true in the tap event.

    Xaml

    <ContentPage.Content>
        <StackLayout>
            <Label Text="Welcome to Xamarin.Forms!"
                VerticalOptions="CenterAndExpand" 
                HorizontalOptions="CenterAndExpand" >
                <Label.GestureRecognizers>
                    <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
                </Label.GestureRecognizers>
            </Label>
            <Image x:Name="image" Source="dog2.png" IsVisible="False"/>
        </StackLayout>
    </ContentPage.Content>
    

    Code

    private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
        {
            image.IsVisible = true;
        }
    
Sign In or Register to comment.