VisualStateGroup 類別

定義

包含互斥 的 VisualState 物件和 VisualTransition 物件,這些物件用來從某個狀態移至另一個狀態。

public ref class VisualStateGroup sealed : DependencyObject
/// [Windows.Foundation.Metadata.Activatable(65536, Windows.Foundation.UniversalApiContract)]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
/// [Windows.UI.Xaml.Markup.ContentProperty(Name="States")]
class VisualStateGroup final : DependencyObject
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
/// [Windows.UI.Xaml.Markup.ContentProperty(Name="States")]
/// [Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
class VisualStateGroup final : DependencyObject
[Windows.Foundation.Metadata.Activatable(65536, typeof(Windows.Foundation.UniversalApiContract))]
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
[Windows.UI.Xaml.Markup.ContentProperty(Name="States")]
public sealed class VisualStateGroup : DependencyObject
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
[Windows.UI.Xaml.Markup.ContentProperty(Name="States")]
[Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
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
屬性

Windows 需求

裝置系列
Windows 10 (已於 10.0.10240.0 引進)
API contract
Windows.Foundation.UniversalApiContract (已於 v1.0 引進)

範例

此範例會為包含一個GridButton建立簡單的ControlTemplate。 它也包含名為 「CommonStates」 的 VisualStateGroup,其定義 「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 集合中使用的每個名稱字串都必須在該範本中是唯一的。 不過,對於不同的控制項,通常會使用相同的組名。 例如,幾乎所有現有的控制項範本都有具有 「CommonStates」 x:Name 屬性 的 VisualStateGroup。

每個 VisualStateGroup 內的一組視覺狀態應該在群組中互斥。 換句話說,控制項應該隨時都只使用每個已定義之 VisualStateGroup 群組的其中一個視覺狀態。 每當有一個控制項要同時處於兩種狀態的情況時,請確定這兩個狀態位於不同的群組中。 例如,下拉式控制項可以同時聚焦並開啟其下拉式清單。 在正確的視覺狀態設計中,每個狀態都有個別的 VisualStateGroup,讓兩者一次都能作用中。 這類群組的名稱可能如 「FocusStates」 和 「DropDownStates」。

每當定義 VisualStateGroup,以在其其中一個 VisualState 元素中啟用暫時分鏡腳本行為時,請確定該群組也包含第二個 VisualState ,可呼叫以取消先前的狀態。 這就像宣告第二個 VisualState 一樣簡單,完全沒有 Storyboard ,只要 x:Name 屬性即可。

您為 VisualStateGroup 設定的x:Name 屬性值不會用於呼叫VisualStateManager.GoToState;而是用於VisualStateManager.GoToStateVisualStatex:Name 屬性。 任何使用 VisualStateManager.GoToState 的人都應該知道所有可用的群組和狀態,讓每個呼叫都能正確地從舊狀態轉換為群組內的新狀態。

除了一組 VisualState 元素之外,VisualStateGroup 也可以定義一組 VisualTransition 元素,其中每個 VisualTransition 都至少與群組中定義的其中一個具名 VisualState 元素有關。 在 XAML 中, VisualState 元素集合可以宣告為 VisualStateGroup 的即時物件專案子項目。 這是可能的,因為 States 屬性是視覺狀態的集合,是 VisualStateGroup 的 XAML 內容屬性。 相反地,若要設定視覺轉換的集合,您必須在 XAML 的 VisualStateGroup.Transitions 屬性元素內宣告該集合。 如需 XAML 內容屬性的詳細資訊,請參閱 XAML 語法指南

使用 StateTriggers 控制視覺狀態時,觸發程式引擎會使用下列優先順序規則來評分觸發程式,並判斷哪個觸發程式和對應的 VisualState將會作用中:

  1. 衍生自StateTriggerBase 的自訂觸發程式
  2. 自適性Trigger因為MinWindowWidth而啟動
  3. 自適性Trigger因為MinWindowHeight而啟動

如果一次有多個作用中的觸發程式在評分 (發生衝突,也就是兩個作用中的自訂觸發程式) ,則標記檔案中宣告的第一個觸發程式優先。

注意: 雖然 AdaptiveTrigger 確實衍生自 StateTriggerBase,但只能透過設定 MinWindowWidth 和/或 MinWindowHeight來啟用。

VisualStateGroup 支援自訂 VisualStateManager 實作的 API

許多 VisualStateGroup 的 API 僅支援自訂 VisualStateManager 實作 。 這些包括:NameCurrentState、CurrentStateChangingCurrentStateChanged 控制項範本最常見的視覺狀態用法不需要這些 API。 特別是處理事件並不一般。 控制項的大部分邏輯作業都應該包含自己的屬性和事件。 對於大部分的應用程式和控制項定義案例,對控制項發生的視覺狀態變更應該只是控制項套用至其範本之邏輯的結束結果,而不是其他邏輯的觸發程式。

建構函式

VisualStateGroup()

初始化 VisualStateGroup 類別的新實例。

屬性

CurrentState

成功呼叫 GoToState方法取得最近設定的 VisualState

Dispatcher

取得與此物件相關聯的 CoreDispatcherCoreDispatcher代表可以存取 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

發生于控制項開始變更為不同的狀態時。

適用於

另請參閱