语言

VisualStateGroup 类

定义

包含相互排斥的 VisualState 对象和 VisualTransition 对象,这些对象用于从一个状态到另一个状态。

public ref class VisualStateGroup sealed : DependencyObject
/// [Microsoft.UI.Xaml.Markup.ContentProperty(Name="States")]
/// [Windows.Foundation.Metadata.Activatable(65536, "Microsoft.UI.Xaml.WinUIContract")]
/// [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 VisualStateGroup final : DependencyObject
[Microsoft.UI.Xaml.Markup.ContentProperty(Name="States")]
[Windows.Foundation.Metadata.Activatable(65536, "Microsoft.UI.Xaml.WinUIContract")]
[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 sealed class VisualStateGroup : DependencyObject
Public NotInheritable Class VisualStateGroup
Inherits DependencyObject
<VisualStateManager.VisualStateGroups>
   <VisualStateGroup x:Name="groupname" ...>
     oneOrMoreVisualStates
   </VisualStateGroup>
   <!--- other peer VisualStateGroup's here ... -->
</VisualStateManager.VisualStateGroups>
继承
Object Platform::Object IInspectable DependencyObject VisualStateGroup
属性

示例

此示例为包含一个网格的按钮创建一个简单的 ControlTemplate。 它还包含一个名为 VisualStateGroup “CommonStates”,用于定义“PointerOver”和“Normal”状态。 VisualStateGroup此外,还有一个 VisualTransition,它指定当用户将指针放在指针上方Button时,需要半秒Grid的时间才能从绿色更改为红色。

<ControlTemplate TargetType="Button">
  <Grid >
    <VisualStateManager.VisualStateGroups>
      <VisualStateGroup x:Name="CommonStates">

        <VisualStateGroup.Transitions>

          <!--Take one half second to transition to the PointerOver state.-->
          <VisualTransition To="PointerOver" 
                              GeneratedDuration="0:0:0.5"/>
        </VisualStateGroup.Transitions>
        
        <VisualState x:Name="Normal" />

        <!--Change the SolidColorBrush, ButtonBrush, to red when the
            Pointer is over the button.-->
        <VisualState x:Name="PointerOver">
          <Storyboard>
            <ColorAnimation Storyboard.TargetName="ButtonBrush" 
                            Storyboard.TargetProperty="Color" To="Red" />
          </Storyboard>
        </VisualState>
      </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    <Grid.Background>
      <SolidColorBrush x:Name="ButtonBrush" Color="Green"/>
    </Grid.Background>
  </Grid>
</ControlTemplate>

注解

XAML 中声明的每个 VisualStateGroup 控件模板应始终设置 x:Name 属性 。 控件模板集中 VisualStateGroups 使用的每个名称字符串在该模板中必须是唯一的。 不过,对不同的控件使用相同的组名称很常见。 例如,几乎所有现有控件模板都具有VisualStateGroupx:Name attribute“CommonStates”。

每个 VisualStateGroup 组中的视觉状态集应相互排斥。 换句话说,控件应始终使用其每个已定义 VisualStateGroup 组的视觉状态之一。 每当有一个控件打算同时处于两种状态的情况下,请确保这两个状态位于不同的组中。 例如,下拉列表控件可以同时聚焦并打开其下拉控件。 在正确的视觉状态设计中,对于每个状态,你将有一个单独的 VisualStateGroup 状态,以便它们一次性处于活动状态。 此类组的名称可能类似于“FocusStates”和“DropDownStates”。

每当定义 VisualStateGroup 在其 VisualState 元素之一中启用临时情节提要行为时,请确保该组还包含一个可以调用以取消上一状态的秒 VisualState 。 这可以像声明第二 VisualState 个情节 提要 一样简单,只需 一个 x:Name 属性。

为 a VisualStateGroup 设置的 x:Name 属性值不用于对 VisualStateManager.GoToState 的调用;而是x:Name attribute用于VisualStateManager.GoToState的 VisualState 属性值。 使用 VisualStateManager.GoToState 的任何人员都应了解所有可用的组和状态,以便每个调用正确从旧状态过渡到组中的新状态。

除了一组 VisualState 元素之外,还可以 VisualStateGroup 定义一组 VisualTransition 元素,其中每个 VisualTransition 元素都与组中定义的至少一个命名 VisualState 元素相关。 在 XAML 中,可以将元素集 VisualState 声明为 的 VisualStateGroup即时对象元素子元素。 这是可能的,因为状态属性是视觉状态的集合,是 XAML 内容属性。VisualStateGroup 相比之下,若要设置视觉转换的集合,必须在 XAML 的 VisualStateGroup.Transitions 属性元素中声明该集合。 有关 XAML 内容属性的详细信息,请参阅 XAML 语法指南。

使用 StateTriggers 控制视觉状态时,触发器引擎使用以下优先规则对触发器进行评分并确定哪个触发器以及相应的 VisualState 将处于活动状态:

  1. 派生自 StateTriggerBase 的自定义触发器
  2. AdaptiveTrigger 已激活,因为 MinWindowWidth
  3. 由于 MinWindowHeight,AdaptiveTrigger 已激活

如果一次有多个活动触发器在评分(即两个活动自定义触发器)中发生冲突,则标记文件中声明的第一个触发器优先。

注意:虽然 AdaptiveTrigger 确实派生自 StateTriggerBase,但只能通过设置 MinWindowWidth 和/或 MinWindowHeight 来激活它。

VisualStateGroup 支持自定义 VisualStateManager 实现的 API

许多 API VisualStateGroup 仅支持自定义 VisualStateManager 实现。 其中包括: Name、 CurrentState、 CurrentStateChanging、 CurrentStateChanged。 控件模板的视觉状态的最常见用法不需要这些 API。 特别是处理事件并不典型。 控件的大多数逻辑操作都应涉及其自己的属性和事件。 对于大多数应用和控件定义方案,发生在控件的视觉状态更改应只是控件应用于其模板的逻辑的最终结果,而不是其他逻辑的触发器。

构造函数

名称 说明
VisualStateGroup()

初始化 VisualStateGroup 类的新实例。

属性

名称 说明
CurrentState

从成功调用 GoToState 方法获取最近设置的 VisualState。

Dispatcher

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

(继承自 DependencyObject)
DispatcherQueue

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

(继承自 DependencyObject)
Name

获取 VisualStateGroup 的名称。

States

获取互斥 VisualState 对象的集合。

Transitions

获取 VisualTransition 对象的集合。

方法

名称 说明
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)

活动

名称 说明
CurrentStateChanged

在控件更改为其他状态后发生。

CurrentStateChanging

当控件开始更改为其他状态时发生。

适用于

另请参阅