WPF加载动画

页面代码如下

XML 复制代码
<UserControl x:Class="ShangHaiElectricalLine.Controls.LoadingAnimation"
             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:ShangHaiElectricalLine.Controls"
             mc:Ignorable="d"
             d:DesignHeight="200"
             d:DesignWidth="200"
             Background="Transparent">

    <UserControl.Resources>
        <!-- 不需要定义转换器资源,可以直接在绑定中使用 -->
    </UserControl.Resources>

    <Grid>
        <!-- 毛玻璃效果 -->
        <Border Background="#80FFFFFF"
                CornerRadius="20"
                Width="260"
                Height="200"
                VerticalAlignment="Center"
                HorizontalAlignment="Center">
            <Border.Effect>
                <BlurEffect Radius="8" />
            </Border.Effect>
        </Border>

        <Border Background="White"
                CornerRadius="16"
                Width="250"
                Height="190"
                VerticalAlignment="Center"
                HorizontalAlignment="Center"
                BorderThickness="1"
                BorderBrush="#E3F2FD">

            <StackPanel VerticalAlignment="Center"
                        HorizontalAlignment="Center"
                        Margin="20">

                <!-- 渐变环形加载 -->
                <Viewbox Width="70"
                         Height="70">
                    <Grid>
                        <!-- 背景环 -->
                        <Ellipse Stroke="#E3F2FD"
                                 StrokeThickness="4" />

                        <!-- 动态环 -->
                        <Ellipse x:Name="LoadingRing"
                                 Stroke="#2196F3"
                                 StrokeThickness="4"
                                 StrokeDashArray="0.5 1.5"
                                 StrokeDashCap="Round"
                                 StrokeDashOffset="2">
                            <Ellipse.RenderTransform>
                                <RotateTransform CenterX="35"
                                                 CenterY="35" />
                            </Ellipse.RenderTransform>
                            <Ellipse.Triggers>
                                <EventTrigger RoutedEvent="Loaded">
                                    <BeginStoryboard>
                                        <Storyboard RepeatBehavior="Forever">
                                            <!-- 旋转动画 -->
                                            <DoubleAnimation Storyboard.TargetName="LoadingRing"
                                                             Storyboard.TargetProperty="(Ellipse.RenderTransform).(RotateTransform.Angle)"
                                                             From="0"
                                                             To="360"
                                                             Duration="0:0:1.2" />
                                            <!-- 线头动画 -->
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="LoadingRing"
                                                                           Storyboard.TargetProperty="StrokeDashOffset">
                                                <LinearDoubleKeyFrame KeyTime="0:0:0"
                                                                      Value="0" />
                                                <LinearDoubleKeyFrame KeyTime="0:0:1.2"
                                                                      Value="3" />
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger>
                            </Ellipse.Triggers>
                        </Ellipse>
                    </Grid>
                </Viewbox>

                <!-- 加载文字 -->
                <TextBlock Text="{Binding LoadingText, RelativeSource={RelativeSource AncestorType=UserControl}}"
                           Margin="0,20,0,0"
                           Foreground="#2196F3"
                           FontSize="16"
                           FontWeight="SemiBold"
                           HorizontalAlignment="Center"
                           TextAlignment="Center">
                    <TextBlock.Effect>
                        <DropShadowEffect BlurRadius="2"
                                          Opacity="0.3"
                                          ShadowDepth="1" />
                    </TextBlock.Effect>
                </TextBlock>
            </StackPanel>
        </Border>
    </Grid>
</UserControl>

对应的.cs文件

cs 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
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;

namespace ShangHaiElectricalLine.Controls
{
    /// <summary>
    /// LoadingAnimation.xaml 的交互逻辑
    /// </summary>
    public partial class LoadingAnimation : UserControl, INotifyPropertyChanged
    {
        public LoadingAnimation()
        {
            InitializeComponent();
        }

        #region 依赖属性

        // 是否激活加载动画
        public static readonly DependencyProperty IsActiveProperty =
            DependencyProperty.Register("IsActive", typeof(bool), typeof(LoadingAnimation),
                new PropertyMetadata(false, OnIsActiveChanged));

        public bool IsActive
        {
            get => (bool)GetValue(IsActiveProperty);
            set => SetValue(IsActiveProperty, value);
        }

        private static void OnIsActiveChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is LoadingAnimation control)
            {
                control.OnPropertyChanged(nameof(IsActive));
            }
        }

        // 加载文本
        public static readonly DependencyProperty LoadingTextProperty =
            DependencyProperty.Register("LoadingText", typeof(string), typeof(LoadingAnimation),
                new PropertyMetadata("正在加载...", OnLoadingTextChanged));

        public string LoadingText
        {
            get => (string)GetValue(LoadingTextProperty);
            set => SetValue(LoadingTextProperty, value);
        }

        private static void OnLoadingTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is LoadingAnimation control)
            {
                control.OnPropertyChanged(nameof(LoadingText));
            }
        }

        #endregion

        #region INotifyPropertyChanged 实现

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}

如何使用

1.在资源中使用

<!-- 添加转换器资源 -->
<converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />

cs 复制代码
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace ShangHaiElectricalLine.Converters
{
    public class BooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (value is bool boolValue && boolValue) ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

2. 在View中加入这个样式

XML 复制代码
<!-- 加载遮罩层样式 -->
  <Style x:Key="LoadingOverlayStyle"
         TargetType="Border">
      <Setter Property="Background">
          <Setter.Value>
              <LinearGradientBrush StartPoint="0,0"
                                   EndPoint="1,1">
                  <GradientStop Color="#1A0D47A1"
                                Offset="0" />
                  <GradientStop Color="#800D47A1"
                                Offset="1" />
              </LinearGradientBrush>
          </Setter.Value>
      </Setter>
      <Setter Property="Visibility"
              Value="Collapsed" />
      <Style.Triggers>
          <DataTrigger Binding="{Binding IsLoading}"
                       Value="True">
              <Setter Property="Visibility"
                      Value="Visible" />
          </DataTrigger>
      </Style.Triggers>
  </Style>

在ViewModel中加入这两个属性

cs 复制代码
[ObservableProperty]
private bool _isLoading;

[ObservableProperty]
private string _loadingText = "正在加载页面...";

3.使用自定义样式

XML 复制代码
 <!-- 加载遮罩层(在最顶层) -->
 <Border Style="{StaticResource LoadingOverlayStyle}"
         Panel.ZIndex="9999">
     <Grid>
         <!-- 使用自定义加载控件 -->
         <controls:LoadingAnimation 
             IsActive="True"
             LoadingText="{Binding LoadingText}"
             HorizontalAlignment="Center"
             VerticalAlignment="Center" />
     </Grid>
 </Border>

效果如下

相关推荐
xiaobai1781 小时前
pytest+playwright实现UI自动化(4)-上夹具fixture
ui·自动化·pytest·playwright
为何创造硅基生物3 小时前
LVGL 妙用 LV_OBJ_FLAG_FLOATING
ui
ANnianStriver4 小时前
PetLumina 04 — 管理后台 UI 全面升级
java·ui·ai编程
ANnianStriver6 小时前
PetLumina 05 — App 端 UI 效果应用
java·ui·ai编程
小小龙学IT6 小时前
Midscene.js:AI驱动的跨平台UI自动化革命
javascript·人工智能·ui
Wenzar_7 小时前
Playwright 实战:高可信 UI 回归验证流水线
java·ui
文创工作室1 天前
Adobe Illustrator 中文
ui·adobe·illustrator
烛衔溟1 天前
HarmonyOS 基础 UI 构建 —— 组件、布局与沉浸式效果
ui·华为·harmonyos
稷下元歌1 天前
七天学会plc加机器视觉之AI 接入 外设模块开发全详细操作文档(全程配套视频按文档实操)
python·sql·qt·贪心算法·r语言·wpf·时序数据库
一次旅行1 天前
CopilotKit实战:用生成式UI打造智能Agent前端
前端·人工智能·ui