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

好用的轮子:http://wpftoolkit.codeplex.com/wikipage?title=BusyIndicator

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

测试如下:
前台代码 MainWindow.xaml:

<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"> <!-- 如有需要,可以引入样式资源 -->
<!--<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Style/LoadingMaskStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>--> <StackPanel VerticalAlignment="Center"> <xctk:BusyIndicator x:Name="_busyIndicator" IsBusy="False"
BusyContent="少女祈祷中..." Background="Red">
<!--<xctk:BusyIndicator.OverlayStyle>
<Style TargetType="Rectangle">
<Setter Property="Fill" Value="#ffffeeee"/>
</Style>
</xctk:BusyIndicator.OverlayStyle>-->
<!--<xctk:BusyIndicator.ProgressBarStyle>
<Style TargetType="ProgressBar">
<Setter Property="Visibility" Value="Collapsed"/>
</Style>
</xctk:BusyIndicator.ProgressBarStyle>--> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Button Click="Show" Content="LLLL" Height="40" Width="60"/>
<Button Click="Show" Content="LLLL" Height="40" Width="60"/>
<Button x:Name="btn" Click="btn_OnClick" Content="测试" Height="40" Width="60"/>
<Button Click="Show" Content="RRRR" Height="40" Width="60"/>
<Button Click="Show" Content="RRRR" Height="40" Width="60"/>
</StackPanel>
</xctk:BusyIndicator>
</StackPanel>
</Window>

对应的后台代码 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
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
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这个小哥这么写的:

https://www.youtube.com/watch?v=1h_mLA-eE40


2016.12.29下午更新:

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

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

谷歌一下:
http://stackoverflow.com/questions/18331723/this-type-of-collectionview-does-not-support-changes-to-its-sourcecollection-fro

意思是说,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);
});
};

重要参考:

https://elegantcode.com/2009/07/03/wpf-multithreading-using-the-backgroundworker-and-reporting-the-progress-to-the-ui/
https://elegantcode.com/2011/10/07/extended-wpf-toolkitusing-the-busyindicator/ 最后一个例子有提及Dispatcher
https://vkishorekumar.wordpress.com/2011/07/18/what-is-thread-affinity/ 报错原因:线程的紧密性(亲和性?)

【WPF】BusyIndicator做Loading遮罩层的更多相关文章

  1. WPF loading遮罩层 LoadingMask

    原文:WPF loading遮罩层 LoadingMask 大家可能很纠结在异步query数据的时候想在wpf程序中显示一个loading的遮罩吧 今天就为大家介绍下遮罩的制作 源码下载 点击此处 先 ...

  2. js实现的简单遮罩层

    超级简单的一个实现,可能会有局限性,贵在简单易懂,使用的时候执行前loading,执行成功后loaded /* * 显示loading遮罩层 */ function loading() { var m ...

  3. JS实现遮罩层

    /* * 显示loading遮罩层 */ function loading() { var mask_bg = document.createElement("div"); mas ...

  4. js 遮罩层 loading 效果

    //调用方法 //关闭事件<button onclick='LayerHide()'>关闭</button>,在loadDiv(text)中,剔除出来 //调用LayerSho ...

  5. C# Winform 实现自定义半透明loading加载遮罩层

    在网页中通过div+css实现半透明效果不难,今天我们看看一种在winfrom中实现的方法: 效果图如下,正常时: 显示遮罩层时: 自定义遮罩层控件的源码如下: View Row Code 1 usi ...

  6. Android UI设计--半透明效果对话框及activity(可做遮罩层)

    下面是style的一些属性及其解释 <style name="dialog_translucent" parent="@android:style/Theme.Di ...

  7. Android自定义遮罩层设计

    在做网页设计时,前端设计人员会经常用到基于JS开发的遮罩层,并且背景半透明.这样的效果怎么样在Android上实现呢?这个实现并不困难,先来上效果图: <ignore_js_op> 201 ...

  8. html+css源码之实现登录弹出框遮罩层效果

    在web开发中,很多网站都做了一些特别炫丽的效果,比如用户登录弹框遮罩层效果,本文章向大家介绍css如何实现登录弹出框遮罩层效果,需要的朋友可以参考一下本文章的源代码. html+css实现登录弹出框 ...

  9. div+css遮罩层

    曾被问到这个问题,不知所措,后来在网上找到了.大神文章:http://www.cnblogs.com/aspx-net/archive/2011/03/11/1981071.html 我想实现的效果没 ...

随机推荐

  1. 微信小程序-简易计算器

    代码地址如下:http://www.demodashi.com/demo/14210.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...

  2. result-charts

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  3. 搭建Hexo博客并部署到Github

    参考: http://www.jianshu.com/p/a67792d93682 http://jingyan.baidu.com/article/d8072ac47aca0fec95cefd2d. ...

  4. Ways to 优化JAVA程序设计和编码,提高JAVA性能

    通过使用一些辅助性工具来找到程序中的瓶颈,然后就可以对瓶颈部分的代码进行优化.一般有两种方案:即优化代码或更改设计方法.我们一般会选择后者,因为不去调用以下代码要比调用一些优化的代码更能提高程序的性能 ...

  5. 浅析iOS tableview的selectRowAtIndexPath选中无效(默认选中cell无效)

    可能很多人都遇到过这种情况: tableview列表,有时加载完,需要默认选中某一行,给予选中效果:或者需要执行某行的点击事件. 我们举例: 比如我想默认选中第一行 可能我们第一个想法就是这样: [m ...

  6. centos7安装MySQL5.7无法设置密码问题

    前言 在使用centos7系统yum方式安装MySQL5.7后 不知道默认密码是多少  知道后没办法修改? 一.找到MySQL密码 service mysqld start vim /var/log/ ...

  7. Concurrency Managed Workqueue(二)CMWQ概述

    一.前言 一种新的机制出现的原因往往是为了解决实际的问题,虽然linux kernel中已经提供了workqueue的机制,那么为何还要引入cmwq呢?也就是说:旧的workqueue机制存在什么样的 ...

  8. jenkins job构建后汇总结果到同一个文本文档中去

    jenkins 构建后,执行下shell脚本,把结果汇总到同一个文件中,这样多个job构建后的结果可以在一个文件中展示 result_all_dir="/app/jenkins_result ...

  9. ubuntu中pip安装redis-py及pip的使用

    安装redis-py的前提是已经将redis成功安装,redis安装过程请看博文 ubuntu14安装redis 1.安装pip sudo apt-get install python-pip 2.使 ...

  10. OpenGl学习 glenable()函数理解

    glEnable用于启用各种功能.功能由参数决定.与glDisable相对应.glDisable是用来关闭的.两个函数参数取值是一至的. 参数说明:void glEnable(GLenum cap)G ...