当前位置: 首页 > news >正文

WPF 控制动画开关

记录一种实现方式:

第一步:

首先定义一个静态类,提供依赖属性,进而方便在xaml中实现绑定:

 public static class AnimationBehavior{// 定义附加属性public static readonly DependencyProperty IsAnimatingProperty =DependencyProperty.RegisterAttached("IsAnimating",typeof(bool),typeof(AnimationBehavior),new PropertyMetadata(false, OnIsAnimatingChanged));public static bool GetIsAnimating(DependencyObject obj) =>(bool)obj.GetValue(IsAnimatingProperty);public static void SetIsAnimating(DependencyObject obj, bool value) =>obj.SetValue(IsAnimatingProperty, value);private static void OnIsAnimatingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){if (d is FrameworkElement element){// 通过 VisualTreeHelper 查找模板中的 Storyboardif (element.FindResource("ShadowAnimation") is Storyboard storyboard){if ((bool)e.NewValue)storyboard.Begin(element, true); // 启动动画elsestoryboard.Stop(element);      // 停止动画}}}}

其中实现了依赖属性IsAnimating。

第二步:

在前端中定义动画:

 <Page.Resources><!--  将 Storyboard 定义在全局资源中  --><Storyboard x:Key="ShadowAnimation"><DoubleAnimationAutoReverse="True"RepeatBehavior="Forever"Storyboard.TargetName="ShadowEffect"Storyboard.TargetProperty="Opacity"From="0.0"To="1"Duration="0:0:3" /><DoubleAnimationAutoReverse="True"RepeatBehavior="Forever"Storyboard.TargetName="myBorder"Storyboard.TargetProperty="Opacity"From="0.3"To="1"Duration="0:0:3" /><!--<ThicknessAnimationAutoReverse="True"RepeatBehavior="Forever"Storyboard.TargetName="myBorder"Storyboard.TargetProperty="Margin"From="15"To="12"Duration="0:0:1.5" />--></Storyboard></Page.Resources>

其中每一个DoubleAnimation定义中的TargetName是当前界面的其他对象名称:

<Borderx:Name="myBorder"Grid.ColumnSpan="1"Margin="4"localVM:AnimationBehavior.IsAnimating="{Binding CurrentActionHandle.ExecutionState, Converter={StaticResource SingleFlowExecutionStateToBoolJustRunnimg}}"Background="{Binding CurrentActionHandle.ExecutionState, Converter={StaticResource SingleFlowExecutionStateToBrushColor}}"CornerRadius="10"><Border.Effect><DropShadowEffectx:Name="ShadowEffect"BlurRadius="10"Direction="12"Opacity="80"ShadowDepth="0"Color="{Binding CurrentActionHandle.ExecutionState, Converter={StaticResource SingleFlowExecutionStateToColor}}" /></Border.Effect></Border>

比如myBorder 和 ShadowEffect; 

第三步:

创建绑定关系:

比如上方代码中:

localVM:AnimationBehavior.IsAnimating="{Binding CurrentActionHandle.ExecutionState, Converter={StaticResource SingleFlowExecutionStateToBoolJustRunnimg}}"

给新手解释一下,其中localVM是自己定义的命名空间:

 

可我这里使用了转换器,可以理解为就是把执行动画的开关绑定到了我后端ViewModel的属性上。

http://www.dtcms.com/a/296170.html

相关文章:

  • 一键修复ipynb,Jupyter Notebook损坏文件
  • redis前期工作:环境搭建-在ubuntu安装redis
  • 基于xxl-job的分片实现分库分表后的扫表
  • Qt WebEngine Widgets的使用
  • MCNN-BiLSTM-Attention分类预测模型等!
  • ChemDraw23软件下载及安装教程|附带软件下载文件|ChemDraw20-23pro版本
  • <<P4116 Qtree3>>
  • 胡良兵Nature Chem Eng:孔隙门控焦耳热精准升级聚乙烯为航油前驱物
  • 中央广播电视总台联合阿里云研究院权威发布《中国人工智能应用发展报告(2025)》:我国依旧需要大力注重人工智能人才的培养
  • Coze工作流-更多图像插件
  • 数据集成难在哪?制造企业该怎么做?
  • Docker多主机网络连接:实现跨主机通信
  • 主流摄像头协议及其开源情况,GB/T 28181协议介绍
  • 配电自动化终端中电源模块的设计
  • uniapp中flex布局gap属性兼容处理
  • PH73211L_VC1/PH73211LQ_VC1:低功耗USB HiFi音频解码器固件技术解析
  • QML WorkerScript
  • 【Spring Boot】热部署终极指南:IDEA高效配置与JRebel替代方案深度解析
  • 第4章唯一ID生成器——4.1 分布式唯一ID
  • Vimba相机二次开发教程,基于Python
  • R 语言科研配色 --- 第 81 期 (附免费下载的配色绘图PPT)
  • 【性能测试】Jmeter+Grafana+InfluxDB+Prometheus Windows安装部署教程
  • 重生学AI第二十集(大结局):完善模型以及学习总结
  • 【STM32】FreeRTOS 任务的删除(三)
  • NX804NX810美光固态闪存NX815NX839
  • 人形机器人双足行走动力学:K-V模型其肌腱特性拟合中的应用
  • Baumer工业相机堡盟工业相机如何通过YoloV8深度学习模型通过YoloV8深度学习模型实现工人安全装备(安全帽、手套、马甲等)检测识别 (C#)
  • C++高效实现轨迹规划、自动泊车、RTS游戏、战术迂回包抄、空中轨迹、手术机器人、KD树
  • 408——数据结构(第二章 线性表)
  • IPSec VPN -- 野蛮模式