/// <summary> /// 逐级找出子节点。 /// </summary> /// <param name="nodes">当前的节点集合。</param> /// <param name="parents">父节点ID集合。</param> /// <param name="factory">生成节点的函数。</param> /// <param name="index">控制从后向前推进的索引值。</param> public static void Expand <TSource, TValue>(this List <TSource> nodes, List <TValue> parents, Func <TValue, IEnumerable <TSource> > factory, int index) where TSource : ITreeNode <TSource> { TreeNodeExpandScope control = null; if (parents.Count == 0 || index < 0) { return; } if (TreeNodeExpandScope.Current == null) { control = new TreeNodeExpandScope(); } try { foreach (var node in nodes) { if (parents[index].ToStringSafely() == node.Id) { node.Children = factory(node.Id.To <TValue>()).ToList(); node.IsLoaded = true; Expand(node.Children, parents, factory, index - 1); } } } finally { if (control != null) { control.Dispose(); } } }
/// <summary> /// 异步的,逐级找出子节点。 /// </summary> /// <param name="nodes">当前的节点集合。</param> /// <param name="parents">父节点ID集合。</param> /// <param name="nodesCreator">生成节点的函数。</param> /// <param name="index">控制从后向前推进的索引值。</param> public static async Task ExpandAsync <TSource, TValue>(this List <TSource> nodes, List <TValue> parents, Func <TValue, Task <IEnumerable <TSource> > > nodesCreator, int index) where TSource : ITreeNode <TSource> where TValue : IEquatable <TValue> { TreeNodeExpandScope control = null; if (parents.Count == 0 || index < 0) { return; } if (TreeNodeExpandScope.Current == null) { control = new TreeNodeExpandScope(); } try { foreach (var node in nodes) { if (parents[index].Equals((TValue)node.Id)) { node.Children = (await nodesCreator((TValue)node.Id)).ToList(); node.IsLoaded = true; await ExpandAsync(node.Children, parents, nodesCreator, index - 1); } } } finally { if (control != null) { control.Dispose(); } } }