我在 .xaml 中有一些图像,如下所示:

<Image x:Name="image1" Source="image1.png">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="OnTapGestureTap" NumberOfTapsRequired="1" />
</Image.GestureRecognizers>
</Image>

<Image x:Name="image2" Source="image2.png">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="OnTapGestureTap" NumberOfTapsRequired="1" />
</Image.GestureRecognizers>
</Image>

单击时,图像会调用某些操作。现在在 xaml.cs 文件中,我需要获取在 开关 中单击的图像:
    async void OnTapGestureTap(object sender, EventArgs args)
    {
        switch () //--HOW VERIFY IN SWiTCH WHICH IMAGE WAS CLICKED???
        {
            case image1:
                await Navigation.PushAsync(new Image1Page());
                break;
            case image2:
                await Navigation.PushAsync(new Image2Page());
                break;
        }
    }

最佳答案

您可以检索原始图像名称并打开该字符串:

if (sender is Image image)
{
    switch (image.Source as FileImageSource).File)
    {
        case "image1.png":
            Console.WriteLine("image1.png");
            break;
        case "image2.png":
            Console.WriteLine("image2.png");
            break;
    }
}

关于c# - 如何获得 x :name of element in code behind/XAML,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54489076/

10-12 03:55
查看更多