猿问

如何获取代码隐藏/ XAML中元素的x:名称

我在.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;

        }

    }


浮云间
浏览 180回答 5
5回答

慕婉清6462132

您可以检索原始映像名称并打开该字符串:if (sender is Image image){&nbsp; &nbsp; switch (image.Source as FileImageSource).File)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; case "image1.png":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("image1.png");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case "image2.png":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("image2.png");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }}

慕容708150

如果你真的把心思放在开关上,这可能会起作用。假设发件人实际上是一个,并且名称将是Imagex:Name更新尝试将发件人与图像本身进行比较&nbsp;if(sender == image1)&nbsp; &nbsp; &nbsp; ...源语言async void OnTapGestureTap(object sender, EventArgs args){&nbsp; &nbsp;if(sender is Image image)&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; switch (image.Name) // switch on the name&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case "image1":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await Navigation.PushAsync(new Image1Page());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case "image2":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await Navigation.PushAsync(new Image2Page());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;}}注意:这是完全未经测试的

白板的微信

您可以使用 Image 中的 ClassId 属性<Image ClassId="image1" Source="image1.png">&nbsp; &nbsp;<Image.GestureRecognizers>&nbsp; &nbsp; &nbsp; <TapGestureRecognizer Tapped="OnTapGestureTap" NumberOfTapsRequired="1" />&nbsp; &nbsp;</Image.GestureRecognizers></Image><Image ClassId="image2" Source="image2.png">&nbsp; &nbsp;<Image.GestureRecognizers>&nbsp; &nbsp; &nbsp; <TapGestureRecognizer Tapped="OnTapGestureTap" NumberOfTapsRequired="1" />&nbsp; &nbsp;</Image.GestureRecognizers></Image>在后台只需切换 ClassIdasync void OnTapGestureTap(object sender, EventArgs args){&nbsp; &nbsp; var image = sender as Image;&nbsp; &nbsp; switch (image.ClassId)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; case "image1":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await Navigation.PushAsync(new Image1Page());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case "image2":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await Navigation.PushAsync(new Image2Page());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }}

牧羊人nacy

如果未指定,则此属性将作为字符串继承该值。StyleIDx:Namex:Name是由 XAML 生成的字段的名称。在这种情况下,有效答案是if (sender == image1){&nbsp; &nbsp; await Navigation.PushAsync(new Image1Page());}else if (sender == image2)&nbsp;{&nbsp; &nbsp; await Navigation.PushAsync(new Image2Page());}您可以获取带有下划线并带有错误消息的字段名称:名称“image1”在当前上下文中不存在。但是上下文将在 XAML 加载时创建。

慕田峪9158850

试试这个&nbsp;async void OnTapGestureTap(object sender, EventArgs args){&nbsp;&nbsp; var image = sender as Image;&nbsp; &nbsp; switch (image)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; case image1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await Navigation.PushAsync(new Image1Page());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case image2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await Navigation.PushAsync(new Image2Page());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答