ivamax9
ivamax9

Reputation: 2629

Using DoubleAnimation code behind in WinRT (XAML)

I have some problem. This is my code behind:

var s = new Storyboard();
var dAnimation = new DoubleAnimation();
dAnimation.RepeatBehavior = RepeatBehavior.Forever;
dAnimation.AutoReverse = true;
dAnimation.EnableDependentAnimation = true;
dAnimation.From = 200;
dAnimation.To = 300;
Storyboard.SetTarget(dAnimation, viewBox);
Storyboard.SetTargetProperty(dAnimation, "Width");
dAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(2000));
s.Children.Add(dAnimation);
s.Begin();

viewBox is on Canvas and has just a few property: Canvas.Left, Canvas.Top, Height and Width. Code from codebehind working great with Opacity, but not working with Width or Height. Codebehind work when user pointer is entered. I want do it without triggers. Found some solutions for Silverlight and WPF:

WinRT/Metro Animation in code-behind

they are not working. I dont undersstand why it work with opacity and didn't work with Width and Height Any ideas?

Upvotes: 0

Views: 2043

Answers (2)

Masahiko Miyasaka
Masahiko Miyasaka

Reputation: 191

I didn't check ViewBox,but I can with DoubleAnimation for Grid.

Code is like below

var s = new Storyboard();
var widthAnim = new DoubleAnimation()
{
    To = w,
    Duration=new Duration(TimeSpan.FromMilliseconds(duration))
};
widthAnim.EnableDependentAnimation = true;
Storyboard.SetTarget(widthAnim,elem);
Storyboard.SetTargetProperty(widthAnim,"Width");
s.Children.Add(widthAnim);
s.Begin();

Upvotes: 0

Shawn Kendrot
Shawn Kendrot

Reputation: 12465

You cannot animate Width with a DoubleAnimation, you need to use a DoubleAnimationUsingKeyFrames.

var animation = new DoubleAnimationUsingKeyFrames();
var frame = new EasingDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(2000)), Value = 300});
animation.KeyFrames.Add(frame);
Storyboard.SetTargetProperty(animation, "(FrameworkElement.Width)");
Storyboard.SetTarget(animation, viewBox);
Storyboard.Children.Add(animation);

Upvotes: 2

Related Questions