Windows UAP:获取页面当前的可视状态

时间:2016-02-11 10:00:14

标签: winrt-xaml win-universal-app windows-10 windows-10-universal

我知道我可以使用VisualStateManager.GoToState以编程方式设置可视状态。

但是,如果从XAML触发视觉状态,如何获取当前视觉状态?

2 个答案:

答案 0 :(得分:6)

首先,您必须了解一个控件有几种当前的视觉状态。但是对于一个视觉状态组(以及一个对照的几个视觉状态组),存在一个当前视觉状态。

对于特定的Visual State Group,您可以使用事件CurrentStateChanged(或CurrentStateChanging)来捕获视觉状态的变化:

<VisualStateManager.VisualStateGroups>
    <VisualStateGroup x:Name="Custom"
                      CurrentStateChanged="CustomGroup_CurrentStateChanged">

并在C#中:

private void CustomGroup_CurrentStateChanged(object sender, VisualStateChangedEventArgs e)
{
    Debug.WriteLine(e.NewState.Name);
}

如果无法修改VisualStateGroup的XAML代码,则可以使用VisualStateManagner.GetVisualStateGroups方法查询控件的VisualStateGroups集合:

foreach(var group in VisualStateManager.GetVisualStateGroups(aControl))
{
    var currentStateName = group.CurrentState.Name;
}

如果您需要检查特定视觉状态组的状态更改,您需要他的名字(例如“CommonStates”),并且可以执行以下操作:

var aControl = this;
var visualStateGroupName = "CommonStates";
var myVsg = VisualStateManager.GetVisualStateGroups(aControl).FirstOrDefault(vsg => vsg.Name == visualStateGroupName);
var currentState = myVsg.CurrentState;
myVsg.CurrentStateChanged += (s, e) =>
{
    currentState = e.NewState;
};

答案 1 :(得分:1)

Handling the state manually, not a crazy idea. Here's how:

numpy.timedelta64

Make sense?

One more important thing to remember. It is possible for more than one visual state to be active. When you ask in your question what the page visual state is, that is a loaded question. A single visual state group can have only one active visual state. But a page can have more than one visual state group. This means a page can have more then one active visual states. I know that in many cases there is a single visual state group. But I wanted to let you know that the question itself is tricky because of multi-visual state groups.

Best of luck!