语言

VisualTransition 类

定义

表示控件从一个视觉状态过渡到另一个视觉状态时发生的视觉行为。

/// [Microsoft.UI.Xaml.Markup.ContentProperty(Name="Storyboard")]
/// [Windows.Foundation.Metadata.ContractVersion(Microsoft.UI.Xaml.WinUIContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class VisualTransition : DependencyObject
[Microsoft.UI.Xaml.Markup.ContentProperty(Name="Storyboard")]
[Windows.Foundation.Metadata.ContractVersion(typeof(Microsoft.UI.Xaml.WinUIContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public class VisualTransition : DependencyObject
Public Class VisualTransition
Inherits DependencyObject
<VisualStateGroup>
  <!--one or more Visual State elements in the implicit States collection property -->
  <VisualStateGroup.Transitions>
    <VisualTransition>
      singleStoryboard
    </VisualTransition>
    <!--more transitions as above-->
  </VisualStateGroup.Transitions>
</VisualStateGroup>
继承
Object IInspectable DependencyObject VisualTransition
属性

示例

本示例创建一个 VisualTransition,指定当用户将鼠标移离控件时,控件的边框将变为蓝色,然后变为黄色,然后在 1.5 秒内变为黑色。

<!--Take one and a half seconds to transition from the
    PointerOver state to the Normal state. 
    Have the SolidColorBrush, BorderBrush, fade to blue, 
    then to yellow, and then to black in that time.-->
<VisualTransition From="PointerOver" To="Normal" 
                      GeneratedDuration="0:0:1.5">
  <Storyboard>
    <ColorAnimationUsingKeyFrames
      Storyboard.TargetProperty="Color"
      Storyboard.TargetName="BorderBrush"
      FillBehavior="HoldEnd" >

      <ColorAnimationUsingKeyFrames.KeyFrames>

        <LinearColorKeyFrame Value="Blue" 
                             KeyTime="0:0:0.5" />
        <LinearColorKeyFrame Value="Yellow" 
                             KeyTime="0:0:1" />
        <LinearColorKeyFrame Value="Black" 
                             KeyTime="0:0:1.5" />

      </ColorAnimationUsingKeyFrames.KeyFrames>
    </ColorAnimationUsingKeyFrames>
  </Storyboard>
</VisualTransition>
<VisualStateGroup x:Name="CommonStates">

  <!--Define the VisualTransitions that can be used when the control
      transitions between VisualStates that are defined in the
      VisualStatGroup.-->
  <VisualStateGroup.Transitions>

    <!--Take one hundredth of a second to transition to the
        Pressed state.-->
    <VisualTransition To="Pressed" 
                          GeneratedDuration="0:0:0.01" />
    
    <!--Take one half second to transition to the PointerOver state.-->
    <VisualTransition To="PointerOver" 
                          GeneratedDuration="0:0:0.5" />

    <!--Take one hundredth of a second to transition from the
        Pressed state to the PointerOver state.-->
    <VisualTransition From="Pressed" To="PointerOver" 
                          GeneratedDuration="0:0:0.01" />

    <!--Take one and a half seconds to transition from the
        PointerOver state to the Normal state. 
        Have the SolidColorBrush, BorderBrush, fade to blue, 
        then to yellow, and then to black in that time.-->
    <VisualTransition From="PointerOver" To="Normal" 
                          GeneratedDuration="0:0:1.5">
      <Storyboard>
        <ColorAnimationUsingKeyFrames
          Storyboard.TargetProperty="Color"
          Storyboard.TargetName="BorderBrush"
          FillBehavior="HoldEnd" >

          <ColorAnimationUsingKeyFrames.KeyFrames>

            <LinearColorKeyFrame Value="Blue" 
                               KeyTime="0:0:0.5" />
            <LinearColorKeyFrame Value="Yellow" 
                               KeyTime="0:0:1" />
            <LinearColorKeyFrame Value="Black" 
                               KeyTime="0:0:1.5" />

          </ColorAnimationUsingKeyFrames.KeyFrames>
        </ColorAnimationUsingKeyFrames>
      </Storyboard>
    </VisualTransition>
  </VisualStateGroup.Transitions>

  <!--The remainder of the VisualStateGroup is the
      same as the previous example.-->

  <VisualState x:Name="Normal" />

  <VisualState x:Name="PointerOver">
    <Storyboard>
      <ColorAnimation Storyboard.TargetName="BorderBrush" 
                    Storyboard.TargetProperty="Color" To="Red" />

    </Storyboard>
  </VisualState>

  <VisualState x:Name="Pressed">
    <Storyboard >
      <ColorAnimation Storyboard.TargetName="BorderBrush" 
                    Storyboard.TargetProperty="Color" To="Transparent"/>
    </Storyboard>
  </VisualState>

  <!--The Disabled state is omitted for brevity.-->

</VisualStateGroup>

注解

A VisualTransition 是启动 情节提要的行为。 此 情节提要 是一个时间线,用于声明在两个视觉状态之间切换的动画将运行的持续时间。 可以根据控件的视觉状态集定义的起始状态( From state)和结束状态( To state)的每个组合以不同的方式定义转换。 转换由 VisualStateGroup 的 Transitions 属性定义,通常在 XAML 中定义。 大多数默认控件模板不定义转换,在这种情况下,状态之间的转换会即时发生。 将删除旧状态对模板的修改,并应用新状态的修改。

引用 VisualTransition 一个或两个命名视觉状态。 From 值引用当前状态的状态的名称。 To 值引用 GoToState 调用请求的新状态的状态的名称。 这些名称来自应用于 VisualState 的 x:Name 属性字符串值,作为其定义中的同一个 VisualStateGroup 的一部分。 From 或 To 是有效 VisualTransition 的必需值,缺少这些值的 VisualTransition 或使用与现有状态不匹配的值不执行任何操作。

A VisualTransition 可以仅引用 From 状态、仅“ 到 ”状态或同时引用 From 和 To 状态。 省略 From 或 To 等于任何状态。 每当视觉状态发生更改时, VisualStateManager 都使用优先逻辑进行转换:

  1. VisualTransition如果存在专门将旧状态引用为 From,而新状态为“To”,请使用该转换。
  2. 否则,如果存在一个 VisualTransition 专门将新状态引用为 To 但未指定 From,请使用该转换。
  3. 最后,如果存在一个 VisualTransition 专门将旧状态引用为 From 但未指定 To,请使用该转换。

如果上述任何内容均不适用,则不会运行转换。

调用 GoToState 更改控件的视觉状态时, VisualStateManager 将执行以下操作:

  • 如果控件在新请求的视觉状态之前使用的 VisualState 具有 情节提要,该情节提要将停止。
  • 在这些操作之间,如果存在涉及两个VisualTransition视觉状态的转换,并且 GoToState 请求的命名视觉状态有效并且是一个新状态,则运行情节提要。
  • 如果 VisualState 命名为 stateNameStoryboard,则情节提要将开始。

A VisualTransition 可以具有 Storyboard 值、 GeneratedDuration 值或两者。 但是,如果某个 VisualTransition 值既 Storyboard 没有值 GeneratedDuration ,也没有值,即使在 VisualTransition 状态更改中涉及由 From 和 To 值命名的状态,也不会在动画方面执行任何操作。

隐式转换

可以定义一个这样的属性 VisualTransition ,使其具有 GeneratedDuration,但没有任何特定的依赖项属性作为目标并对其进行动画处理。 这会创建隐式转换。 任何专门针对 “从 ”或 “到” 视觉状态的动画的依赖属性,因此在状态更改中具有不同的值,然后使用生成的过渡动画。 此生成的动画使用内插在 “从 状态值”和此类属性的 “到 ”状态值之间切换。 隐式过渡动画在 GeneratedDuration 所表示的时间持续。

隐式转换仅适用于 双精度值、 颜色 或 点 值的属性。 换句话说,必须使用 DoubleAnimation、PointAnimation 或 ColorAnimation 隐式进行动画处理。 如果要对某些其他值创建过渡动画,例如需要 ObjectAnimationUsingKeyFrames 的值,请将该动画放在 Storyboard 中,并为动画提供要运行的 持续时间 。

默认情况下,隐式过渡动画使用线性内插通过 GeneratedDuration 对值进行动画处理。 可以通过设置 GeneratedEasingFunction,GeneratedDurationVisualTransition将线性内插更改为所选的内插行为。

切换动画

还有另一种设计模式和 API,用于显示使用 C++ 或 C# 的应用的视觉转换。 此概念称为 过渡动画 ,实现行为的类是 主题切换 或 主题动画。 切换动画表示完整 UI 元素与整个应用和 UI 之间的关系的变化,而不是在同一控件的视觉状态之间声明转换,而是将更改应用于控件部件的属性。 例如,每当 UI 元素在其布局容器的 UI 坐标空间中移动时,都可以应用 RepositionThemeTransition 。 许多过渡动画由用户操作启动。 过渡动画适用于 UIElement 的各种过渡属性和特定的派生类,不适用于 VisualStateGroup。 过渡动画和主题动画通常内置于控件的默认行为。

构造函数

名称 说明
VisualTransition()

初始化 VisualTransition 类的新实例。

属性

名称 说明
Dispatcher

始终在 Windows 应用 SDK 应用中返回 null 。 请改用 DispatcherQueue 。

(继承自 DependencyObject)
DispatcherQueue

获取 DispatcherQueue 与此对象关联的对象。 表示 DispatcherQueue 即使代码是由非 UI 线程启动,也可以访问 DependencyObject UI 线程上的设施。

(继承自 DependencyObject)
From

获取或设置要从中转换的 VisualState 的名称。

GeneratedDuration

获取或设置从一个状态移动到另一个状态所需的时间,以及任何隐式过渡动画应作为过渡行为的一部分运行的时间。

GeneratedEasingFunction

获取或设置应用于生成的动画的缓动函数。

Storyboard

获取或设置在转换发生时运行的 情节提要 。

To

获取或设置要转换到的 VisualState 的名称。

方法

名称 说明
ClearValue(DependencyProperty)

清除依赖属性的本地值。

(继承自 DependencyObject)
GetAnimationBaseValue(DependencyProperty)

返回为依赖属性建立的任何基值,在动画未处于活动状态的情况下,该属性将适用。

(继承自 DependencyObject)
GetValue(DependencyProperty)

从 DependencyObject 返回依赖属性的当前有效值。

(继承自 DependencyObject)
ReadLocalValue(DependencyProperty)

如果设置了本地值,则返回依赖属性的本地值。

(继承自 DependencyObject)
RegisterPropertyChangedCallback(DependencyProperty, DependencyPropertyChangedCallback)

注册一个通知函数,用于侦听此 DependencyObject 实例上对特定 DependencyProperty 所做的更改。

(继承自 DependencyObject)
SetValue(DependencyProperty, Object)

设置 DependencyObject 上的依赖属性的本地值。

(继承自 DependencyObject)
UnregisterPropertyChangedCallback(DependencyProperty, Int64)

取消以前通过调用 RegisterPropertyChangedCallback 注册的更改通知。

(继承自 DependencyObject)

适用于

另请参阅