简体   繁体   中英

Image not getting the Class ID - Xamarin Forms

I have several images in my XAML as follows (for this example I included two images).

    <StackLayout Orientation="Horizontal" HorizontalOptions="Fill" Padding="0,5,0,5">
    <Image  x:Name="Image1" ClassId="{Binding Image1}" Source="tickoff.png" HeightRequest="18">
        <Image.GestureRecognizers>
            <TapGestureRecognizer
                Tapped="SelectedImage"
                NumberOfTapsRequired="1" />
        </Image.GestureRecognizers>
    </Image>
    <Label Text="Asian Foods" FontSize="16" HorizontalOptions="Center" Margin="5,0,0,0" 
        TextColor="{StaticResource greyishBrown}"/>
</StackLayout>

StackLayout Orientation="Horizontal" HorizontalOptions="Fill" Padding="0,5,0,5">
    <Image  x:Name="Image2" ClassId="{Binding Image2}" Source="tickoff.png" HeightRequest="18">
        <Image.GestureRecognizers>
            <TapGestureRecognizer
                    Tapped="SelectedImage"
                    NumberOfTapsRequired="1" />
            </Image.GestureRecognizers>
    </Image>
    <Label Text="Japanese Foods" FontSize="16" HorizontalOptions="Center" Margin="5,0,0,0" 
         TextColor="{StaticResource greyishBrown}"/>
</StackLayout>

I want to check what image has been clicked by the user. Fort this I'm using ClassId. I have included ClassIds in all images. This has a single method in code behind and I'm accessing classId from there.

But I'm not getting the Id when user clicks an image. I'm new to C# and Xamarin Forms. Could someone help me.

public void SelectedImage(object sender, EventArgs eventArgs)
{
        Image image = (Image)sender;
        string imageString = image.ClassId;

        DisplayAlert("Test", imageSender.ClassId,"OK");
        //TODO

}

XAML

 <StackLayout Orientation="Horizontal" HorizontalOptions="Fill" Padding="0,5,0,5">
                <Image  x:Name="Image1" ClassId="Image1" Source="tickoff.png" HeightRequest="18">
                    <Image.GestureRecognizers>
                        <TapGestureRecognizer
                Tapped="SelectedImage"
                NumberOfTapsRequired="1" />
                    </Image.GestureRecognizers>
                </Image>
                <Label Text="Asian Foods" FontSize="16" HorizontalOptions="Center" Margin="5,0,0,0" TextColor="{StaticResource greyishBrown}" />
            </StackLayout>

            <StackLayout Orientation="Horizontal" HorizontalOptions="Fill" Padding="0,5,0,5">
            <Image  x:Name="Image2"  ClassId="Image2" Source="tickoff.png" HeightRequest="18">
                <Image.GestureRecognizers>
                    <TapGestureRecognizer
                    Tapped="SelectedImage"
                    NumberOfTapsRequired="1" />
                </Image.GestureRecognizers>
            </Image>
            <Label Text="Japanese Foods" FontSize="16" HorizontalOptions="Center" Margin="5,0,0,0" TextColor="{StaticResource greyishBrown}"
         />
        </StackLayout>

C#

public void SelectedImage(object sender, EventArgs eventArgs)
{
        Image image = (Image)sender;
        string imageString = image.ClassId;

        DisplayAlert("Test", imageString ,"OK");
        //TODO

}

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