WPF加载等待动画

简介: 原文:WPF加载等待动画 原文地址:https://www.codeproject.com/Articles/57984/WPF-Loading-Wait-Adorner 界面遮罩 等待动画全局颜色 ...

原文地址:https://www.codeproject.com/Articles/57984/WPF-Loading-Wait-Adorner


界面遮罩

    
        
    

等待动画全局颜色

 
        
    

等待动画中的小圆

 

    
        
    
    
        
    
    
        
            
                
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                        
                    
                
            
        
        
    

后台业务代码,添加了几项属性、动画控制、小圆的位置设置

    /// 
    /// LoadingWait.xaml 的交互逻辑
    /// 
    public partial class LoadingWait : UserControl
    {
        #region Data
        private readonly DispatcherTimer animationTimer;

        public int TextSize
        {
            get { return (int)GetValue(TextSizeProperty); }
            set { SetValue(TextSizeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TextSize.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextSizeProperty =
            DependencyProperty.Register("TextSize", typeof(int), typeof(LoadingWait), new PropertyMetadata(
                defaultValue: 24,
                propertyChangedCallback: new PropertyChangedCallback(
                    (sender, e) => {
                        var loading = sender as LoadingWait;
                        if(loading != null) {
                            loading.TextControl.FontSize = (int)e.NewValue;
                        }
                    })
                ));


        public Color TextColor
        {
            get { return (Color)GetValue(TextColorProperty); }
            set { SetValue(TextColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TextColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextColorProperty =
            DependencyProperty.Register("TextColor", typeof(Color), typeof(LoadingWait), new PropertyMetadata(
                defaultValue: Colors.Black,
                propertyChangedCallback: new PropertyChangedCallback(
                    (sender, e) => {
                        var loading = sender as LoadingWait;
                        if(loading != null) {
                            loading.TextControl.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString(e.NewValue.ToString()));
                        }
                    }),
                coerceValueCallback: new CoerceValueCallback((sender, e) => {
                    LoadingWait loading = (LoadingWait)sender;
                    try {
                        return (Color)ColorConverter.ConvertFromString(e.ToString());
                    }
                    catch(Exception ex) {
                        return Colors.Black;
                    }
                })
                ));



        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(LoadingWait), new PropertyMetadata(
                defaultValue: string.Empty,
                propertyChangedCallback: new PropertyChangedCallback(
                    (sender, e) => {
                        var loading = sender as LoadingWait;
                        if(loading != null) {
                            loading.TextControl.Text = e.NewValue.ToString();
                        }
                    })
                ));


        public string Tip
        {
            get { return (string)GetValue(TipProperty); }
            set { SetValue(TipProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Tip.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TipProperty =
            DependencyProperty.Register("Tip", typeof(string), typeof(LoadingWait), new PropertyMetadata(
                defaultValue: string.Empty,
                propertyChangedCallback: new PropertyChangedCallback(
                    (sender, e) => {
                        var loading = sender as LoadingWait;
                        if(loading != null) {
                            loading.LayoutRoot.ToolTip = e.NewValue;
                        }
                    })
                ));

        #endregion

        #region Constructor
        public LoadingWait() {
            InitializeComponent();

            animationTimer = new DispatcherTimer(
                DispatcherPriority.ContextIdle, Dispatcher);
            animationTimer.Interval = new TimeSpan(0, 0, 0, 0, 75);
        }
        #endregion

        #region Private Methods
        private void Start() {
            Mouse.OverrideCursor = Cursors.Wait;
            animationTimer.Tick += HandleAnimationTick;
            animationTimer.Start();
        }

        private void Stop() {
            animationTimer.Stop();
            Mouse.OverrideCursor = Cursors.Arrow;
            animationTimer.Tick -= HandleAnimationTick;
        }

        private void HandleAnimationTick(object sender, EventArgs e) {
            SpinnerRotate.Angle = (SpinnerRotate.Angle + 36) % 360;
        }

        private void HandleLoaded(object sender, RoutedEventArgs e) {
            const double offset = Math.PI;
            const double step = Math.PI * 2 / 10.0;

            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);
        }

        //设置动画中小圆的位置
        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);
        }

        private void HandleUnloaded(object sender, RoutedEventArgs e) {
            Stop();
        }

        //显示控件就启动动画,隐藏(不显示)就停止动画
        private void HandleVisibleChanged(object sender,
            DependencyPropertyChangedEventArgs e) {
            bool isVisible = (bool)e.NewValue;

            if(isVisible)
                Start();
            else
                Stop();
        }
        #endregion
    }
目录
相关文章
|
安全 编译器 C语言
【C语言】typeof 关键字详解
`typeof` 关键字在GCC中用于获取表达式的类型,便于动态类型定义和宏编程。它可以用于简化代码、提高代码的灵活性和可维护性。虽然 `typeof` 是 GCC 扩展,并非标准C的一部分,但它在实际编程中非常有用。
755 1
|
前端开发 JavaScript UED
React 滚动条组件 Scrollbar
本文介绍了在 React 中创建和使用滚动条组件的方法。首先,通过设置 `overflow: auto` 等 CSS 属性实现基础滚动功能。接着,推荐了如 `react-custom-scrollbars` 和 `react-perfect-scrollbar` 等第三方库以增强滚动条的功能与外观。针对常见问题,如样式不一致、无法正常工作及性能瓶颈,提供了相应的解决方案,包括自定义样式、确保容器高度和优化渲染逻辑。最后,强调了避免滚动事件绑定错误和不合理嵌套滚动的重要性,帮助开发者打造流畅、美观的用户界面。
794 16
|
JavaScript 前端开发
JavaScript实现手写签名,可触屏手写,支持移动端与PC端双端保存
JavaScript实现手写签名,可触屏手写,支持移动端与PC端双端保存
679 0
|
存储 搜索推荐 数据建模
阿里巴巴大数据实践之数据建模:构建企业级数据湖
阿里巴巴通过构建高效的数据湖和实施先进的数据建模策略,实现了数据驱动的业务增长。这些实践不仅提升了内部运营效率,也为客户提供了更好的服务体验。随着数据量的不断增长和技术的不断创新,阿里巴巴将持续优化其数据建模方法,以适应未来的变化和发展。
|
数据库
解决numpy.core._exceptions.UFuncTypeError: ufunc ‘add‘ did not contain a loop with signature matching
解决numpy.core._exceptions.UFuncTypeError: ufunc ‘add‘ did not contain a loop with signature matching
1662 0
解决numpy.core._exceptions.UFuncTypeError: ufunc ‘add‘ did not contain a loop with signature matching
|
算法 API C#
Winform控件优化之圆角按钮【各种实现中的推荐做法】(下)
最终优化实现ButtonPro按钮(继承自Button),既提供Button原生功能,又提供扩展功能,除了圆角以外,还实现了圆形、圆角矩形的脚尖效果、边框大小和颜色、背景渐变颜色...
3765 0
Winform控件优化之圆角按钮【各种实现中的推荐做法】(下)
|
数据采集 监控 物联网
IOT/智能设备日志解决方案(1):概述
无论是物联网还是智能设备,规模都越来越大,产业分工也越来越明确,逐渐形成一整套的生态系统。而同时无论是物联网还是智能设备的生态系统中,日志数据永远是不可缺少的一个重要环节。
6283 0
IOT/智能设备日志解决方案(1):概述
|
安全 网络协议 网络安全
在Windows7搭建FTP服务器详细教学
在Windows7搭建FTP服务器详细教学
2096 0
|
缓存 数据可视化 C#
WPF技术之Frame控件
WPF (Windows Presentation Foundation) Frame是WPF中的一个控件,它在应用程序中提供了一个容器,用于加载和显示其他页面或用户界面元素。
2145 0
|
存储 传感器 缓存
让QSPI FLASH(W25Q64)支持Fatfs文件系统
让QSPI FLASH(W25Q64)支持Fatfs文件系统
951 0