WPF加载等待动画
原文:WPF加载等待动画
原文地址:https://www.codeproject.com/Articles/57984/WPF-Loading-Wait-Adorner

界面遮罩
<UserControl.Background>
<SolidColorBrush Color="Black" Opacity=".20" />
</UserControl.Background>
等待动画全局颜色
<UserControl.Resources>
<SolidColorBrush Color="CornflowerBlue" x:Key="CirclesColor" />
</UserControl.Resources>
等待动画中的小圆
<Ellipse x:Name="C0" Width="20" Height="20"
Canvas.Left="0"
Canvas.Top="0" Stretch="Fill"
Fill="{StaticResource CirclesColor}" Opacity="1.0"/>
<UserControl x:Class="ControlSamples.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:ControlSamples"
IsVisibleChanged="HandleVisibleChanged"
mc:Ignorable="d" >
<UserControl.Background>
<SolidColorBrush Color="Black" Opacity=".20" />
</UserControl.Background>
<UserControl.Resources>
<SolidColorBrush Color="CornflowerBlue" x:Key="CirclesColor" />
</UserControl.Resources>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Viewbox Width="100" Height="100"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Grid x:Name="LayoutRoot"
Background="Transparent"
ToolTip="..."
HorizontalAlignment="Center"
VerticalAlignment="Center">
<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>
<TextBlock x:Name="TextControl" FontSize="24" Text="" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap" Margin="20"></TextBlock>
</StackPanel>
</UserControl>
后台业务代码,添加了几项属性、动画控制、小圆的位置设置
/// <summary>
/// LoadingWait.xaml 的交互逻辑
/// </summary>
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
}
WPF加载等待动画的更多相关文章
- WPF 加载等待动画
原文:WPF 加载等待动画 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_29844879/article/details/80216587 ...
- [Swift通天遁地]一、超级工具-(11)使用EZLoadingActivity制作Loading加载等待动画
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- vue实现首屏加载等待动画 避免首次加载白屏尴尬
0 直接上效果图 1背景,用户体验良好一直是个重要的问题. 2怎么加到自己项目里面? 复制css html代码到自己的index.html即可 代码链接 源码地址 Vue学习前端群493671066, ...
- IOS开发UI篇之──自定义加载等待框(MBProgressHUD)
本文转载至 http://blog.csdn.net/xunyn/article/details/8064984 原文地址http://www.189works.com/article-89289 ...
- 【Ionic】---$ionicLoading ion-spinner SVG旋转加载的动画图标
ionic 加载动作 $ionicLoading $ionicLoading 是 ionic 默认的一个加载交互效果.里面的内容也是可以在模板里面修改. 用法 angular.module('Load ...
- 页面预加载loading动画,再载入内容
默认情况下如果网站请求速度慢,所以会有一段时间的空白页面等等,用户体验效果不好,见到很多的页面都有预加载的效果,加载之前先加载一个动画,后台进程继续加载页面内容,当页面内容加载完之后再退出动画显示内容 ...
- HTML5+javascript实现图片加载进度动画效果
在网上找资料的时候,看到网上有图片加载进度的效果,手痒就自己也写了一个. 图片加载完后,隐藏loading效果. 想看加载效果,请ctrel+F5强制刷新或者清理缓存. 效果预览: 0% // ...
- 纯css3 加载loading动画特效
最近项目中要实现当页面还没有加载完给用户提示正在加载的loading,本来是想做个图片提示的,但是图片如果放大电脑的分辨率就会感觉到很虚,体验效果很不好.于是就采用css3+js实现这个loading ...
- C#窗体的加载等待(BackgroundWorker控件)实现
窗体拉一个Button按钮和一个加载等待显示的label, label默认隐藏,点击按钮时显示这个label,加载完再隐藏 1.工具箱拉BackgroundWorker控件到窗体 2.backgrou ...
随机推荐
- 9个完整android开源app项目
一.photoup 介绍: photoup 是一款开源的相册类app,主要功能是将本地图片提交到facebook上去,虽然他的功能和facebook的远程服务相关,但是本身是可以被当作一款 相册应用的 ...
- ebook https://salttiger.com/category/notification/
https://salttiger.com/category/notification/ 因为个人精力有限,持续搜集.整理.分享电子书已经占用我大部分业余时间,今后不再给评论区留言的朋友们找书,还希望 ...
- kali 系统的源
sources.list deb http://http.kali.org/kali kali-rolling main non-free contrib deb http://mirrors.ust ...
- 5.7-基于Binlog+Position的复制搭建
基本环境 Master Slave MySQL版本 MySQL-5.7.16-X86_64 MySQL-5.7.16-X86_64 IP 192.168.56.156 192.168.56.157 ...
- NOIP 模拟 路径求和 - Tarjan+dfs+码
题目大意: 各一个奇环内向森林,求每两个点对间的距离之和.无法到达则距离为-1. 分析: 首先Tarjan找出size大于1的连通分量(环),环中的边的贡献可以单独计算. 然后从入度为0的点向内dfs ...
- 我的Boss有性能优化强迫症
我有一个Boss,他曾经在阿里深造,在UC修炼,在一号店奔波. 经过几个月的合作开发和技术交流,我发现他非常在乎程序的性能,但是呢,对于有些地方,我觉得划不来. 比如说, 把数据库中的30多条记录,查 ...
- SpringBoot JPA 专题
Error: Error starting ApplicationContext. To display the auto-configuration report re-run your appli ...
- 【a703】求逆序对
Time Limit: 10 second Memory Limit: 2 MB 问题描述 给定一个序列a1,a2...an.如果存在i小于j 并且ai大于aj,那么我们称之为逆序对,求给定序列中逆序 ...
- hive 导出数据的几种方式
1.使用insert导出 这种方式的优点在于既可以导出到hdfs上还可以导出到本地目录 下面以导出emp表中数据为例 insert overwrite local directory "/o ...
- 【29.82%】【codeforces 703D】Mishka and Interesting sum
[题解] 题意: 给n个数字组成有序数列; 给m个询问. 对于每个询问区间.输出这个区间里面出现次数为偶数次的所有数的异或值; 做法: 我们可以先求出这段区间里面所有(包括重复的数字)数字的异或值p1 ...