You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin开发中如何获取父控件的所有子控件?

Xamarin.Forms 获取父控件全部子控件的实现方式

方式1:通用递归扩展方法(适用所有布局类型)

这是最常用的落地实现,通过扩展方法实现全嵌套层级的子控件遍历,支持按控件类型过滤:

using System.Collections.Generic;
using System.Linq;
using Xamarin.Forms;

public static class VisualElementExtensions
{
    // 支持按指定类型过滤子控件的重载
    public static IEnumerable<T> GetAllDescendants<T>(this VisualElement parent) where T : VisualElement
    {
        if (parent == null) yield break;
        
        // 匹配当前层级的目标类型控件
        if (parent is T target)
        {
            yield return target;
        }

        // 遍历子元素递归查找
        var children = parent is Layout layout ? layout.Children : new List<Element>();
        foreach (var child in children.OfType<VisualElement>())
        {
            foreach (var descendant in child.GetAllDescendants<T>())
            {
                yield return descendant;
            }
        }
    }

    // 无需过滤类型,返回所有子控件的重载
    public static IEnumerable<VisualElement> GetAllDescendants(this VisualElement parent)
    {
        return parent.GetAllDescendants<VisualElement>();
    }
}

使用示例:

  • 获取当前页面所有Button控件:var allButtons = this.Content.GetAllDescendants<Button>().ToList();
  • 获取指定StackLayout下所有层级的子控件:var allChildren = targetStackLayout.GetAllDescendants().ToList();

注意:该方法会遍历所有嵌套层级的控件,包括Grid、Frame、ScrollView内部的嵌套控件

方式2:获取直接子控件(非递归,仅当前层级)

如果不需要深层嵌套的子控件,仅需要父控件的第一级子元素,可以直接调用布局的Children属性,性能优于递归方案:

if (targetParent is Layout layout)
{
    // 仅获取父控件的直接子元素,不包含嵌套层级
    var directChildren = layout.Children.ToList();
}

特殊场景适配

  • ScrollView/ContentView/Frame 类单内容容器:如果要获取容器内部的所有控件,先取Content属性再调用递归方法即可:var innerChildren = myScrollView.Content.GetAllDescendants().ToList();
  • ListView/CollectionView 列表类控件:不要用上述方法遍历列表项内部控件,列表存在复用机制,直接遍历会得到错误结果,建议通过绑定数据源处理列表项逻辑。

内容的提问来源于stack exchange,提问作者Eir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 17:54:03