首页 > 代码库 > WPF 利用BackgroundWorker 动态加载UI
WPF 利用BackgroundWorker 动态加载UI
BackgroundWorker bw = new BackgroundWorker(); bw.DoWork += (ee, se) => { //TODO 需要异步请求的操作 }; bw.RunWorkerCompleted += (ew, sw) => { Dispatcher.Invoke(new Action(() => { <span style="white-space:pre"> </span>//TODO 动态生成UI })); }; bw.RunWorkerAsync();
Demo:
List<ResourceIndex> CourseWareList = new List<ResourceIndex>();//课件 List<ResourceIndex> TeachingPlanList = new List<ResourceIndex>();//教案 List<ResourceIndex> ExercisesList = new List<ResourceIndex>();//习题 List<ResourceIndex> MaterialList = new List<ResourceIndex>();//素材 List<ResourceIndex> resList = new List<ResourceIndex>(); string bookCode = EBookContext.Instance.CurrentBook.IndexInfo.Code; string unitCode = EBookContext.Instance.CurrentUnit.UnitCode; BackgroundWorker bw = new BackgroundWorker(); bw.DoWork += (ee, se) => { resList = SearchResource(bookCode, unitCode); foreach (ResourceIndex resIndex in resList) { switch (resIndex.Usage) { case ResourceUsage.Courseware: CourseWareList.Add(resIndex); break; case ResourceUsage.TechingDesign: TeachingPlanList.Add(resIndex); break; case ResourceUsage.TestQuestion: ExercisesList.Add(resIndex); break; case ResourceUsage.TeachMaterial: MaterialList.Add(resIndex); break; default: break; } } }; bw.RunWorkerCompleted += (ew, sw) => { Dispatcher.Invoke(new Action(() => { ClearResource(); this.onLoading.Visibility = Visibility.Collapsed; if (CourseWareList.Count > 0) { ResDetailContainer resc1 = new ResDetailContainer("课件", "#FF48B95A", CourseWareList); this.resContainer.Children.Add(resc1); } if (TeachingPlanList.Count > 0) { ResDetailContainer resc2 = new ResDetailContainer("教案", "#FFFB5454", TeachingPlanList); this.resContainer.Children.Add(resc2); } if (ExercisesList.Count > 0) { ResDetailContainer resc3 = new ResDetailContainer("习题", "#FF3E80DA", ExercisesList); this.resContainer.Children.Add(resc3); } if (MaterialList.Count > 0) { ResDetailContainer resc4 = new ResDetailContainer("素材", "#FFE7B01B", MaterialList); this.resContainer.Children.Add(resc4); } if (resList.Count == 0) { this.noRes.Visibility = Visibility.Visible; } })); }; bw.RunWorkerAsync();
WPF 利用BackgroundWorker 动态加载UI
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。