Page view counter

Did You Know... That You Can Create A Timer Using XAML Animation?

To do so, create a Storyboard and mark that you will handle the Completed event.

<Storyboard Completed="OnTimerCompleted">

Put in the simplest Animation you can manage (name it, so that you can set the duration programmatically).

<DoubleAnimation x:Name="TimerDuration"

make sure you give it a duration, and point it at the simplest object you can create.

Duration="00:00:01.00" 
Storyboard.TargetName="MyRect"
Storyboard.TargetProperty="Height"

When the duration expires you can take action and, if you wish, reset the timer.

function OnTimerCompleted...

Here's a complete sample...

<Canvas xmlns="http://schemas.microsoft.com/client/2007" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
    <Canvas.Resources>
        <Storyboard 
            x:Name="myTimer" 
            Completed="OnTimerCompleted">
            <DoubleAnimation x:Name="TimerDuration"
                Duration="00:00:01.00" 
                Storyboard.TargetName="MyRect"
                Storyboard.TargetProperty="Height" />
        </Storyboard>
    </Canvas.Resources>

    <Canvas>
        <Rectangle x:Name="MyRect"/>
    </Canvas>

</Canvas>

Here's the javascript...

MyDemo.Scene.prototype =
{
    handleLoad: function(plugIn, userContext, rootElement) 
    {
        // set the timer duration
        timer = plugIn.content.findName("TimerDuration"); 
        timer.Duration="00:00:05.00";
        
        // start the timer
        myStoryboard = plugIn.content.findName("myTimer"); 
        myStoryboard.begin();
    }
}

function OnTimerCompleted(sender, args)
{
       alert ("Timer completed!");
       myStoryboard = sender.findName("myTimer");
       myStoryboard.begin();  //restart timer
}
Published Sunday, December 09, 2007 4:39 PM by jesseliberty

Comments

# Did You Know... That You Can Create A Timer Using XAML Animation? - Jesse Liberty - Silverlight Geek

Pingback from  Did You Know... That You Can Create A Timer Using XAML Animation? - Jesse Liberty - Silverlight Geek

# re: Did You Know... That You Can Create A Timer Using XAML Animation?

FYI...you can put the Duration on the Storyboard and not even have a dummy DoubleAnimation child node.

Sunday, December 09, 2007 6:05 PM by Bill Reiss

# Did You Know... That You Can Create A Timer Using XAML Animation?

To do so, create a Storyboard and mark that you will handle the Completed event. &lt;Storyboard Completed=&quot;OnTimerCompleted&quot;&gt;

Sunday, December 09, 2007 6:22 PM by Test

# re: Did You Know... That You Can Create A Timer Using XAML Animation?

how can I take the seconds value and update a textblock with the current second on each passing second?

Sunday, December 09, 2007 8:37 PM by gjhdigital

# re: Did You Know... That You Can Create A Timer Using XAML Animation?

Much Simpler!! Thank you Bill Reiss....

Here's the much simplified code:

<Canvas xmlns="schemas.microsoft.com/.../2007" xmlns:x="schemas.microsoft.com/.../xaml">

   <Canvas.Resources>

       <Storyboard x:Name="myTimer" Completed="OnTimerCompleted" Duration="00:00:01.00" />

   </Canvas.Resources>

</Canvas>

The Javascript then becomes...

MyDemo.Scene.prototype =

{

  handleLoad: function(plugIn, userContext, rootElement)

 {

   // set the timer duration

   timer = plugIn.content.findName("myTimer");

   timer.Duration="00:00:05.00";

   timer.begin();

   }

 }

function OnTimerCompleted(sender, args)

{

 alert ("Timer completed!");

 myStoryboard = sender.findName("myTimer");

 myStoryboard.begin();

}

Sunday, December 09, 2007 9:07 PM by jesseliberty

# Del.icio.us Links for 12/10/2007

Monday, December 10, 2007 10:56 AM by Frank La Vigne

# Del.icio.us Links for 12/11/2007

Tuesday, December 11, 2007 6:41 AM by Frank La Vigne

# Thoughts on Silverlight &raquo; BIT-101 Blog

Pingback from  Thoughts on Silverlight &raquo; BIT-101 Blog

Tuesday, December 11, 2007 6:30 PM by Thoughts on Silverlight » BIT-101 Blog

# Silverlight Cream for December 11, 2007 - 2 -- #151

First of two-parts of Cream I&#39;ve found in my blog-reading: Koen of FirsFloor demonstrates a Slideshow

Wednesday, December 12, 2007 12:30 AM by Community Blogs

# Sample Code: Silverlight 1.0-enabled Quiz

A Customer who asked if a quiz-type application can be enabled by Silverlight. The answer is yes (obviously

Sunday, March 16, 2008 3:59 AM by