博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【WPF】BusyIndicator做Loading遮罩层
阅读量:7109 次
发布时间:2019-06-28

本文共 2929 字,大约阅读时间需要 9 分钟。

百度了一下,粗略看了几个国内野人的做法,花了时间看下去感觉不太好用(比如有Loading居然只是作为窗体的一个局部控件的,没法全屏遮罩,那要你有何用?),于是谷歌找轮子去。

好用的轮子:

引入DLL(在官网或者Nuget里下载)、引入名称空间等步骤根据官方的文档来操作就好了,很简单。

测试如下:

前台代码 MainWindow.xaml:

对应的后台代码 MainWindow.xaml.cs:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Diagnostics;using System.Linq;using System.Net;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace WpfApplication1{    ///     /// Interaction logic for MainWindow.xaml    ///     public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();        }        private void btn_OnClick(object sender, RoutedEventArgs e)        {            BackgroundWorker worker = new BackgroundWorker();            //this is where the long running process should go            worker.DoWork += (o, ea) =>            {                //no direct interaction with the UI is allowed from this method                for (int i = 0; i < 100; i++)                {                    System.Threading.Thread.Sleep(100);                }            };            worker.RunWorkerCompleted += (o, ea) =>            {                //work has completed. you can now interact with the UI                _busyIndicator.IsBusy = false;            };            //set the IsBusy before you start the thread            _busyIndicator.IsBusy = true;            worker.RunWorkerAsync();        }        private void Show(object sender, RoutedEventArgs e)        {            MessageBox.Show("还能点!");        }    }}

运行效果如下:

这里写图片描述

注意点:

  • 想要这个BusyIndicator全屏遮罩,需要把它作为这个Window的直接子节点。而这个BusyIndicator控件自身也只能包含一个子节点,所以可用一个Grid或StackPanel标签做包裹。
  • 官方文档中没有说到BusyIndicator的XMAL全屏遮罩写法,我是看youtube这个小哥这么写的:


2016.12.29下午更新:

发现一个问题,当在worker.DoWork委托中执行的操作需要修改UI时,会发生运行时异常:

Additional information: 该类型的 CollectionView 不支持从调度程序线程以外的线程对其 SourceCollection 进行的更改。

谷歌一下:

这里写图片描述

意思是说,UI是由UI线程创建并维护的,BackgroundWorker对象作为一个新建的子线程,无法跨线程操作UI(这点跟Android很像嘛),想要跨线程操作UI,需要使用Dispatcher分发器机制。所以worker.DoWork的代码修改一下就好了:

worker.DoWork += (o, ea) =>{    //no direct interaction with the UI is allowed from this method    // 不需要操作UI的代码    // ...    // 需要操作UI的代码    App.Current.Dispatcher.Invoke((Action)delegate    {        // 如MVVM中其他Controller、ViewModel中操作UI的方法        designController.Initialize();        webImageViewModel.UpdateImages(designId);    });};

重要参考:

最后一个例子有提及Dispatcher
报错原因:线程的紧密性(亲和性?)

 

你可能感兴趣的文章
黑马程序员--------Java多线程讲解笔记
查看>>
前端性能分析优化
查看>>
即将分拆为独立迈克菲的英特尔安全推出策略和架构产品
查看>>
弃Win7!教你这几招让Windows 10更加顺手好用
查看>>
苹果回应FBI:若因错因开发软件将有害于数百万人
查看>>
互联网+时代的APP医生:云智慧解读APM市场生态
查看>>
索尼PS Networks服务全球范围内宕机
查看>>
SD-WAN助力物联网,让生活更智慧
查看>>
软件测试管理工具—JIRA使用初体验
查看>>
AMD狂打鸡血逼Intel“觉醒” Zen 2架构首曝光
查看>>
成功的MES应用案例需要做好哪些项目前期工作?
查看>>
网络安全从业者的福音:思科1000万美元奖学金计划培养安全人才
查看>>
有关软件测试用例执行的讨论
查看>>
外媒:英特尔巨资收购Mobileye瞄准的是数据
查看>>
如何保证呼叫中心系统的售后服务
查看>>
Facebook免费上网计划印度碰壁
查看>>
一名数据分析师走过的路
查看>>
《深入理解大数据:大数据处理与编程实践》一一第3章 大数据存储——分布式文件系统HDFS...
查看>>
富士康拟在印度“硅谷”班加罗尔设立制造基地
查看>>
武汉智慧城市建设迎来千兆宽带 下载1G电影仅需8秒
查看>>