开发者

How to make the starting animation value take place immediately

开发者 https://www.devze.com 2023-03-14 03:34 出处:网络
I\'m programmatically starting a DoubleAnimation on a dependency property (double, obviously). Here\'s a truncated version of the code:

I'm programmatically starting a DoubleAnimation on a dependency property (double, obviously). Here's a truncated version of the code:

// Using a DependencyProperty as the backing store for TestDoubleValue.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty TestDoubleValueProperty =
    DependencyProperty.Register("TestDoubleValue", typeof(double), typeof(MainWindow), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsArrange));

void BeginAnimation()
{
    Storyboard sb = new Storyboard();
    DoubleAnimation da = new DoubleAnimation(0, 100, new Duration(TimeSpan.FromMilliseconds(1000)));
    Storyboard.SetTarget(da, this);
    Storyboard.SetTargetProperty(da, new PropertyPath(TestDoubleValueProperty));
    sb.Children.Add(da);
    sb.FillBehavior = FillBehavior.Stop;
    sb.Begin();
    SetValue(TestDoubleValueProperty, 100.0);
}

However, the behavior I want to achieve is this:

  • The animation should animate from 0 to 100
  • Before the animation begins, the local value should be 100
  • Inside the next call to ArrangeOverride, calling GetValue(...) on that property should return 0, not 100, even though the local value is 100.

The current behavior I am seeing is as follows:

  • In subsequent calls to ArrangeOverride, the values are like: 100, 0, 0.52647, 0.92353, ...
  • That first 100 is what I don't like. I want it to start as 0. In other words, I want the local value to be 100, but I want the animated value to start at 0 immediately not a few calls later.

How do I achieve this behavior?

My current thinking is that I would do something like this:

SetValue(TestDoubleValueProperty, 100.0);
Storyboard sb = new Storyboard();
DoubleAnimation da = new DoubleAnimation(0, 100, new Duration(TimeSpan.FromMilliseconds(1000)));
Storyboard.SetTarget(da, this);
Storyboard.SetTargetProperty(da, new PropertyPath(TestDoubleValueProperty));
sb.Children.Add(da);
sb.Co开发者_JAVA百科mpleted += (s, arg) => { sb.Remove(); };
sb.Begin();
SetCurrentValue(TestDoubleValueProperty, 0.0);

This seems to work, but it seems somewhat hacky. Is there a better way?

0

精彩评论

暂无评论...
验证码 换一张
取 消