A new home

I’ve made the move to WordPress!

You should be automatically redirected in 5 seconds. Please visit World Wide Code on Wordpress

Hope to see you there!
Showing posts with label Storyboard. Show all posts
Showing posts with label Storyboard. Show all posts

Tuesday, February 8, 2011

Creating a collapsible panel using storyboard

Every time when you want to create a pane that can collapse or expand, you have to write either a Storyboard in xaml or some other animation logic. This can be a tedious task and has many pitfalls. Some of them being
  1. Change in width of the pane requires rewiring of the storyboard.
  2. Consistency in the collapse/expand duration of the pane.
  3. Flexibility in changing the pane from vertical to horizontal.
To solve all of these and many more problems that may arise we need to build a custom PaneManager that will do this for us when required.
At first we must create a class in your Silverlight application, lets call it PaneManager, with the following members
public class PaneManager
{
#region Public Members
public TimeSpan Interval { get; set; }
public double Increment { get; set; }
public double MinValue { get; set; }
public double MaxValue { get; set; }
public Grid Grid { get; set; }
public int Index { get; set; }
public GridUnitType GridUnitType { get; set; }
public Orientation Alignment { get; set; }
#endregion
}
Let us go over each member in detail.
  • Interval - This defines the duration to wait between successive calls to make the pane transition.
  • Increment - This is the value by which the pane's width or height must change.
  • MinValue - This defines the minimum threshold of the pane's width or height.
  • MaxValue- This defines the maximum threshold of the pane's width or height.
  • Grid - This is the parent Grid that contains the desired pane.
  • Index - This is the index of the row or column containing the pane.
  • GridUnitType - this is the unit of measurement of the Grid's row/column.
  • Alignment - This specifies whether the pane transition is to be done on a row or column.
We will also need to add the following members and events to the PaneManager class
#region Private Members
private Storyboard _storyboard = new Storyboard();
#endregion
#region Public Event
public event EventHandler Completed
{
add { _storyboard.Completed += value; }
remove { _storyboard.Completed -= value; }
}
#endregion
We have until now created the basic structure of the PaneManager without adding any functionality as such. To make this class usable,

Tuesday, February 1, 2011

DispatcherTimer vs Storyboard


Many of you must have heard and used the Dispatcher Timer class in Silverlight for all sorts of things. One common usage is for animation purpose, which is an incorrect practice.
Timers are not guaranteed to execute exactly when the time interval occurs, but they are guaranteed to not execute before the time interval occurs. The Dispatcher Timer is also influenced by the UI Thread, so if any heavy rendering is to be done can cause you animation to be rigid and many a times slow.
Silverlight also has a class called Storyboard which can solve these issues and is a better solution than the Dispatcher Timer. The Storyboard has simple properties like TargetProperty, TartgetName which specify which property of which element you would like to animate. The Storyboard supports several animations namely, Color Animation, Double Animation, Point Animation andObject Animation Using Key Frames
Using a Storyboard in XAML


<StackPanel>
  <StackPanel.Resources>
    <!-- Animates the rectangle's opacity. -->
    <Storyboard x:Name="myStoryboard">
      <DoubleAnimation
        Storyboard.TargetName="MyAnimatedRectangle"
        Storyboard.TargetProperty="Opacity"
        From="1.0" To="0.0" Duration="0:0:1"
        AutoReverse="True" RepeatBehavior="Forever" />
    </Storyboard>
  </StackPanel.Resources>

  <Rectangle
    x:Name="MyAnimatedRectangle"
    Width="100" Height="100" Fill="Blue" />
</StackPanel>
Heres a link to create an Animation in Procedural Code if you need.


In Conclusion
I would suggest avoid using the Dispatcher Timer whenever possible. If you have any queries, you can get back to me.


Related Links