Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Duration Structure

Represents the duration of time that a Timeline is active.

Namespace:  System.Windows
Assembly:  PresentationCore (in PresentationCore.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
[TypeConverterAttribute(typeof(DurationConverter))]
public struct Duration
<object property="[days.]hours:minutes:seconds[.fractionalSeconds]"/>- or -<object property="[days.]hours:minutes"/>- or -<object property="Automatic"/>- or -<object property="Forever"/>

XAML Values

days

System.Int32

A value greater than or equal to 0 that describes the number of days spanned by this duration.

hours

System.Int32

A value between 0 and 23 that represents the number of hours spanned by this duration.

minutes

System.Int32

A value between 0 and 59 that represents the number of minutes spanned by this duration.

seconds

System.Int32

A value between 0 and 59 that represents the number of seconds spanned by this duration

fractionalSeconds

System.Int32

A value consisting of 1 to 7 digits that represents fractional seconds.

The Duration type exposes the following members.

  NameDescription
Public methodDurationInitializes a new instance of the Duration structure with the supplied TimeSpan value.
Top
  NameDescription
Public propertyStatic memberAutomaticGets a Duration value that is automatically determined.
Public propertyStatic memberForeverGets a Duration value that represents an infinite interval.
Public propertyHasTimeSpanGets a value that indicates if this Duration represents a TimeSpan value.
Public propertyTimeSpanGets the TimeSpan value that this Duration represents.
Top
  NameDescription
Public methodAddAdds the value of the specified instance of Duration to the value of the current instance.
Public methodStatic memberCompareCompares one Duration value to another.
Public methodEquals(Duration)Determines whether a specified Duration is equal to this instance of Duration.
Public methodEquals(Object)Determines whether a specified object is equal to an instance of Duration. (Overrides ValueType.Equals(Object).)
Public methodStatic memberEquals(Duration, Duration)Determines whether two instances of Duration are equal.
Public methodGetHashCodeGets a hash code for this instance. (Overrides ValueType.GetHashCode().)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodStatic memberPlusReturns the specified instance of Duration.
Public methodSubtractSubtracts the value of the specified instance of Duration from this instance.
Public methodToStringConverts an instance of Duration to a String representation. (Overrides ValueType.ToString().)
Top
  NameDescription
Public operatorStatic memberAdditionAdds two instances of Duration together.
Public operatorStatic memberEqualityDetermines whether two instances of Duration are equal.
Public operatorStatic memberGreaterThanDetermines if one instance of Duration is greater than another.
Public operatorStatic memberGreaterThanOrEqualDetermines whether an instance of Duration is greater than or equal to another instance.
Public operatorStatic memberImplicit(TimeSpan to Duration)Implicitly creates a Duration from a given TimeSpan.
Public operatorStatic memberInequalityDetermines if two instances of Duration are not equal.
Public operatorStatic memberLessThanDetermines if the value of one instance of Duration is less than the value of another instance.
Public operatorStatic memberLessThanOrEqualDetermines if the value of one instance of Duration is less than or equal to the value of another instance.
Public operatorStatic memberSubtractionSubtracts the value of one instance of Duration from another.
Public operatorStatic memberUnaryPlusReturns the specified instance of Duration.
Top

A Timeline represents a segment of time and the length of that segment is determined by the timeline's Duration. When a Timeline reaches the end of its duration, it stops playing. If the Timeline has child timelines, they stop playing as well. In the case of an animation, the Duration specifies how long the animation takes to transition from its starting value to its ending value.

You can specify a Duration with a specific, finite time or the special values Automatic or Forever. An animation's duration must always be a time value, because an animation must always have a defined, finite length—otherwise, the animation would not know how to transition between its target values. Container timelines (TimelineGroup objects), such as Storyboard and ParallelTimeline, have a default duration of Automatic, which means they automatically end when their last child stops playing.

In the following example, the width, height and fill color of a Rectangle is animated. Durations are set on animation and container timelines resulting in animation effects including controlling the perceived speed of an animation and overriding the duration of child timelines with the duration of a container timeline.

<Page 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <StackPanel Margin="20">

    <Rectangle Width="100" Height="100" Name="myRectangle">
      <Rectangle.Fill>
        <SolidColorBrush x:Name="MyAnimatedBrush" Color="Black" />
      </Rectangle.Fill>
      <Rectangle.Triggers>

        <!-- Animates the rectangle fill to yellow and width to 300. -->
        <EventTrigger RoutedEvent="Rectangle.Loaded">
          <BeginStoryboard>

            <!-- By default, TimelineGroup objects like Storyboard and ParallelTimeline have 
            a Duration of "Automatic". A TimelineGroup's automatic duration encompasses its 
            last-ending child. In this example, there is only one child of the Storyboard, the
            ParallelTimeline, so when the ParallelTimeline ends, the Storyboard duration will
            automatically end. -->
            <Storyboard>

              <!-- This ParallelTimeline has overriden its default duration of "Automatic" with
              a finite duration of half a second. This will force this Timeline to end after half a
              second even though its child Timelines have a longer duration (2 and 4 seconds respectively). 
              This cuts off the animation prematurely and the rectangle's fill will not go all the way to 
              yellow nor will the rectangle width get all the way to 300. Again, the default duration of a
              ParallelTimeline is "Automatic" so if you remove the finite duration, the ParallelTimeline
              will wait for its child timelines to end before it ends. -->

              <!-- Note: To specify a finite time in XAML, use the syntax of "days:hours:seconds". As mentioned,
              this ParallelTimeline has a duration of half a second. -->
              <ParallelTimeline Duration="0:0:0.5">

                <!-- For Animation Timelines like DoubleAnimation, the duration is one factor that
                determines the rate at which an animation appears to progress. For example, the DoubleAnimation
                below that animates the rectangle height will complete in only one second while the animation
                that animates the width willwill complete in 2 seconds which is relatively fast compared to the DoubleAnimation
                which animates the rectangle width over 4 seconds. -->
                <DoubleAnimation
                  Storyboard.TargetName="myRectangle"
                  Storyboard.TargetProperty="Height"
                  To="300" Duration="0:0:1" />

                <DoubleAnimation
                  Storyboard.TargetName="myRectangle"
                  Storyboard.TargetProperty="Width"
                  To="300" Duration="0:0:4" />

                <ColorAnimation
                  Storyboard.TargetName="MyAnimatedBrush"
                  Storyboard.TargetProperty="Color"
                  To="Yellow" Duration="0:0:2" />

              </ParallelTimeline>
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger>

      </Rectangle.Triggers>
    </Rectangle>
  </StackPanel>
</Page>

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft. All rights reserved.