wpf-转载-以绘制gif的方式提示用户程序正在加载

参考:https://www.cnblogs.com/miketwais/articles/WPF_Loading.html


效果是一个gif
在这里插入图片描述

用户控件LoadingWait

LoadingWait.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Windows.Threading;

namespace SnapDemo
{
    /// <summary>
    /// LoadingWait.xaml 的交互逻辑
    /// </summary>
    public partial class LoadingWait : UserControl
    {
        #region Data  
        private readonly DispatcherTimer animationTimer;
        #endregion

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

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

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

        private void Stop()
        {
            animationTimer.Stop();
            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

    }
}

LoadingWait.xaml

<UserControl x:Class="SnapDemo.LoadingWait"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:SnapDemo"
             Width="100"
             Height="100"
             IsVisibleChanged="HandleVisibleChanged">
    <UserControl.Background>
        <SolidColorBrush Color="Transparent" Opacity="0.2"  />
    </UserControl.Background>
    <UserControl.Resources>
        <SolidColorBrush Color="#FF007BE5" x:Key="CirclesColor" />
    </UserControl.Resources>

    <Viewbox Width="100" Height="100"    
            HorizontalAlignment="Center"    
            VerticalAlignment="Center">
        <Grid x:Name="LayoutRoot"     
                Background="Transparent"    
                ToolTip="请稍后"    
                HorizontalAlignment="Center"    
                VerticalAlignment="Center">
            <TextBlock Text="加载中"  HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="20" Foreground="Red" FontWeight="Bold" />
            <Canvas RenderTransformOrigin="0.5,0.5"    
                    HorizontalAlignment="Center"    
                    VerticalAlignment="Center" Width="120"    
                    Height="120" Loaded="HandleLoaded"    
                    Unloaded="HandleUnloaded"  >
                <Ellipse x:Name="C0" Width="20" Height="20"    
                         Canvas.Left="0"    
                         Canvas.Top="0" Stretch="Fill"    
                         Fill="{StaticResource CirclesColor}" Opacity="1.0"/>
                <Ellipse x:Name="C1" Width="20" Height="20"    
                         Canvas.Left="0"    
                         Canvas.Top="0" Stretch="Fill"    
                         Fill="{StaticResource CirclesColor}" Opacity="0.9"/>
                <Ellipse x:Name="C2" Width="20" Height="20"    
                         Canvas.Left="0"    
                         Canvas.Top="0" Stretch="Fill"    
                         Fill="{StaticResource CirclesColor}" Opacity="0.8"/>
                <Ellipse x:Name="C3" Width="20" Height="20"    
                         Canvas.Left="0"    
                         Canvas.Top="0" Stretch="Fill"    
                         Fill="{StaticResource CirclesColor}" Opacity="0.7"/>
                <Ellipse x:Name="C4" Width="20" Height="20"    
                         Canvas.Left="0"    
                         Canvas.Top="0" Stretch="Fill"    
                         Fill="{StaticResource CirclesColor}" Opacity="0.6"/>
                <Ellipse x:Name="C5" Width="20" Height="20"    
                         Canvas.Left="0"    
                         Canvas.Top="0" Stretch="Fill"    
                         Fill="{StaticResource CirclesColor}" Opacity="0.5"/>
                <Ellipse x:Name="C6" Width="20" Height="20"    
                         Canvas.Left="0"    
                         Canvas.Top="0" Stretch="Fill"    
                         Fill="{StaticResource CirclesColor}" Opacity="0.4"/>
                <Ellipse x:Name="C7" Width="20" Height="20"    
                         Canvas.Left="0"    
                         Canvas.Top="0" Stretch="Fill"    
                         Fill="{StaticResource CirclesColor}" Opacity="0.3"/>
                <Ellipse x:Name="C8" Width="20" Height="20"    
                         Canvas.Left="0"    
                         Canvas.Top="0" Stretch="Fill"    
                         Fill="{StaticResource CirclesColor}" Opacity="0.2"/>
                <Canvas.RenderTransform>
                    <RotateTransform x:Name="SpinnerRotate"    
                         Angle="0" />
                </Canvas.RenderTransform>
            </Canvas>
        </Grid>
    </Viewbox>
</UserControl>

使用

前台:

<!-- 放在需要显示的位置 -->
<local:LoadingWait x:Name="_loading"  Visibility="Collapsed"/>

后台:

this._loading.Visibility = Visibility.Visible;
//...需要长时间执行的任务
this._loading.Visibility = Visibility.Collapsed;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值