gpt4 book ai didi

c# - 如何得到 x :name of element in code behind/XAML

转载 作者:行者123 更新时间:2023-11-30 14:05:43 25 4
gpt4 key购买 nike

我在 .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 文件中,我需要获取单击哪个图像以在 switch 中使用:

    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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com