From a023c7f6cb59a7d07c5ee55ae6119c5461b0bdb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8E=E6=99=BA=E7=BA=AF?= <于智纯@LODESTAR> Date: Sun, 7 Sep 2025 21:09:03 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E3=80=8C=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E6=95=85=E4=BA=8B=E9=9D=A2=E6=9D=BF=E3=80=8D=E4=B8=8E?= =?UTF-8?q?=E3=80=8C=E5=B1=9E=E6=80=A7=E8=AE=BE=E7=BD=AE=E5=8A=A8=E7=94=BB?= =?UTF-8?q?=E3=80=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RazorEngineTest/PropertySetterAnimation.cs | 41 +++++++++++++++++++ RazorEngineTest/PropertyStoryboard.cs | 46 ++++++++++++++++++++++ RazorEngineTest/Themes/Generic.xaml | 4 +- 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 RazorEngineTest/PropertySetterAnimation.cs create mode 100644 RazorEngineTest/PropertyStoryboard.cs diff --git a/RazorEngineTest/PropertySetterAnimation.cs b/RazorEngineTest/PropertySetterAnimation.cs new file mode 100644 index 0000000..a27efbd --- /dev/null +++ b/RazorEngineTest/PropertySetterAnimation.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Media.Animation; + +namespace RazorEngineTest +{ + public class PropertySetterAnimation : AnimationTimeline + { + // 定义依赖属性用于绑定目标值 + public static readonly DependencyProperty ValueProperty = + DependencyProperty.Register("Value", typeof(object), typeof(PropertySetterAnimation)); + + public object Value + { + get { return GetValue(ValueProperty); } + set { SetValue(ValueProperty, value); } + } + + // 指定目标属性类型(可设置为任意类型) + public override Type TargetPropertyType => typeof(object); + + // 创建动画实例 + protected override Freezable CreateInstanceCore() => new PropertySetterAnimation(); + + // 核心方法:返回固定值(无插值过程) + public override object GetCurrentValue( + object defaultOriginValue, + object defaultDestinationValue, + AnimationClock animationClock) + { + return Value; // 直接返回预设值 + } + + // 可选:设置是否使用动画插值 + public bool IsAnimationEnabled { get; set; } = false; + } +} diff --git a/RazorEngineTest/PropertyStoryboard.cs b/RazorEngineTest/PropertyStoryboard.cs new file mode 100644 index 0000000..21cbbc2 --- /dev/null +++ b/RazorEngineTest/PropertyStoryboard.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Media.Animation; + +namespace RazorEngineTest +{ + public class PropertyStoryboard : Storyboard + { + private readonly SetterBaseCollection _setters = new SetterBaseCollection(); + + public SetterBaseCollection Setters => _setters; + public new void Begin() + { + base.Begin(); + ApplyPropertySetters(); + } + + private void ApplyPropertySetters() + { + foreach (SetterBase setterBase in _setters) + { + if (setterBase is Setter setter && GetTarget(setter.TargetName) is DependencyObject targetObject) + { + DependencyProperty property = setter.Property; + object value = setter.Value; + + if (property != null) + { + targetObject.SetValue(property, value); + } + } + } + } + + // 快捷添加Setter的方法 + public void AddSetter(DependencyProperty property, object value) + { + _setters.Add(new Setter(property, value)); + } + public DependencyObject GetTarget(string targetName) => throw new NotImplementedException(); + } +} diff --git a/RazorEngineTest/Themes/Generic.xaml b/RazorEngineTest/Themes/Generic.xaml index a73f963..a860c72 100644 --- a/RazorEngineTest/Themes/Generic.xaml +++ b/RazorEngineTest/Themes/Generic.xaml @@ -30,7 +30,8 @@ - @@ -50,6 +51,7 @@ +