示例:WPF中自定义StoryBoarService在代码中封装StoryBoard、Animation用于简化动画编写
原文:示例:WPF中自定义StoryBoarService在代码中封装StoryBoard、Animation用于简化动画编写
一、目的:通过对StoryBoard和Animation的封装来简化动画的编写
二、示例:
说明:渐隐藏是WPF中比较常用的动画,上图是通过StoryBoarService封装后的效果,在代码中只要执行如下代码即可:
DoubleStoryboardEngine.Create(1, 0, 1, "Opacity").Start(element);
上面的关闭效果可以定义一个命令如下:
-
public class CollapsedOfOpacityCommand : ICommand
-
{
-
-
public bool CanExecute(object parameter) => true;
-
-
public void Execute(object parameter)
-
{
-
if(parameter is UIElement element)
-
{
-
var engine = DoubleStoryboardEngine.Create(1, 0, 1, "Opacity");
-
-
engine.Start(element);
-
}
-
}
-
-
public event EventHandler CanExecuteChanged;
-
}
在Xaml中调用如下命令即可完成关闭渐隐藏的效果
Command="{x:Static base:CommandService.CollapsedOfOpacityCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=GroupBox}}"
传入的CommandParmeter将会在执行命令时渐隐藏
其中动画效果的代码只需一句代码即可,简化了动画在代码中繁琐的编码过程
DoubleStoryboardEngine.Create(1, 0, 1, "Opacity").Start(element);
二、代码:
目前只实现DoubleAnimation的封装,后续将会对其他类型进行封装
1、封闭修改基类
-
/// <summary> 动画引擎基类 </summary>
-
public abstract class StoryboardEngineBase : IDisposable
-
{
-
protected Storyboard storyboard = new Storyboard();
-
-
public EventHandler CompletedEvent { get; set; }
-
-
public EasingFunctionBase Easing { get; set; } = EasingFunctionFactroy.PowerEase;
-
-
public PropertyPath PropertyPath { get; set; }
-
-
public Duration Duration { get; set; }
-
-
public void Dispose()
-
{
-
storyboard.Completed -= CompletedEvent;
-
}
-
-
public abstract StoryboardEngineBase Start(UIElement element);
-
-
public abstract StoryboardEngineBase Stop();
-
-
public StoryboardEngineBase(int second, string property)
-
{
-
this.PropertyPath = new PropertyPath(property);
-
this.Duration = new Duration(TimeSpan.FromSeconds(second));
-
}
-
-
}
-
-
/// <summary> 动画泛型引擎基类 </summary>
-
public abstract class StoryboardEngineBase<T> : StoryboardEngineBase
-
{
-
public StoryboardEngineBase(T from, T to, int second, string property) : base(second, property)
-
{
-
this.FromValue = from;
-
this.ToValue = to;
-
}
-
-
public T FromValue { get; set; }
-
-
public T ToValue { get; set; }
-
-
//public RepeatBehavior RepeatBehavior { get; set; };
-
-
}
2、开放扩展DoubleStoryboardEngine
-
/// <summary> DoubleAnimation动画引擎 </summary>
-
public class DoubleStoryboardEngine : StoryboardEngineBase<double>
-
{
-
public static DoubleStoryboardEngine Create(double from, double to, int second, string property)
-
{
-
return new DoubleStoryboardEngine(from, to, second, property);
-
}
-
-
public DoubleStoryboardEngine(double from, double to, int second, string property) : base(from, to, second, property)
-
{
-
-
}
-
-
public override StoryboardEngineBase Start(UIElement element)
-
{
-
// Do:时间线
-
DoubleAnimation animation = new DoubleAnimation(1, 0, this.Duration);
-
-
if (this.Easing != null)
-
animation.EasingFunction = this.Easing;
-
-
//if (this.RepeatBehavior != default(RepeatBehavior))
-
// animation.RepeatBehavior = (RepeatBehavior);
-
-
// Do:属性动画
-
storyboard.Children.Add(animation);
-
Storyboard.SetTarget(animation, element);
-
Storyboard.SetTargetProperty(animation, this.PropertyPath);
-
-
if (CompletedEvent != null)
-
storyboard.Completed += CompletedEvent;
-
storyboard.Begin();
-
-
return this;
-
}
-
-
public override StoryboardEngineBase Stop()
-
{
-
this.storyboard.Stop();
-
-
return this;
-
}
-
}
3、过度效果工厂
-
/// <summary> 说明:https://docs.microsoft.com/zh-cn/dotnet/framework/wpf/graphics-multimedia/easing-functions </summary>
-
public static class EasingFunctionFactroy
-
{
-
/// <summary> PowerEase:创建加速和/或减速使用的公式的动画f(t) = tp其中 p 等于Power属性。 </summary>
-
public static PowerEase PowerEase { get; set; } = new PowerEase();
-
/// <summary> BackEase:略微收回动画的动作,然后再开始进行动画处理指示的路径中。 </summary>
-
public static BackEase BackEase { get; set; } = new BackEase();
-
/// <summary> ElasticEase:创建类似于弹簧来回直到静止的动画 </summary>
-
public static ElasticEase ElasticEase { get; set; } = new ElasticEase();
-
/// <summary> BounceEase:创建弹跳效果。 </summary>
-
public static BounceEase BounceEase { get; set; } = new BounceEase();
-
/// <summary> CircleEase:创建加速和/或减速使用循环函数的动画。 </summary>
-
public static CircleEase CircleEase { get; set; } = new CircleEase();
-
-
/// <summary> QuadraticEase:创建加速和/或减速使用的公式的动画f(t) = t2。 </summary>
-
public static QuadraticEase QuadraticEase { get; set; } = new QuadraticEase();
-
-
/// <summary> CubicEase:创建加速和/或减速使用的公式的动画f(t) = t3。 </summary>
-
public static CubicEase CubicEase { get; set; } = new CubicEase();
-
/// <summary> QuarticEase:创建加速和/或减速使用的公式的动画f(t) = t4。 </summary>
-
public static QuarticEase QuarticEase { get; set; } = new QuarticEase();
-
/// <summary> QuinticEase:创建加速和/或减速使用的公式的动画f(t) = t5。 </summary>
-
public static QuinticEase QuinticEase { get; set; } = new QuinticEase();
-
-
/// <summary> ExponentialEase:创建加速和/或减速使用指数公式的动画。 </summary>
-
public static ExponentialEase ExponentialEase { get; set; } = new ExponentialEase();
-
-
/// <summary> SineEase:创建加速和/或减速使用正弦公式的动画。 </summary>
-
public static SineEase SineEase { get; set; } = new SineEase();
-
-
}
4、使用方法:
/// <summary> 构造方法 </summary>
/// <param name="from"> 起始值</param>
/// <param name="to"> 结束值 </param>
/// <param name="second"> 间隔时间秒 </param>
/// <param name="property"> 修改属性名称 </param>
///
public static DoubleStoryboardEngine Create(double from, double to, int second, string property)
{
return new DoubleStoryboardEngine(from, to, second, property);
}
下载地址:https://github.com/HeBianGu/WPF-ControlBase.git
示例:WPF中自定义StoryBoarService在代码中封装StoryBoard、Animation用于简化动画编写的更多相关文章
- Salesforce 自定义标签在代码中的应用
自定义标签简介 Salesforce 中自定义标签(Custom Label)的作用是存储一般性的文本,可以用于 Apex.Visualforce 页面.Lightning 组件等地方,用于显示提示信 ...
- SQL语句在查询分析器中可以执行,代码中不能执行
问题:SQL语句在查询分析器中可以执行,代码中不能执行 解答:sql中包含数据库的关键字,将关键字用[]括起来,可以解决. 后记:建数据库的时候尽量避免使用关键字. 例子: sql.Format(&q ...
- JDK中ThreadDump诊断Java代码中的线程死锁问题
多线程的死锁..死锁不是死了而是线程互相等待... 在项目中可能就是在几十万行的代码中存在一个死锁的问题,如何发现这个问题并且解决这个问题. JavaJDK为我们提供了一个诊断工具叫做ThreadDu ...
- .NET/C# 中你可以在代码中写多个 Main 函数,然后按需要随时切换
.NET/C# 程序从 Main 函数开始执行,基本上各种书籍资料都是这么写的.不过,我们可以写多个 Main 函数,然后在项目文件中设置应该选择哪一个 Main 函数. 你可能会觉得这样没有什么用, ...
- VS中批量删除cs代码中的#region和#endregion
Visual Studio中如何批量删除cs代码中的#region和#endregion,不删除它们中间的代码,只删除这两个标记及标记的注解的方法.Vs中提供了很强大的文本查找与替换功能,简单的替换只 ...
- Android中自定义veiw使用Java中的回调方法
//------------------MainActivity----中---------------------------------- import android.os.Bundle;imp ...
- Xpath在选择器中正确,在代码中返回的是空列表问题
一.问题: 在进行爬虫的时候我们会用到xpath解析html文件,但是会有一种情况就是在xpath选择器中可以使用,但是在代码中就无法使用的情况. 二.原因: 1.是元素中有tbody的原因,这个元素 ...
- c代码中调用c++,c++代码中调用c代码
注意这里的c调用c++或者c++调用c的意思是.c文件中调用.cpp文件中的代码,或者相反 集成开发环境如vc++6.0或者vs都是通过文件后缀来区别当前要编译的是C代码还是C++代码,然后采用相应的 ...
- [django]模板中自定义变量&django模板中的变量
django自定义模板变量 context_processors.py def mysetings(request): return { 'NAME': 'maotai' } settings.py ...
随机推荐
- mysql单个表拆分成多个表
一.横向拆分 create table 新表的名称 select * from 被拆分的表 order by id limit int1,int2 int1为其实位置,int2为几条 注意:这样拆分后 ...
- DDL(数据库定义语言)(五)
一.数据定义语言(Data Definition Language)的基本操作 定义数据库.表等,包括CREATE语句.ALTER语句.DROP语句.CREATE语句用于创建数据库.数据表等,ALTE ...
- pod install/update失败:Failed to connect to 127.0.0.1 port 1080: Connection refused
出现这类错误,通常是因为代理发生的,取消代理即可! 1.查看有无相关代理: git config --global http.proxy git config --global https.proxy ...
- 复数基础及其2D空间的旋转
本文我们讨论复数及其旋转的含义.复数很有意思,本文介绍了复数的基本定义和性质,以及它关于旋转的几何意义. 复数对于旋转的两个方面极为重要: 1. 它引入了旋转算子(rotational operato ...
- 洛谷P1192-台阶问题(线性递推 扩展斐波那契)
占坑 先贴上AC代码 回头来补坑 #include <iostream> using namespace std; int n, k; const int mod = 100003; lo ...
- nginx 常用的中间件
1.--with-http_stub_status_module nginx客户端状态 # 打开default.conf文件 vim /etc/nginx/conf.d/default.conf # ...
- Python基础之while和for
实现ATM的输入密码重新输入的功能 while True: user_db = 'nick' pwd_db = '123' inp_user = input('username: ') inp_pwd ...
- Windbg Memory(内存)窗口的使用
在 WinDbg 中,可以查看和编辑内存,通过输入命令或通过使用内存窗口. 内存窗口的打开 通过菜单View-->Memory 通过快捷键Alt+5 通过工具栏 使用内存窗口 通过上面方式打开的 ...
- Flask常用实列化参数
Flask中实列化配置: app = Flask( __name__, template_folder=’temp’ , ...... ) >template_folder = "te ...
- vb.net 判断某个文件是否已经打开了
' 判断这个excel文件是否已经打开了: 如果打开了,不能下载 Try Dim fs AsFileStream = NewFileStream(excelFileName, FileMode.O ...