4

这可能与动画有关。我有一个 WPF 控件,我通过几个不同的故事板为其宽度和高度设置动画。我创建故事板,然后在它们上调用 Begin()。我已经提供了故事板在下面的样子的代码。

我想重新评估某些事件的控件大小(例如窗口调整大小),使其与动画的值不同。我尝试通过设置控件的WidthHeight属性手动(在动画运行后)在 SizeChanged 上处理此问题。调试器显示未设置这些值(保留原始值)。

当我通过 Snoop 检查 WPF 时,Width 和 Height 行以桃色/橙色突出显示,并且尝试再次通过它设置值不会保留它(当我专注时会显示原始值。我的猜测是我的动画以某种方式覆盖了对属性的手动更改,但我不确定这是否属实或如何解决它。

故事板类

public class MyAnimationClass
{
    private Storyboard _myStoryboard;
    private DoubleAnimation _animation1;
    private DoubleAnimation _animation2;
    private DoubleAnimation _animation3;

    private void InitializeStoryboard()
    {
        _myStoryboard = CreateMyStoryboard(out _animation1, out _animation2, out _animation3);
    }

    private Storyboard CreateMyStoryboard(out DoubleAnimation animation1, out DoubleAnimation animation2, out DoubleAnimation animation3)
    {
        var myStoryboard = new Storyboard();

        // create animations
        animation1 = new DoubleAnimation { Duration = new TimeSpan(0, 0, 0, 0, 250), From = 0, To = 0 };
        animation2 = new DoubleAnimation { BeginTime = new TimeSpan(), Duration = new TimeSpan(0, 0, 0, 0, 250), From = 35, To = 35 };
        animation3 = new DoubleAnimation { BeginTime = new TimeSpan(0, 0, 0, 0, 250), Duration = new TimeSpan(0, 0, 0, 0, 250), From = 35, To = 0 };

        Storyboard.SetTargetProperty(animation1, new PropertyPath(FrameworkElement.WidthProperty));
        Storyboard.SetTargetProperty(animation2, new PropertyPath(FrameworkElement.HeightProperty));
        Storyboard.SetTargetProperty(animation3, new PropertyPath(FrameworkElement.HeightProperty));

        myStoryboard.Children.Add(animation1);
        myStoryboard.Children.Add(animation2);
        myStoryboard.Children.Add(animation3);

        return myStoryboard;
    }

    public void Animate(Control controlToAnimate)
    {
        // ....

        var finalWidth = CalculateFinalWidth();
        var finalHeight = CalculateFinalHeight();

        _animation1.To = finalWidth;
        _animation3.To = finalHeight;
        _myStoryboard.Begin(controlToAnimate);
    }
}

当我想为某些东西制作动画时,我会调用我的类Animate()的实例。MyAnimationClass

想法?

4

1 回答 1

2

这最终成为一个非常简单的修复。由于FillBehavior控制了属性值,该值没有改变。但是,简单地更改它FillBehavior.Stop并不能解决我的问题,因为当我的非动画代码设置宽度/高度时,下次我的动画运行时,它会动画到我想要的宽度/高度,然后默认回到设置的高度。这是通过在计算之后和动画之前设置控件的宽度/高度来解决的:

public void Animate(Control controlToAnimate)
{
    // ....

    var finalWidth = CalculateFinalWidth();
    var finalHeight = CalculateFinalHeight();

    // set values here so after animation they stay
    controlToAnimate.Width = finalWidth;
    controlToAnimate.Height = finalHeight;

    _animation1.To = finalWidth;
    _animation3.To = finalHeight;
    _myStoryboard.Begin(controlToAnimate);
}
于 2013-09-26T20:12:49.973 回答