一直想实现这么个功能来着,所以去网上搜了资料,复杂的看不懂,后来挑了一个最简单的,复用了这位大神的很多代码(大神看到了别打脸)。这位大神是用UserControl,使用时则是调用用户控件中的方法。之前有用过Telerik的RadBusyIndicator,感觉很好,它是将要遮罩部分直接以内容的形式包裹在RadBusyIndicator的Content属性中,使用时就更简单了,IsBusy=true时遮层罩开启,IsBusy=false遮层罩关闭/隐藏。一为了实现起来不复杂,二为了实现RadBusyIndicator的用法,实现自己的自定义控件,所以才有了这篇文章,先上效果图

接下来先上自定义控件前台代码:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyBusyIndicator">
    
    <SolidColorBrush Color="#FF007BE5" x:Key="CirclesColor" />
    <Style TargetType="{x:Type local:BusyIndicator}">
        <Setter Property="Background" Value="#FFFFC0CB"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:BusyIndicator}">
                    <Grid>
                        <ContentControl x:Name="PART_Content" Content="{TemplateBinding Content}" />
                        <Border x:Name="PART_Border" Visibility="Hidden" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"
                                Opacity="0.5">
                            
                        </Border>
                        <Grid x:Name="PART_LayoutRoot" 
                Background="Transparent"    
                ToolTip="Please wait...."    
                HorizontalAlignment="Center"    
                VerticalAlignment="Center" Visibility="Hidden">
 
                            <TextBlock Text="Loading..."  HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="14" Foreground="#FFE3953D" FontWeight="Bold" />
                            <Canvas RenderTransformOrigin="0.5,0.5" x:Name="PART_Canvas"
                    HorizontalAlignment="Center"    
                    VerticalAlignment="Center" Width="120"    
                    Height="120">
                                <Ellipse x:Name="PART_C0" Width="20" Height="20"   
                         Canvas.Left="0"    
                         Canvas.Top="0" Stretch="Fill"    
                         Fill="{StaticResource CirclesColor}" Opacity="1.0"/>
                                <Ellipse x:Name="PART_C1" Width="20" Height="20"   
                         Canvas.Left="0"    
                         Canvas.Top="0" Stretch="Fill"    
                         Fill="{StaticResource CirclesColor}" Opacity="0.9"/>
                                <Ellipse x:Name="PART_C2" Width="20" Height="20"   
                         Canvas.Left="0"    
                         Canvas.Top="0" Stretch="Fill"    
                         Fill="{StaticResource CirclesColor}" Opacity="0.8"/>
                                <Ellipse x:Name="PART_C3" Width="20" Height="20"   
                         Canvas.Left="0"    
                         Canvas.Top="0" Stretch="Fill"    
                         Fill="{StaticResource CirclesColor}" Opacity="0.7"/>
                                <Ellipse x:Name="PART_C4" Width="20" Height="20"   
                         Canvas.Left="0"    
                         Canvas.Top="0" Stretch="Fill"    
                         Fill="{StaticResource CirclesColor}" Opacity="0.6"/>
                                <Ellipse x:Name="PART_C5" Width="20" Height="20"   
                         Canvas.Left="0"    
                         Canvas.Top="0" Stretch="Fill"    
                         Fill="{StaticResource CirclesColor}" Opacity="0.5"/>
                                <Ellipse x:Name="PART_C6" Width="20" Height="20"   
                         Canvas.Left="0"    
                         Canvas.Top="0" Stretch="Fill"    
                         Fill="{StaticResource CirclesColor}" Opacity="0.4"/>
                                <Ellipse x:Name="PART_C7" Width="20" Height="20"   
                         Canvas.Left="0"    
                         Canvas.Top="0" Stretch="Fill"    
                         Fill="{StaticResource CirclesColor}" Opacity="0.3"/>
                                <Ellipse x:Name="PART_C8" Width="20" Height="20"   
                         Canvas.Left="0"    
                         Canvas.Top="0" Stretch="Fill"    
                         Fill="{StaticResource CirclesColor}" Opacity="0.2"/>
                                <Canvas.RenderTransform>
                                    <RotateTransform x:Name="PART_SpinnerRotate"   
                         Angle="0" />
                                </Canvas.RenderTransform>
                            </Canvas>
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

下面的是自定义控件的后台逻辑和依赖属性:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Windows.Threading;
 
namespace MyBusyIndicator
{
    /// <summary>
    /// 按照步骤 1a 或 1b 操作,然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
    ///
    /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根 
    /// 元素中: 
    ///
    ///     xmlns:MyNamespace="clr-namespace:MyBusyIndicator"
    ///
    ///
    /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根 
    /// 元素中: 
    ///
    ///     xmlns:MyNamespace="clr-namespace:MyBusyIndicator;assembly=MyBusyIndicator"
    ///
    /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用,
    /// 并重新生成以避免编译错误: 
    ///
    ///     在解决方案资源管理器中右击目标项目,然后依次单击
    ///     “添加引用”->“项目”->[浏览查找并选择此项目]
    ///
    ///
    /// 步骤 2)
    /// 继续操作并在 XAML 文件中使用控件。
    ///
    ///     <MyNamespace:BusyIndicator/>
    ///
    /// </summary>
    public class BusyIndicator : Control
    {
        private readonly DispatcherTimer animationTimer;
 
        public static readonly DependencyProperty IsBusyProperty;
 
        public static readonly DependencyProperty ContentProperty;
        static BusyIndicator()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(BusyIndicator), new FrameworkPropertyMetadata(typeof(BusyIndicator)));
 
            IsBusyProperty = DependencyProperty.Register("IsBusy", typeof(bool), typeof(BusyIndicator), new PropertyMetadata(false, new PropertyChangedCallback(OnBusyChanged)));
 
            ContentProperty = DependencyProperty.Register("Content", typeof(object), typeof(BusyIndicator));
        }
 
        public bool IsBusy
        {
            get { return (bool)GetValue(IsBusyProperty); }
            set { SetValue(IsBusyProperty, value); }
        }
 
        public object Content 
        {
            get { return GetValue(ContentProperty); }
            set { SetValue(ContentProperty, value); }
        }
 
        private static void OnBusyChanged(DependencyObject sender,DependencyPropertyChangedEventArgs e)
        {
            BusyIndicator indicator = sender as BusyIndicator;
            if ((bool)e.NewValue == true)
            {
                indicator.Start();
            }
            else
            {
                indicator.Stop();
            }
        }
 
        public BusyIndicator()
        {
            animationTimer = new DispatcherTimer(
                DispatcherPriority.ContextIdle, Dispatcher);
            animationTimer.Interval = new TimeSpan(0, 0, 0, 0, 90);
        }
        #region Private Methods
        private void Start()
        {
            if (this.IsInitialized)
            {
                Border border = GetTemplateChild("PART_Border") as Border;
                border.Visibility = Visibility.Visible;
                Grid grid = GetTemplateChild("PART_LayoutRoot") as Grid;
                grid.Visibility = Visibility.Visible;
                animationTimer.Tick += HandleAnimationTick;
                animationTimer.Start();
            }
            
        }
 
        private void Stop()
        {
            if (this.IsInitialized)
            {
                Border border = GetTemplateChild("PART_Border") as Border;
                Grid grid = GetTemplateChild("PART_LayoutRoot") as Grid;
                border.Visibility = Visibility.Hidden;
                grid.Visibility = Visibility.Hidden;
                animationTimer.Stop();
                animationTimer.Tick -= HandleAnimationTick;
            }
        }
 
        private void HandleAnimationTick(object sender, EventArgs e)
        {
            RotateTransform SpinnerRotate = GetTemplateChild("PART_SpinnerRotate") as RotateTransform;
            SpinnerRotate.Angle = (SpinnerRotate.Angle + 36) % 360;
        }
 
        private void SetPosition(Ellipse ellipse, double offset,
            double posOffSet, double step)
        {
            ellipse.SetValue(Canvas.LeftProperty, 50.0
                + Math.Sin(offset + posOffSet * step) * 50.0);
 
            ellipse.SetValue(Canvas.TopProperty, 50
                + Math.Cos(offset + posOffSet * step) * 50.0);
        }
 
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
 
            const double offset = Math.PI;
            const double step = Math.PI * 2 / 10.0;
 
            Ellipse C0 = GetTemplateChild("PART_C0") as Ellipse;
            Ellipse C1 = GetTemplateChild("PART_C1") as Ellipse;
            Ellipse C2 = GetTemplateChild("PART_C2") as Ellipse;
            Ellipse C3 = GetTemplateChild("PART_C3") as Ellipse;
            Ellipse C4 = GetTemplateChild("PART_C4") as Ellipse;
            Ellipse C5 = GetTemplateChild("PART_C5") as Ellipse;
            Ellipse C6 = GetTemplateChild("PART_C6") as Ellipse;
            Ellipse C7 = GetTemplateChild("PART_C7") as Ellipse;
            Ellipse C8 = GetTemplateChild("PART_C8") as Ellipse;
 
            SetPosition(C0, offset, 0.0, step);
            SetPosition(C1, offset, 1.0, step);
            SetPosition(C2, offset, 2.0, step);
            SetPosition(C3, offset, 3.0, step);
            SetPosition(C4, offset, 4.0, step);
            SetPosition(C5, offset, 5.0, step);
            SetPosition(C6, offset, 6.0, step);
            SetPosition(C7, offset, 7.0, step);
            SetPosition(C8, offset, 8.0, step);
 
            if (this.IsBusy)
            {
                this.Start();
            }
            else
            {
                this.Stop();
            }
        }
 
        #endregion   
    }
}

以上便是全部控件的代码了,为了能够使用它,我将他单独编译成了一个dll,下面是如何使用该控件的例子,前台代码如下:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:CustomControls"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
        xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="CustomControls.MainWindow"
        xmlns:my="clr-namespace:MyBusyIndicator;assembly=MyBusyIndicator"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <my:BusyIndicator x:Name="busy">
            <my:BusyIndicator.Content>
                <Grid>
                    <DataGrid>
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="第一列"/>
                            <DataGridTextColumn Header="第二列"/>
                            <DataGridTextColumn Header="第三列"/>
                        </DataGrid.Columns>
                    </DataGrid>
                </Grid>
            </my:BusyIndicator.Content>
        </my:BusyIndicator>
        <Button Content="Button" HorizontalAlignment="Right"  VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    </Grid>
</Window>

记得注意引用的命名空间和程序集,下面是后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 CustomControls
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
 
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            busy.IsBusy = !busy.IsBusy;
        }
 
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            busy.IsBusy = true;
        }
    }
}

OK,全部代码都已上。简单的说一句,不怎么会使用CSDN,不知道怎么插入代码,所以只好选择全部复制,一个很简单的控件。

WPFLoading遮层罩的更多相关文章

  1. JQuery插件:遮罩+数据加载中。。。(特点:遮你想遮,罩你想罩)

    在很多项目中都会涉及到数据加载.数据加载有时可能会是2-3秒,为了给一个友好的提示,一般都会给一个[数据加载中...]的提示.今天就做了一个这样的提示框. 先去jQuery官网看看怎么写jQuery插 ...

  2. js弹出遮层

    <script> var docEle = function () { return document.getElementById(arguments[0]) || false; } f ...

  3. WPF 蒙层罩,正在加载

    参考园子里的一篇文章,比较好用.可以直接用,可以自己改. 动画效果: 容器的触发器,旋转容器: 属性配置:使用依赖属性,并且在xaml中写绑定.

  4. 《Spring 3.x 企业应用开发实战》目录

    图书信息:陈雄华 林开雄 编著 ISBN 978-7-121-15213-9 概述: 第1章:对Spring框架进行宏观性的概述,力图使读者建立起对Spring整体性的认识. 第2章:通过一个简单的例 ...

  5. VS中的 MD/MT设置 【转】

    VS系列工具作为目前微软主打的集成开发环境,在历经了近20多年的发展后,到如今已经可以 说是Windows平台上各种IDE环境中的翘楚了.很多别的开发工具已经难望其项背了,如今VS2010也已经面市很 ...

  6. 纯CSS实现柱形图

    CSS在处理排版之强大,没有做不到,只有想不到.下面我们将一同实现一个柱状图. 先打好一个具体的框架.我们利用无序列表做整体,里面的东西我们根本选择内联无素span,strong,em来填充. < ...

  7. Metronic 使用到的开源插件汇总

    Metronic 是一套完整的 UI 模板,但不仅仅是模板,更应该说是一个 UI 框架.它除了提供了大量网页模板,也提供了非常多的 UI 组件,并且应用了众多 jQuery 插件.通过这些资源的整合, ...

  8. 150个JS特效脚本

    收集了其它一些不太方便归类的JS特效,共150个,供君查阅. 1. simplyScroll simplyScroll这个jQuery插件能够让任意一组元素产生滚动动画效果,可以是自动.手动滚动,水平 ...

  9. Android刮刮卡自定义控件

    网上的都是自己绘制的或者图片,我的需求是可以随意的自定义底部和顶部的布局.所以自己重写一个,原理就是直接继承 View 来实现一个刮层,让这个刮层和图片以及文字不产生任何依赖,再结合 FrameLay ...

随机推荐

  1. LR中判断HTTP返回状态

    有天,有个人问:我在做b/s测试,请问如何保存从服务器传回来的http头的信息,怎么能得到http状态,和状态200进行比较? 后来,我给出的代码如下: Action() {int i; // [WC ...

  2. onWindowFocusChanged重要作用(得到/失去焦点call) 、

    onWindowFocusChanged重要作用 Activity生命周期中,onStart, onResume, onCreate都不是真正visible的时间点,真正的visible时间点是onW ...

  3. 【DB2】NVL2函数

    语法: NVL2(表达式1,表达式2,表达式3) 如果表达式1为空,返回值为表达式3的值.如果表达式1不为空,返回值为表达式2的值. 例子: SELECT ID, NVL2(SEX,'非空','空值' ...

  4. jq的form验证

    jQuery(document).ready(function(){ $('#cform img.contact-loader').hide(); $('#cform').submit(functio ...

  5. 如何把HTML标记分类

    p.h1.或div等元素常常称为块级元素,这些元素显示为一块内容:Strong,span等元素称为行内元素,它们的内容显示在行中,即“行内框”.(可以使用display=block将行内元素转换成块元 ...

  6. golang使用sqlite

    安装问题 在import sqlite的时候,golang build 出现以下错误, exec: "gcc": executable file not found in %PAT ...

  7. 工作总结 .ToString("000000")

    ; ");//000123 指定格式 Console.WriteLine(ssp); ; ");//123456789 超过了返回原值 Console.WriteLine(ss);

  8. Java 装饰模式(4.4)

    装饰模式(decorator pattern). 依照Num模型.讨论职业/IProfession类层次. IProfession定义了方法say(String),事实上现类教师/ Teacher.医 ...

  9. chrome 浏览器设置字体大小,方便调试

    H5开发的时候,有时候明明字体没有那么大,但在chrome中看上去依然很大.就像型号不对一样,但在手机端是正常的. 这是因为chrome浏览器设置了默认字体.只需要设置一下即可.  「chrome:/ ...

  10. laravel中,提交表单后给出提示例如添加成功,添加失败等等

    laravel中的表单插入,我想在表单插入成功后,可以像thinkphp一样可以有一个提示内容,上网Google,他们还是给出的方法就是 return redirect('/')->with(' ...