2012-06-29 7 views
15

Il seguente codice funziona bene in Silverlight:WinRT/Metro Animazione in code-behind

private void Button_Click_1(object sender, RoutedEventArgs e) 
{  
    Storyboard storyboard = new Storyboard(); 
    DoubleAnimation doubleAnimation = new DoubleAnimation(); 
    doubleAnimation.From = 50; 
    doubleAnimation.To = 100; 
    doubleAnimation.RepeatBehavior = RepeatBehavior.Forever; 
    doubleAnimation.AutoReverse = true; 
    doubleAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(200)); 
    storyboard.Children.Add(doubleAnimation); 
    Storyboard.SetTarget(doubleAnimation, button1); 
    Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Width")); 
    storyboard.Begin(); 
} 

In WinRT/Metro ha bisogno di una piccola modifica per rendere più compilare:

Storyboard.SetTargetProperty(doubleAnimation, "Width"); 

ma quando si eseguilo, non succede niente.

Se si modifica la proprietà da "Larghezza" a "Opacità" (modificare anche Da = 0 e A = 1) che funziona.

Qual è il problema con "Larghezza"?

+0

Perché non hai usato il 'EventTrigger' http://msdn.microsoft.com/en-us/library/system.windows.eventtrigger.aspx per spostare tutto questo codice su xaml? – RredCat

+2

@RredCat: per quanto ne so, non abbiamo EventTrigger in WinRT. –

risposta

20

È necessario aggiungere il seguente:

doubleAnimation.EnableDependentAnimation = true; 

che sembra risolvere il problema .

+0

Grazie amico, dopo aver tirato un sacco di capelli! – legends2k

+2

http://msdn.microsoft.com/en-us/library/windows/apps/xaml/jj819807#dependent spiega il motivo – legends2k

+0

Molto utile, grazie! –

1

non sono sicuro, ma tenta di utilizzare:

Storyboard.SetTargetProperty(doubleAnimation, Button.WidthProperty); 

Invece di

Storyboard.SetTargetProperty(doubleAnimation, "Width");