Today I was in the process of making my own copy-paste Analog clock. I went aruond the internet and found this pretty thing. Of course I could have used that code and gone on with life. But that was not the point of the exercise. I wanted to learn something of this.
I liked the way he did the animations, no more need for a timer to update the angles, all is done in XAML.
So his project is Silverlight. And he has this to aniamte the hands of the clock.
xml
<Canvas.Resources>
<Storyboard x:Name="SecondsHandStoryboard">
<DoubleAnimation From="0" To="360" Duration="00:01:00" RepeatBehavior="Forever"
Storyboard.TargetProperty="(Polygon.RenderTransform).(RotateTransform.Angle)" Storyboard.TargetName="SecondHand"/>
</Storyboard>
</Canvas.Resources>
and this in his codebehind
csharp
private void SecondsHandCanvas_Loaded( object sender, RoutedEventArgs e )
{
this.SecondsHandStoryboard.Begin();
this.SecondsHandStoryboard.Seek( DateTime.Now.TimeOfDay );
}
vbnet
Private Sub SecondsHandCanvas_Loaded( ByVal sender As object, ByVal e As RoutedEventArgs)
Me.SecondsHandStoryboard.Begin()
Me.SecondsHandStoryboard.Seek( DateTime.Now.TimeOfDay );
End Sub
Cool I can just copy paste that and get on with it.
But Silverlight and WPF aren’t exactly the same they just look the same.
In WPF A name is no good for a resource apparently. It needs a Key.
So the above XAML becomes this instead of the above Silverlight code.
xml
<Canvas.Resources>
<Storyboard x:Key="HourHandStoryboard">
<DoubleAnimation From="0" To="360" Duration="12:00:00" RepeatBehavior="Forever" Storyboard.TargetProperty="(Polygon.RenderTransform).(RotateTransform.Angle)" Storyboard.TargetName="HourHand"/>
</Storyboard>
</Canvas.Resources>
And then you find out that the code you have doesn’t work either. YOu need to change it to this.
vbnet
Private Sub HoursHandCanvas_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Dim s = CType(Me.HoursHandCanvas.Resources("SecondHandStoryboard"), Storyboard)
s.Begin()
s.Seek(DateTime.Now.TimeOfDay)
End Sub
csharp
private void HoursHandCanvas_Loaded(System.Object, sender,System.Windows.RoutedEventArgs e)
{
var s = (StoryBoard)this.HoursHandCanvas.Resources("SecondHandStoryboard");
s.Begin();
s.Seek(DateTime.Now.TimeOfDay);
}
And BTW doing s.Begin(Me, True) does not work whatever MSDN say.