Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Xamarin Android:循环活动中的所有控件_C#_Xamarin_Xamarin.android_Imageview - Fatal编程技术网

C# Xamarin Android:循环活动中的所有控件

C# Xamarin Android:循环活动中的所有控件,c#,xamarin,xamarin.android,imageview,C#,Xamarin,Xamarin.android,Imageview,我有一个Android应用程序,每当用户点击一个图像(其中有几个图像)时,就会执行一个操作。 我想为这些图片中的每一个设置一个共同的监听器,假设我们有6个。 而不是说 myImg0.Click += new MyImgClick; myImg1.Click += new MyImgClick; 我希望有一种方法可以运行foreach循环来快速设置click事件监听器, 差不多 foreach(img i in Application) { i.Click += new MyImgClic

我有一个Android应用程序,每当用户点击一个图像(其中有几个图像)时,就会执行一个操作。 我想为这些图片中的每一个设置一个共同的监听器,假设我们有6个。 而不是说

myImg0.Click += new MyImgClick;
myImg1.Click += new MyImgClick;
我希望有一种方法可以运行foreach循环来快速设置click事件监听器, 差不多

foreach(img i in Application)
{
   i.Click += new MyImgClick;
}
然后我可以使用事件处理程序,使用“sender”参数将允许我访问单击的单个图像

我试着阅读活动和捆绑课程,但迄今为止还没有发现任何有价值的东西。
这似乎不是一种常见的方法,因为大多数搜索刚刚返回了“设置事件侦听器”的解决方案。

您可以遍历视图层次结构,并使用以下简单的递归方法获取所需的所有图像视图:

private IEnumerable<T> GetViewsByType<T>(ViewGroup root) where T : View
{
    var children = root.ChildCount;
    var views = new List<T>();
    for (var i = 0; i < children; i++)
    {
        var child = root.GetChildAt(i);
        if (child is T myChild)
            views.Add(myChild);
        else if (child is ViewGroup viewGroup)
            views.AddRange(GetViewsByType<T>(viewGroup));
    }
    return views;
}
然后,您可以使用在该根目录上运行该方法,并使用以下方法获取所有
ImageView
s:

var imageViews = GetViewsByType<ImageView>(root);
无论这是不是最好的方法,我都非常怀疑。也许您应该考虑一下使用
RecyclerView
GridView
或其他一些使用和
Adapter
的视图来提高内存效率。这可能是一个更好的解决方案

var imageViews = GetViewsByType<ImageView>(root);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>
    <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>
    <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>
    <LinearLayout
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
      <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/>
        <ImageView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/>
      </LinearLayout>
    </LinearLayout>
  </LinearLayout>
  <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>
  </RelativeLayout>
  <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>
    <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>
    <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>
  </FrameLayout>
</LinearLayout>