原文:示例:WPF中自定义StoryBoarService在代码中封装StoryBoard、Animation用于简化动画编写

一、目的:通过对StoryBoard和Animation的封装来简化动画的编写

二、示例:

说明:渐隐藏是WPF中比较常用的动画,上图是通过StoryBoarService封装后的效果,在代码中只要执行如下代码即可:

 DoubleStoryboardEngine.Create(1, 0, 1, "Opacity").Start(element);

上面的关闭效果可以定义一个命令如下:


  1. public class CollapsedOfOpacityCommand : ICommand
  2. {
  3. public bool CanExecute(object parameter) => true;
  4. public void Execute(object parameter)
  5. {
  6. if(parameter is UIElement element)
  7. {
  8. var engine = DoubleStoryboardEngine.Create(1, 0, 1, "Opacity");
  9. engine.Start(element);
  10. }
  11. }
  12. public event EventHandler CanExecuteChanged;
  13. }

在Xaml中调用如下命令即可完成关闭渐隐藏的效果

Command="{x:Static base:CommandService.CollapsedOfOpacityCommand}"

CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=GroupBox}}"

传入的CommandParmeter将会在执行命令时渐隐藏

其中动画效果的代码只需一句代码即可,简化了动画在代码中繁琐的编码过程

DoubleStoryboardEngine.Create(1, 0, 1, "Opacity").Start(element);

二、代码:

目前只实现DoubleAnimation的封装,后续将会对其他类型进行封装

1、封闭修改基类


  1. /// <summary> 动画引擎基类 </summary>
  2. public abstract class StoryboardEngineBase : IDisposable
  3. {
  4. protected Storyboard storyboard = new Storyboard();
  5. public EventHandler CompletedEvent { get; set; }
  6. public EasingFunctionBase Easing { get; set; } = EasingFunctionFactroy.PowerEase;
  7. public PropertyPath PropertyPath { get; set; }
  8. public Duration Duration { get; set; }
  9. public void Dispose()
  10. {
  11. storyboard.Completed -= CompletedEvent;
  12. }
  13. public abstract StoryboardEngineBase Start(UIElement element);
  14. public abstract StoryboardEngineBase Stop();
  15. public StoryboardEngineBase(int second, string property)
  16. {
  17. this.PropertyPath = new PropertyPath(property);
  18. this.Duration = new Duration(TimeSpan.FromSeconds(second));
  19. }
  20. }
  21. /// <summary> 动画泛型引擎基类 </summary>
  22. public abstract class StoryboardEngineBase<T> : StoryboardEngineBase
  23. {
  24. public StoryboardEngineBase(T from, T to, int second, string property) : base(second, property)
  25. {
  26. this.FromValue = from;
  27. this.ToValue = to;
  28. }
  29. public T FromValue { get; set; }
  30. public T ToValue { get; set; }
  31. //public RepeatBehavior RepeatBehavior { get; set; };
  32. }

2、开放扩展DoubleStoryboardEngine


  1. /// <summary> DoubleAnimation动画引擎 </summary>
  2. public class DoubleStoryboardEngine : StoryboardEngineBase<double>
  3. {
  4. public static DoubleStoryboardEngine Create(double from, double to, int second, string property)
  5. {
  6. return new DoubleStoryboardEngine(from, to, second, property);
  7. }
  8. public DoubleStoryboardEngine(double from, double to, int second, string property) : base(from, to, second, property)
  9. {
  10. }
  11. public override StoryboardEngineBase Start(UIElement element)
  12. {
  13. // Do:时间线
  14. DoubleAnimation animation = new DoubleAnimation(1, 0, this.Duration);
  15. if (this.Easing != null)
  16. animation.EasingFunction = this.Easing;
  17. //if (this.RepeatBehavior != default(RepeatBehavior))
  18. // animation.RepeatBehavior = (RepeatBehavior);
  19. // Do:属性动画
  20. storyboard.Children.Add(animation);
  21. Storyboard.SetTarget(animation, element);
  22. Storyboard.SetTargetProperty(animation, this.PropertyPath);
  23. if (CompletedEvent != null)
  24. storyboard.Completed += CompletedEvent;
  25. storyboard.Begin();
  26. return this;
  27. }
  28. public override StoryboardEngineBase Stop()
  29. {
  30. this.storyboard.Stop();
  31. return this;
  32. }
  33. }

3、过度效果工厂


  1. /// <summary> 说明:https://docs.microsoft.com/zh-cn/dotnet/framework/wpf/graphics-multimedia/easing-functions </summary>
  2. public static class EasingFunctionFactroy
  3. {
  4. /// <summary> PowerEase:创建加速和/或减速使用的公式的动画f(t) = tp其中 p 等于Power属性。 </summary>
  5. public static PowerEase PowerEase { get; set; } = new PowerEase();
  6. /// <summary> BackEase:略微收回动画的动作,然后再开始进行动画处理指示的路径中。 </summary>
  7. public static BackEase BackEase { get; set; } = new BackEase();
  8. /// <summary> ElasticEase:创建类似于弹簧来回直到静止的动画 </summary>
  9. public static ElasticEase ElasticEase { get; set; } = new ElasticEase();
  10. /// <summary> BounceEase:创建弹跳效果。 </summary>
  11. public static BounceEase BounceEase { get; set; } = new BounceEase();
  12. /// <summary> CircleEase:创建加速和/或减速使用循环函数的动画。 </summary>
  13. public static CircleEase CircleEase { get; set; } = new CircleEase();
  14. /// <summary> QuadraticEase:创建加速和/或减速使用的公式的动画f(t) = t2。 </summary>
  15. public static QuadraticEase QuadraticEase { get; set; } = new QuadraticEase();
  16. /// <summary> CubicEase:创建加速和/或减速使用的公式的动画f(t) = t3。 </summary>
  17. public static CubicEase CubicEase { get; set; } = new CubicEase();
  18. /// <summary> QuarticEase:创建加速和/或减速使用的公式的动画f(t) = t4。 </summary>
  19. public static QuarticEase QuarticEase { get; set; } = new QuarticEase();
  20. /// <summary> QuinticEase:创建加速和/或减速使用的公式的动画f(t) = t5。 </summary>
  21. public static QuinticEase QuinticEase { get; set; } = new QuinticEase();
  22. /// <summary> ExponentialEase:创建加速和/或减速使用指数公式的动画。 </summary>
  23. public static ExponentialEase ExponentialEase { get; set; } = new ExponentialEase();
  24. /// <summary> SineEase:创建加速和/或减速使用正弦公式的动画。 </summary>
  25. public static SineEase SineEase { get; set; } = new SineEase();
  26. }

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用于简化动画编写的更多相关文章

  1. Salesforce 自定义标签在代码中的应用

    自定义标签简介 Salesforce 中自定义标签(Custom Label)的作用是存储一般性的文本,可以用于 Apex.Visualforce 页面.Lightning 组件等地方,用于显示提示信 ...

  2. SQL语句在查询分析器中可以执行,代码中不能执行

    问题:SQL语句在查询分析器中可以执行,代码中不能执行 解答:sql中包含数据库的关键字,将关键字用[]括起来,可以解决. 后记:建数据库的时候尽量避免使用关键字. 例子: sql.Format(&q ...

  3. JDK中ThreadDump诊断Java代码中的线程死锁问题

    多线程的死锁..死锁不是死了而是线程互相等待... 在项目中可能就是在几十万行的代码中存在一个死锁的问题,如何发现这个问题并且解决这个问题. JavaJDK为我们提供了一个诊断工具叫做ThreadDu ...

  4. .NET/C# 中你可以在代码中写多个 Main 函数,然后按需要随时切换

    .NET/C# 程序从 Main 函数开始执行,基本上各种书籍资料都是这么写的.不过,我们可以写多个 Main 函数,然后在项目文件中设置应该选择哪一个 Main 函数. 你可能会觉得这样没有什么用, ...

  5. VS中批量删除cs代码中的#region和#endregion

    Visual Studio中如何批量删除cs代码中的#region和#endregion,不删除它们中间的代码,只删除这两个标记及标记的注解的方法.Vs中提供了很强大的文本查找与替换功能,简单的替换只 ...

  6. Android中自定义veiw使用Java中的回调方法

    //------------------MainActivity----中---------------------------------- import android.os.Bundle;imp ...

  7. Xpath在选择器中正确,在代码中返回的是空列表问题

    一.问题: 在进行爬虫的时候我们会用到xpath解析html文件,但是会有一种情况就是在xpath选择器中可以使用,但是在代码中就无法使用的情况. 二.原因: 1.是元素中有tbody的原因,这个元素 ...

  8. c代码中调用c++,c++代码中调用c代码

    注意这里的c调用c++或者c++调用c的意思是.c文件中调用.cpp文件中的代码,或者相反 集成开发环境如vc++6.0或者vs都是通过文件后缀来区别当前要编译的是C代码还是C++代码,然后采用相应的 ...

  9. [django]模板中自定义变量&django模板中的变量

    django自定义模板变量 context_processors.py def mysetings(request): return { 'NAME': 'maotai' } settings.py ...

随机推荐

  1. mysql单个表拆分成多个表

    一.横向拆分 create table 新表的名称 select * from 被拆分的表 order by id limit int1,int2 int1为其实位置,int2为几条 注意:这样拆分后 ...

  2. DDL(数据库定义语言)(五)

    一.数据定义语言(Data Definition Language)的基本操作 定义数据库.表等,包括CREATE语句.ALTER语句.DROP语句.CREATE语句用于创建数据库.数据表等,ALTE ...

  3. 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 ...

  4. 复数基础及其2D空间的旋转

    本文我们讨论复数及其旋转的含义.复数很有意思,本文介绍了复数的基本定义和性质,以及它关于旋转的几何意义. 复数对于旋转的两个方面极为重要: 1. 它引入了旋转算子(rotational operato ...

  5. 洛谷P1192-台阶问题(线性递推 扩展斐波那契)

    占坑 先贴上AC代码 回头来补坑 #include <iostream> using namespace std; int n, k; const int mod = 100003; lo ...

  6. nginx 常用的中间件

    1.--with-http_stub_status_module nginx客户端状态 # 打开default.conf文件 vim /etc/nginx/conf.d/default.conf # ...

  7. Python基础之while和for

    实现ATM的输入密码重新输入的功能 while True: user_db = 'nick' pwd_db = '123' inp_user = input('username: ') inp_pwd ...

  8. Windbg Memory(内存)窗口的使用

    在 WinDbg 中,可以查看和编辑内存,通过输入命令或通过使用内存窗口. 内存窗口的打开 通过菜单View-->Memory 通过快捷键Alt+5 通过工具栏 使用内存窗口 通过上面方式打开的 ...

  9. Flask常用实列化参数

    Flask中实列化配置: app = Flask( __name__, template_folder=’temp’ , ...... ) >template_folder = "te ...

  10. vb.net 判断某个文件是否已经打开了

    '   判断这个excel文件是否已经打开了: 如果打开了,不能下载 Try Dim fs AsFileStream = NewFileStream(excelFileName, FileMode.O ...