2014-02-25 34 views
0

我有一些問題。這背後是我的代碼:WinRT(XAML)後面使用DoubleAnimation代碼

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(); 

viewBoxCanvas和只有幾個屬性:Canvas.Left,Canvas.Top,高度和寬度。來自代碼隱藏的代碼與Opacity很好地工作,但不與WidthHeight一起使用。輸入用戶指針後的代碼隱藏工作。我想要沒有觸發器。發現Silverlight和WPF一些解決方案:

WinRT/Metro Animation in code-behind

他們沒有工作。我不認爲它爲什麼不透明,並沒有與WidthHeight任何想法?

+0

你應該立即鏈接到這個回答你的問題,我(和其他人)沒有寫一個相同的答案。 –

回答

2

您無法使用DoubleAnimation動畫寬度,您需要使用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); 
0

我沒有檢查ViewBox,但我可以使用DoubleAnimation for Grid。

代碼就像下面

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(); 
相關問題