Fondamentalmente, voglio creare un gruppo di forme e renderle animate. Così mi è venuta con le seguenti classe personalizzata:Windows 8 - Animazione di proprietà personalizzate nel code-behind
public class FunkyShape : DependencyObject
{
public double Animator
{
get { return (double)GetValue(AnimatorProperty); }
set { SetValue(AnimatorProperty, value); }
}
public static readonly DependencyProperty AnimatorProperty =
DependencyProperty.Register("Animator", typeof(double), typeof(FunkyShape),
new PropertyMetadata(0, new PropertyChangedCallback(Animator_Changed)));
private static void Animator_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
double delta = (double)e.NewValue - (double)e.OldValue;
((FunkyShape)d).ProcessDelta((double)e.NewValue, delta);
}
private void ProcessDelta(double val, double delta)
{
Holder.Width = val;
Holder.Height = val;
// Keep shape centered
HolderPosition.X = delta/2;
HolderPosition.Y = delta/2;
}
private Shape Holder;
public TranslateTransform HolderPosition
{
get { return (TranslateTransform)Holder.RenderTransform; }
}
public FunkyShape(Canvas playground, Shape shapeToInit)
{
Holder = shapeToInit;
Holder.Width = 10;
Holder.Height = 10;
Holder.Fill = new SolidColorBrush(Colors.Blue);
Holder.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center;
Holder.RenderTransform = new TranslateTransform()
{
X = 500,
Y = 500
};
Holder.RenderTransformOrigin = new Point(0.5, 0.5);
// init done
playground.Children.Add(Holder);
Animate();
}
public void Animate()
{
DoubleAnimation g1 = GrowAnimation();
Storyboard sb = new Storyboard();
Storyboard.SetTarget(g1, this);
// CAN'T FIND ANIMATOR PROPERTY
Storyboard.SetTargetProperty(g1, "Animator");
sb.Children.Add(g1);
sb.Begin(); // THROWS EXCEPTION
}
private static DoubleAnimation GrowAnimation()
{
DoubleAnimation growAnimation = new DoubleAnimation();
growAnimation.Duration = TimeSpan.FromMilliseconds(3000);
growAnimation.From = 0;
growAnimation.To = 100;
growAnimation.AutoReverse = true;
growAnimation.EnableDependentAnimation = true;
growAnimation.RepeatBehavior = new RepeatBehavior(5);
return growAnimation;
}
}
Tuttavia, quando provo fare un'istanza della classe e aggiungendolo alla tela, ottengo Eccezione - Storyboard.Being() genera e mi dice che impossibile trovare la proprietà Animator.
Quindi, cosa sto sbagliando?
MODIFICA: Dopo 3 modifiche al codice, non funziona ancora; Ottengo errore "Impossibile risolvere TargetProperty Animator su oggetto specificato". Quindi se qualcuno conosce la risposta, per favore aiutateci modificando il codice. Grazie!
EDIT: OK, dopo 24 ore di sbattere la testa contro il muro c'è qualche progresso - se aggiungo forma attraverso XAML si anima, ma se lo aggiungo con il codice dietro (Canvas.Children.Add), è non funziona Fammi vedere se riesco a capire perché.
Questo è esattamente lo stesso problema Im fronte atm. Se trovi una soluzione, per favore postala qui, anch'io lo farò;) –
Penso che questo sia correlato: http://www.charlespetzold.com/blog/2012/03/The-Animated-Pie-Slice-in- Windows-8.html – kape123
Bene, questo articolo è correlato a questo http://blog.jerrynixon.com/2012/06/windows-8-animated-pie-slice.html –