原文:示例: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. NI CWGraph 显示波形图

    ptrWaveBox.Axes(1).Maximum = 1000 ptrWaveBox.Axes(2).Maximum = 20 ptrWaveBox.Axes(2).Minimum = 0 Dim ...

  2. Spring通过配置类加载实体bean

    以下4个java类都在都一个包下: 1.定义接口 public interface AA { void play(); } 2.定义实体bean //组件注解,表明该类是一个组件 @Component ...

  3. Python从零开始——安装与运行

  4. Odoo中的字段显示方式和行为控制

      在odoo的视图中,字段都是通过widget来控制显示效果和行为的.   一般情况下,不同类型的字段odoo会使用默认的widget来显示和控制它的行为.   options以一种JSON对象的形 ...

  5. bert模型参数简化

    我们下载下来的预训练的bert模型的大小大概是400M左右,但是我们自己预训练的bert模型,或者是我们在开源的bert模型上fine-tuning之后的模型的大小大约是1.1G,我们来看看到底是什么 ...

  6. php观察者模式(observer pattern)

    ... <?php /* The observer pattern implements a one-too-many dependency between objects. The objec ...

  7. python 解决粘包问题的例子(ftp文件的上传与下载)简单版本

    服务端 ! /user/bin/env python3 -- coding:utf_8 -- """ Author:Markli # 2019/9/9,16:41 &qu ...

  8. Python多线程采集百度相关搜索关键词

    百度相关搜索关键词抓取,读取txt关键词,导出txt关键词 #百度相关搜索关键词抓取,读取txt关键词,导出txt关键词   # -*- coding=utf-8 -*- import request ...

  9. python语言(八)多线程、多进程、虚拟环境、unittest、生成测试报告

    一.多线程 进程与线程 进程:进程是资源(CPU.内存等)分配的最小单位,进程有独立的地址空间与系统资源,一个进程可以包含一个或多个线程 线程:线程是CPU调度的最小单位,是进程的一个执行流,线程依赖 ...

  10. CSS布局对齐的小技巧

    类似以上这种对齐怎么做? 很简单,上面是的污水开始的位置是由于被"能源种类"顶着,下面没有字怎么办?最差的办法就是用margin-left,因为在不同的机器上,可能会出现兼容性问题 ...