Partager via


Duration Structure

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Represents the duration of time that a Timeline is active.

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public Structure Duration
public struct Duration
<object property="[days.]hours:minutes:seconds[.fractionalSeconds]"/>
-or-
<object property="Automatic" .../>
-or-
<object property="Forever" .../>

XAML Values

  • days
    An integer value greater than or equal to 0 that specifies the number of days.

  • hours
    An integer value between 0 and 23 that specifies the number of hours. If you specify a Duration as a XAML attribute, an hours component is required, even if is 0.

  • minutes
    An integer value between 0 and 59 that specifies the number of minutes. Set hours:minutes components as 0:0 if you are setting only a seconds component.

  • seconds
    An integer value between 0 and 59 that specifies the number of seconds. Set hours:minutes components as 0:0 if you are setting only a seconds component.

  • fractionalSeconds
    Optional. A decimal value consisting of 1 to 7 positions past the decimal point, which specifies fractional seconds.

  • Automatic
    The literal string Automatic; see "Automatic and Forever" section in Remarks.

  • Forever
    The literal string Forever; see "Automatic and Forever" section in Remarks.

  • In the grammar, [] indicates optional values, the [] are not literals. The : (colon) and . (period) characters are both literals, and delimit the h:m:s string form of a common timespan, or the optional days and fractionalSeconds values.

The Duration type exposes the following members.

Constructors

  Name Description
Public methodSupported by Silverlight for Windows Phone Duration Initializes a new instance of the Duration structure with the supplied TimeSpan value.

Top

Properties

  Name Description
Public propertyStatic memberSupported by Silverlight for Windows Phone Automatic Gets a Duration value that is automatically determined.
Public propertyStatic memberSupported by Silverlight for Windows Phone Forever Gets a Duration value that represents an infinite interval.
Public propertySupported by Silverlight for Windows Phone HasTimeSpan Gets a value that indicates if this Duration represents a TimeSpan value.
Public propertySupported by Silverlight for Windows Phone TimeSpan Gets the TimeSpan value that this Duration represents.

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows Phone Add Adds the value of the specified Duration to this Duration.
Public methodStatic memberSupported by Silverlight for Windows Phone Compare Compares one Duration value to another.
Public methodSupported by Silverlight for Windows Phone Equals(Duration) Determines whether a specified Duration is equal to this Duration.
Public methodSupported by Silverlight for Windows Phone Equals(Object) Determines whether a specified object is equal to a Duration. (Overrides ValueType.Equals(Object).)
Public methodStatic memberSupported by Silverlight for Windows Phone Equals(Duration, Duration) Determines whether two Duration values are equal.
Protected methodSupported by Silverlight for Windows Phone Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetHashCode Gets a hash code for this object. (Overrides ValueType.GetHashCode().)
Public methodSupported by Silverlight for Windows Phone GetType Gets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows Phone MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodStatic memberSupported by Silverlight for Windows Phone Plus Adds one Duration to this Duration.
Public methodSupported by Silverlight for Windows Phone Subtract Subtracts the specified Duration from this Duration.
Public methodSupported by Silverlight for Windows Phone ToString Converts a Duration to a String representation. (Overrides ValueType.ToString().)

Top

Operators

  Name Description
Public operatorStatic memberSupported by Silverlight for Windows Phone Addition Adds two Duration values together.
Public operatorStatic memberSupported by Silverlight for Windows Phone Equality Determines whether two Duration cases are equal.
Public operatorStatic memberSupported by Silverlight for Windows Phone GreaterThan Determines if one Duration is greater than another.
Public operatorStatic memberSupported by Silverlight for Windows Phone GreaterThanOrEqual Determines whether a Duration is greater than or equal to another.
Public operatorStatic memberSupported by Silverlight for Windows Phone Implicit(TimeSpan to Duration) Implicitly creates a Duration from a given TimeSpan.
Public operatorStatic memberSupported by Silverlight for Windows Phone Inequality Determines if two Duration cases are not equal.
Public operatorStatic memberSupported by Silverlight for Windows Phone LessThan Determines if a Duration is less than the value of another instance.
Public operatorStatic memberSupported by Silverlight for Windows Phone LessThanOrEqual Determines if a Duration is less than or equal to another.
Public operatorStatic memberSupported by Silverlight for Windows Phone Subtraction Subtracts the value of one Duration from another.
Public operatorStatic memberSupported by Silverlight for Windows Phone UnaryPlus Returns the specified Duration.

Top

Remarks

Specifying a Duration by only an integer without any time span literal characters such as : or . will result in a Duration of that number of days! This is seldom the intended result. Usually you specify animation durations in seconds. As such, the Duration string must include preceding 0 values for hours and minutes, along with the appropriate literal : characters as separators between hours, minutes and seconds. For instance, to specify a Duration of five seconds, the Duration string would be "0:0:5" ("0:0:05" is equivalent).

Duration can be set on either a Storyboard or an animation.

Automatic and Forever

Automatic and Forever are values that hold special meaning for a Duration property value, and are represented by the static properties Automatic and Forever in the managed API for Silverlight. For details, see Animation Overview.

The Automatic value applied in either XAML or code results in different behavior on the Storyboard as opposed to an animation. For Storyboard, the Automatic duration time behavior is equal to the end time of its latest ending child animation, such that no clipping of any of the child animation durations occurs. For animations, the Automatic duration is a time span of 1 second (0:0:1). This short duration is seldom desirable, but this default provides you the opportunity to see the animation start during testing. This is better debugging information than never having the animation run, as would occur with a 0 duration.

A Duration of Forever for an animation is part of a deprecated usage, and is seldom used. A Duration of Forever would result in an animation that never advanced from its starting value, no matter what values were provided for From/To, key frames, and so on. If you want an animation to repeat continuously, use RepeatBehavior="Forever", not Duration="Forever".

Duration does not support an object element syntax for Silverlight XAML, and you cannot declare a Duration as a resource dictionary item in XAML. In XAML you should always specify Duration -type properties in the attribute syntax shown here.

JavaScript API Notes

Creating a Duration in the JavaScript API for Silverlight is only possible through a type conversion syntax when setting a property such as Duration, with the value specified as a string. The string is generally specified in same format as TimeSpan. Duration can also take the literals Automatic and Forever.

In the managed API, you can create a Duration using the constructors.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference