次の方法で共有


IXRTimeline::SetDuration (Compact 2013)

3/28/2014

This method sets the total length of time that this timeline will play.

Syntax

virtual HRESULT STDMETHODCALLTYPE SetDuration(
    XRDuration* pDuration
) = 0;

Parameters

  • pDuration
    [in] Pointer to an XRDuration structure that indicates the total length of time that this timeline will play.

Return Value

Returns an HRESULT that indicates success or failure.

Remarks

The simple duration of a timeline is the time for a single forward iteration, whereas the total play time includes all repetitions.

If you set a new duration value by calling SetDuration on a running animation, the new value is immediately applied and includes the time that has already progressed. For example, entering a smaller value causes the remainder of the animation to immediately speed up and entering a larger value causes the remainder of the animation to immediately slow down. Entering a total duration value that is less than the time that has already progressed causes the animation to end prematurely. For more information, see the code example below.

If the duration value for an animation is not explicitly set either in C++ code or in the source XAML for your application, it will be null. Leaving this default value has the same effect on an animation as setting the pDuration value to XRDuration::DurationType_Automatic. If IXRTimeline::GetAutoReverse retrieves a value of true, the timeline plays for double the length specified by pDuration.

This method applies both to specific animations and to the parent storyboard. For an animation in a storyboard, make sure that you do not unintentionally cause the durations of child animations to end prematurely when you call IXRTimeline::SetDuration.

For information about how to make a timeline repeat itself, see IXRTimeline::SetRepeatBehavior.

Note    If you set an XRDuration structure with its DurationType set to XRDuration::DurationType_Forever, an exception will be thrown.

Example

The following example code shows how to use the XRDuration and XRTimeSpan structures to set a value for an animation's duration that is larger than the current duration value while the animation is playing.

Important

For readability, the following code example does not contain security checking or error handling. Do not use the following code in a production environment.

#include "windows.h"
#include "XamlRuntime.h"
#include "XRPtr.h"


void ChangeAnimationDuration(IXRStoryboard* pColorStoryboard)
{
     // Initialize variables
     IXRTimelineCollectionPtr pChildAnimations;
     IXRColorAnimationPtr pAnimation;
     UINT index = 0;
     XRDuration currentDur;


     // Begin the storyboard
     pColorStoryboard->Begin();

     // Get the first animation from the storyboard's collection
     pColorStoryboard->GetChildren(&pChildAnimations);
     pChildAnimations->GetItem(0, &pAnimation);

     // Get the timespan of its current duration
     pAnimation->GetDuration(&currentDur);


    // If the timespan is less than 5 seconds, set it to 5 seconds, 
    // which causes the animation to run at a slower rate to fill its longer duration

     if (currentDur.TimeSpan.Ticks < (XRTimeSpan::TicksPerSecond * 5))
     {
          XRDuration newDur;
          newDur.DurationType = XRDuration::DurationType_TimeSpan;
          newDur.TimeSpan.Ticks = XRTimeSpan::TicksPerSecond * 5;

          pAnimation->SetDuration(&newDur);

     }

}

To run this code, you must have already created an IXRStoryboard object that has a collection of IXRColorAnimation objects, and these animations must already have durations assigned to them. The visual tree must contain the IXRStoryboard object.

.NET Framework Equivalent

System.Windows.Media.Animation.Timeline.Duration

Requirements

Header

XamlRuntime.h

sysgen

SYSGEN_XAML_RUNTIME

See Also

Reference

IXRTimeline
IXRTimeline::GetDuration