/// <summary> /// 10万级别UI线程直接加载方式 //目前是100万个1.5秒左右 /// </summary> /// <param name="orgs"></param> private void LoadOrgsSync(List <NodeInfoFromDB> orgs) { var dd = TreeUtil.ImageRescource; List <TreeNodeModel> roots = new List <TreeNodeModel>(); var orgDicts = orgs.ToDictionary(c => c.Index, c => c); foreach (var org in orgs) { //获取org对应节点 TreeNodeModel orgNode = GetOrCreateCacheOrgsNode(org); //获取父节点 //1、判断当前节点是否为根节点(根节点的父节点组织信息是不存在的 if (!orgDicts.ContainsKey(org.ParentIndex)) //org为根节点 { roots.Add(orgNode); //记录根节点。 //可以在此处直接添加到树控件中 continue; } //2、创建父节点 TreeNodeModel orgParentNode = GetOrCreateCacheOrgsNode(orgDicts[org.ParentIndex]); //3、添加到父节点 orgParentNode.Nodes.Add(orgNode); } //添加到树控件种 _tv.ItemsSource = new ObservableCollection <TreeNodeModel>(roots); }
/// <summary> /// 批量加载组织节点(一次性添加所有子节点) ,已经按父节点分好组了!!!) 复杂度 O(n) /// </summary> /// <param name="orgs"></param> private void LoadOrgsWayBatchAsync(List <NodeInfoFromDB> orgs) { //获取根节点列表 List <TreeNodeModel> roots = new List <TreeNodeModel>(); var tempDicts = orgs.GroupBy(g => g.ParentIndex).ToDictionary(g => g.Key, g => g.ToList());// var orgDicts = orgs.ToDictionary(g => g.Index, g => g); foreach (var item in tempDicts.Keys) { if (!orgDicts.ContainsKey(item)) { foreach (var rt in tempDicts[item]) { roots.Add(GetOrCreateCacheOrgsNode(rt)); } } } //添加到根 _tv.ItemsSource = new ObservableCollection <TreeNodeModel>(roots); Task.Run(() => { //tempDicts的Values中的所有集合对应了所有的组织信息 foreach (var item in tempDicts) //Keys中的根节点的父节点是不存在的。 { if (!orgDicts.ContainsKey(item.Key)) //Key为根的父节点,直接pass { continue; } //Key对应的组织节点 TreeNodeModel orgParentNode = GetOrCreateCacheOrgsNode(orgDicts[item.Key]); var tempOrgs = new List <TreeNodeModel>(); foreach (var info in item.Value)//Value中的子节点,此处可以改成批量添加*************** { TreeNodeModel node = GetOrCreateCacheOrgsNode(info); tempOrgs.Add(node); node.ParentNode = orgParentNode; } _tv.Dispatcher.Invoke(() => { tempOrgs.ForEach(c => orgParentNode.Nodes.Add(c)); }); } }); }