这个效果图...直接放上吧,实际是转圈效果,使用起来最方便的一种。
【这是个基础版,灵活度很高】
- Xaml
绘制Loading图案,及触发的动画效果,实际控制的每个组件 Opacity - 透明度 属性。
<Style TargetType="{x:Type local:LoadingControl}">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Focusable" Value="False"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:LoadingControl}">
<Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" VerticalAlignment="Center" >
<Grid.Resources>
<DrawingBrush x:Key="brush" Stretch="None" AlignmentX="Center" AlignmentY="Top">
<DrawingBrush.Drawing>
<GeometryDrawing Brush="Red">
<GeometryDrawing.Geometry>
<EllipseGeometry RadiusX="{Binding RadiusX, RelativeSource={RelativeSource TemplatedParent}}"
RadiusY="{Binding RadiusY, RelativeSource={RelativeSource TemplatedParent}}"/>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingBrush.Drawing>
</DrawingBrush>
</Grid.Resources>
<!--绘制图案-->
<Rectangle x:Name="r01" Fill="{StaticResource brush}" Opacity="0.3" RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<RotateTransform Angle="0"/>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="r02" Fill="{StaticResource brush}" Opacity="0.3" RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<RotateTransform Angle="30"/>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="r03" Fill="{StaticResource brush}" Opacity="0.3" RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<RotateTransform Angle="60"/>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="r04" Fill="{StaticResource brush}" Opacity="0.3" RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<RotateTransform Angle="90"/>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="r05" Fill="{StaticResource brush}" Opacity="0.3" RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<RotateTransform Angle="120"/>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="r06" Fill="{StaticResource brush}" Opacity="0.3" RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<RotateTransform Angle="150"/>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="r07" Fill="{StaticResource brush}" Opacity="0.3" RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<RotateTransform Angle="180"/>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="r08" Fill="{StaticResource brush}" Opacity="0.3" RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<RotateTransform Angle="210"/>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="r09" Fill="{StaticResource brush}" Opacity="0.3" RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<RotateTransform Angle="240"/>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="r10" Fill="{StaticResource brush}" Opacity="0.3" RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<RotateTransform Angle="270"/>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="r11" Fill="{StaticResource brush}" Opacity="0.3" RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<RotateTransform Angle="300"/>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle x:Name="r12" Fill="{StaticResource brush}" Opacity="0.3" RenderTransformOrigin="0.5,0.5">
<Rectangle.RenderTransform>
<RotateTransform Angle="330"/>
</Rectangle.RenderTransform>
</Rectangle>
<Grid.Triggers>
<!--动画效果 可以放别的 事件 或 属性 触发-->
<EventTrigger RoutedEvent="Grid.Loaded">
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever">
<DoubleAnimation Storyboard.TargetName="r01" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:0.39333" BeginTime="0:0:0.00000" To="1"/>
<DoubleAnimation Storyboard.TargetName="r02" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:0.39333" BeginTime="0:0:0.08333" To="1"/>
<DoubleAnimation Storyboard.TargetName="r03" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:0.39333" BeginTime="0:0:0.16666" To="1"/>
<DoubleAnimation Storyboard.TargetName="r04" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:0.39333" BeginTime="0:0:0.24999" To="1"/>
<DoubleAnimation Storyboard.TargetName="r05" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:0.39333" BeginTime="0:0:0.33332" To="1"/>
<DoubleAnimation Storyboard.TargetName="r06" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:0.39333" BeginTime="0:0:0.41665" To="1"/>
<DoubleAnimation Storyboard.TargetName="r07" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:0.39333" BeginTime="0:0:0.49998" To="1"/>
<DoubleAnimation Storyboard.TargetName="r08" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:0.39333" BeginTime="0:0:0.58331" To="1"/>
<DoubleAnimation Storyboard.TargetName="r09" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:0.39333" BeginTime="0:0:0.66664" To="1"/>
<DoubleAnimation Storyboard.TargetName="r10" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:0.39333" BeginTime="0:0:0.74997" To="1"/>
<DoubleAnimation Storyboard.TargetName="r11" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:0.39333" BeginTime="0:0:0.83330" To="1"/>
<DoubleAnimation Storyboard.TargetName="r12" Storyboard.TargetProperty="Opacity" AutoReverse="True" Duration="0:0:0.39333" BeginTime="0:0:0.91663" To="1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Grid.Triggers>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
如果是想直接用 Storyboard 动态效果,那就:(示例只是随便找的Code,不是Loading了哈~)
// i:Interaction.Triggers -- i:EventTrigger -- i:InvokeCommandAction -- 无关的插曲
//比如 DockPanel 的样式里,想在 Event 加入动态效果
<DockPanel.Triggers>
<!--MouseEnter时 Storyboard效果-->
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="(Control.BorderBrush).(SolidColorBrush.Color)"
Storyboard.TargetName="BranchAccountingBorder" Duration="0:0:0.1"
From="#E7E7E8" To="#E0301E" AutoReverse="False" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave">
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="(Control.BorderBrush).(SolidColorBrush.Color)"
Storyboard.TargetName="BranchAccountingBorder" Duration="0:0:0.1"
From="#E0301E" To="#E7E7E8" AutoReverse="False" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</DockPanel.Triggers>
<!--在没有 Command 属性的控件加入交互命令,使用i : 引用 xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" -->
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<i:InvokeCommandAction Command="{Binding CheckTestCommand}"></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
- LoadingControl.cs 可省略
提供使用处的 X Y 属性(外观效果),用的时候调整就看得出来啦
using System;
using System.Windows;
using System.Windows.Controls;
namespace MyTest.Control
{
public class LoadingControl : ContentControl
{
/// <summary>
/// Y轴半径
/// </summary>
public static readonly DependencyProperty RadiusYPropertyProperty;
/// <summary>
/// X轴半径
/// </summary>
public static readonly DependencyProperty RadiusXPropertyProperty;
/// <summary>
/// 静态构造方法
/// </summary>
static LoadingControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(LoadingControl), new FrameworkPropertyMetadata(typeof(LoadingControl)));
RadiusYPropertyProperty = DependencyProperty.Register("RadiusY", typeof(string), typeof(LoadingControl), new PropertyMetadata("0"));
RadiusXPropertyProperty = DependencyProperty.Register("RadiusX", typeof(string), typeof(LoadingControl), new PropertyMetadata("0"));
}
/// <summary>
/// 构造方法
/// </summary>
public LoadingControl()
{
}
/// <summary>
/// X轴半径
/// </summary>
public string RadiusX
{
get { return (string)GetValue(RadiusXPropertyProperty); }
set { SetValue(RadiusXPropertyProperty, value); }
}
/// <summary>
/// Y轴半径
/// </summary>
public string RadiusY
{
get { return (string)GetValue(RadiusYPropertyProperty); }
set { SetValue(RadiusYPropertyProperty, value); }
}
}
}
- xaml 使用
其实就引用这个控件,然后调整属性就OK 了。
由于动画效果,示例里是加载的时候就一直转动,所以控制 Visibility 属性就可以了。
也可以自定义其他属性 或 事件,触发转动、停止效果。
<mycontrols:LoadingControl Visibility="Collapsed" RadiusX="1.2" RadiusY="3" Width="20" Height="20"/>
【这是个基础版,灵活度很高】 可以改样式、触发时机......