Duration Duration Duration Struct
Represents the duration of time that a Timeline is active, or more generally represents a duration of time that also supports two special values Automatic and Forever.
public struct Duration
public struct Duration
Public Structure Duration
<object property="[days.]hours:minutes:seconds[.fractionalSeconds]"/> -or- <object property="Automatic" .../> -or- <object property="Forever" .../>
Windows 10 (introduced v10.0.10240.0)
Windows.Foundation.UniversalApiContract (introduced v1)
A Duration value is used for these properties:
- Timeline.Duration (can be set on either a Storyboard or an animation)
- MediaElement.NaturalDuration (this usage isn't part of the storyboarded animation scenario; all the others are) For more info on how to use a Duration as part of a Timeline, including code examples, see Storyboarded animations or Timeline.Duration.
The most common way to use a Duration value in the Windows Runtime is to set it using a XAML attribute. When you set a value in XAML, you're supplying a string, and the string is parsed using the hours:minutes:seconds string format and its variants as is described in the XAML Syntax sections near the beginning of this reference topic.
Specifying a Duration using a string that resembles an integer, without any literal characters used in the hours:minutes:seconds string format 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, with literal : characters as separators between hours and minutes, and between minutes and seconds. For example, to specify a Duration of five seconds, the correct string for either or a XAML attribute value is "0:0:5" ("0:0:05" is equivalent).
If you're using a Duration in code, a Duration uses a definition of time that is also used by the TimeSpan structure. The TimeSpan structure is represented by System.TimeSpan if you are programming using C# or Microsoft Visual Basic, or Windows.Foundation.TimeSpan if you are programming using C++.
- The C# or Microsoft Visual Basic System.TimeSpan has a Parse method that use hours:minutes:seconds string format. If you need to create a Duration value in code you can call the Duration constructor and provide the System.TimeSpan argument by calling TimeSpan.Parse with an hours:minutes:seconds string. Always use the "en-us" culture for parsing this string, because that's how XAML interprets the string format, and you shouldn't be using culture-specific inputs for animating timings anyways.
- The C++ Windows.Foundation.TimeSpan doesn't support a way to create it in an hours:minutes:seconds string format. You'll have to use DurationHelper.FromTimeSpan, and do the conversion yourself for how hours:minutes:seconds converts to the C++ Windows.Foundation.TimeSpan data value, which is a value in milliseconds.
Notes on XAML syntax
In the grammar shown in the XAML attribute usage,  indicates optional values, the  (square brackets) are not literals. The : (colon) and . (period) characters are both literals, and delimit the hⓂ️s string form of a common time span, or the optional days and fractionalSeconds values.
Projection and members of Duration
If you are using a Microsoft .NET language (C# or Microsoft Visual Basic), or in Visual C++ component extensions (C++/CX), then Duration has non-data members available, and its data members are exposed as read-write properties, not fields. Duration exposes several operators, including comparison operators.
For Microsoft .NET, Duration exposes TimeSpan.Parse for its TimeSpan, Implicit and UnaryPlus operators, and Add and Subtract methods. These aren't available from the structure in Visual C++ component extensions (C++/CX) but you can use equivalent DurationHelper methods for some of these.
If you are programming with C++ using the Windows Runtime Template Library (WRL), then only the data member fields exist as members of Duration, and you cannot use the utility methods or properties listed in the members table. WRL code can access similar utility methods that exist on the DurationHelper class. For example, you can call DurationHelper.Compare to compare two C++ Duration values. For more info, see DurationHelper.
Automatic and Forever
The Automatic value applied in either XAML or code results in different behavior on a Storyboard as opposed to an animation. For Storyboard, the Automatic value sets the effective time span to be equal to the end time of its longest-running child animation, such that no clipping occurs for any child animation. For animations, the Automatic value results in the behavior whereby the animation runs with a time span of 1 second (0:0:1). This behavior is seldom desirable as a final result, but it enables you to see the running animation during testing, before you have established a final time span.
Using Forever for an animation is a deprecated usage, and is seldom used. This results in an animation that never advances 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
TimeSpan TimeSpan TimeSpan
The TimeSpan value component.
public field TimeSpan TimeSpan
public field TimeSpan TimeSpan
Public Field TimeSpan