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

时间:2019-02-02 01:12:12

标签: c# xaml xamarin xamarin.forms

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

5 个答案:

答案 0 :(得分:4)

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

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;
    }
}

答案 1 :(得分:2)

如果您确实将心脏放在开关上,则可能会起作用。假设发件人实际上是Image,名称将是x:Name

更新

尝试将发件人与图片本身进行比较

 if(sender == image1)
      ...

原始

async void OnTapGestureTap(object sender, EventArgs args)
{
   if(sender is Image image)
   {
      switch (image.Name) // switch on the name
      {
         case "image1":
            await Navigation.PushAsync(new Image1Page());
            break;
         case "image2":
            await Navigation.PushAsync(new Image2Page());
            break;
      }
   }
}

注意 :这完全未经测试

答案 2 :(得分:1)

您可以使用Image中的 ClassId 属性

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

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

在后台只需切换 ClassId

async void OnTapGestureTap(object sender, EventArgs args)
{
    var image = sender as Image;
    switch (image.ClassId)
    {
        case "image1":
            await Navigation.PushAsync(new Image1Page());
            break;
        case "image2":
            await Navigation.PushAsync(new Image2Page());
            break;
    }
}

答案 3 :(得分:0)

尝试

 async void OnTapGestureTap(object sender, EventArgs args)
{ 
  var image = sender as Image;

    switch (image) 
    {
        case image1:
            await Navigation.PushAsync(new Image1Page());
            break;
        case image2:
            await Navigation.PushAsync(new Image2Page());
            break;
    }
}

答案 4 :(得分:0)

如果未指定.a,则此属性将StyleID值作为字符串继承。

x:Name是XAML生成的字段的名称。在这种情况下,有效答案是

x:Name

您可以获得带错误消息加下划线的字段名称:

名称“ image1”在当前上下文中不存在。

但是上下文将在XAML加载时创建。