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)