2

I am creating a Windows 8 App, and I need to animate the positions of buttons. The buttons are created dynamically, so XAML is not an option. The buttons' positions are calculated according to the Margin property, which are of Thickness type. I've found that ThicknessAnimation does exist in WPF but not in Windows Store apps. How do I animate a dynamically created button's position? For many reasons, I can't use RenderTransform too as I have too much events and properties depending on the actual position (Margin) of the object. I've found this question Windows 8 store app c# animating margin but unfortunatelly it is closed. I know animations in general (create a storyboard, add animations, add target object and properties, begin the storyboard) but I don't know how to apply it to the Thickness property.

Thanks,

Can.

Community
  • 1
  • 1
Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389
  • I've done very little with animation, so take this comment for what it is. This [answer](http://stackoverflow.com/a/7292762/1822514) seems to do a thickness animation for Windows Phone 7 using a storyboard. Apparently the thickness animation doesn't exist for WP7 either. It is in xaml, but could provide a clue for you as to how to do it in code. – chue x Jun 13 '13 at 16:20
  • It is actually not really good idea to animate Margin, because this will require to rearrange all other elements on. Why you don't want to use http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.media.translatetransform.aspx ? – outcoldman Jun 13 '13 at 18:01
  • @outcoldman there are many values depending on the value of margin property. transform is just visual (as far as I know), I need the actual values of the position of the buttons for many calculations, and transform would make it really complex. (all the controls are also in a viewbox that also scales the controls, transforms just mess everything up). anyway, i ended up animating the margin property using a dispatchertimer manually. – Can Poyrazoğlu Jun 15 '13 at 20:50

1 Answers1

0

You can handle CompositionTarget.Rendering event although you will have to manage all the positioning mathematics yourself which can be difficult in case of many objects - this is only an example and looks a bit strange but works:

public MainPage()
{
    this.InitializeComponent();
    CompositionTarget.Rendering += OnCompositionTargetRendering;
}

private void OnCompositionTargetRendering(object sender, object e)
{
    RenderingEventArgs args = e as RenderingEventArgs;
    double t = (args.RenderingTime.TotalMilliseconds) % 20;
    button1.Margin = new Thickness(t);
}
  • I could also animate with a timer in a similar fashion. but I'm trying to find a more convenient way. what I don't understand is that does really no one animate ui elements is windows store apps? seems that no one really does it. – Can Poyrazoğlu Jun 13 '13 at 21:48