我认为你应该使用“StoryBoard”和“Animation”来处理你的 UserControl 下降。
内置动画基于“计时器”系统,但它已经处理了所有问题,例如“如果尚未提交先前的更改,则不要将 3 次更改发布到屏幕”,或者“如果 UI 线程没有提交,则中止先前的位置渲染” '没有时间开始渲染它"。
所以,我会以编程方式生成“故事板”。
知道每个 DependencyProperty 都可以“动画化”是很有用的。
见:Using a Storyboard animation on a programatically-added control
这是我为具有“双”类型的属性设置动画的代码:
/// <summary>
/// Create a linear animation
/// </summary>
/// <param name="duration"></param>
public void CreateDoubleLinearAnimation(DependencyObject element, string name, string property, double? valueFrom, double valueTo, TimeSpan timeSpan, Storyboard sb)
{
// --- Animation toute bête
DoubleAnimation animation = new DoubleAnimation();
animation.Duration = new Duration(timeSpan);
animation.To = valueTo;
if (valueFrom != null)
animation.From = valueFrom.Value;
animation.SetValue(Storyboard.TargetNameProperty, name);
animation.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath(property));
sb.Children.Add(animation);
}
调用示例:
Storyboard sb = new Storyboard();
CreateDoubleLinearAnimation(element, "myElement", "(UIElement.Opacity)", 0, 1, TimeSpan.FromSeconds(1), sb);
“属性”参数示例:
(Shape.StrokeThickness)
(UIElement.Opacity)
(UIElement.RenderTransform).(TransformGroup.Children)[0].(RotateTransform.Angle)
最好的问候,