/// <summary>
        /// Find the first child of a specific type.
        /// </summary>
        /// <typeparam name="T">Expected type of the searched child</typeparam>
        /// <param name="view"></param>
        /// <param name="selector">Additional selector for the child</param>
        /// <param name="childLevelLimit">Defines the max depth, null if not limit (Should never be used)</param>
        /// <param name="includeCurrent">Indicates if the current view should also be tested or not.</param>
        /// <returns></returns>
        public static T FindFirstChild <T>(this ViewGroup view, Func <T, bool> selector, int?childLevelLimit = null, bool includeCurrent = true)
            where T : View
        {
            Func <View, bool> childSelector;

            if (selector == null)
            {
                childSelector = child => child is T;
            }
            else
            {
                childSelector = child =>
                {
                    var t = child as T;
                    return(t != null && selector(t));
                };
            }

            if (includeCurrent &&
                childSelector(view))
            {
                return(view as T);
            }

            var maxDepth = childLevelLimit.HasValue
                                ? childLevelLimit.Value
                                : Int32.MaxValue;

            return((T)view.EnumerateAllChildren(childSelector, maxDepth).FirstOrDefault());
        }
Exemple #2
0
        public static T?FindFirstChild <T>(this ViewGroup view, Func <T, bool>?selector, int?childLevelLimit = null, bool includeCurrent = true)
            where T : View
        {
            Func <View?, bool> childSelector;

            if (selector == null)
            {
                childSelector = child => child is T;
            }
            else
            {
                childSelector = child => child is T t && selector(t);
            }

            if (includeCurrent &&
                childSelector(view))
            {
                return(view as T);
            }

            var maxDepth = childLevelLimit ?? int.MaxValue;

            return((T?)view.EnumerateAllChildren(childSelector, maxDepth).FirstOrDefault());
        }