c# - VisualStateManager GoToState 不起作用

标签 c# xaml windows-store-apps winrt-xaml visualstatemanager

当屏幕方向改变时,我称之为:

        string CurrentViewState = ApplicationView.GetForCurrentView().Orientation.ToString();

        // Trigger the Visual State Manager
        bool success = VisualStateManager.GoToState(this, CurrentViewState, true);

在我的 XAML 中有一个简单的 GRID(位于页面层次结构的深处)

            <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Grid.Row="2">

                <Grid.ColumnDefinitions>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                </Grid.ColumnDefinitions>

                <Rectangle x:Name="rect_1" Grid.Column="0" Fill="Blue"></Rectangle>
                <Rectangle x:Name="rect_2" Grid.Column="1" Fill="Red"></Rectangle>
                <Rectangle x:Name="rect_3" Grid.Column="2" Fill="Green"></Rectangle>

                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup>
                        <VisualState x:Name="Landscape">
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames
      Storyboard.TargetName="rect_1"
      Storyboard.TargetProperty="Fill">
                                    <DiscreteObjectKeyFrame KeyTime="0" Value="Brown"/>
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>

                        </VisualState>

                        <VisualState x:Name="Portrait">
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames
      Storyboard.TargetName="rect_1"
      Storyboard.TargetProperty="Fill">
                                    <DiscreteObjectKeyFrame KeyTime="0" Value="Orange"/>
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>

            </Grid>

所以它是 3 个简单的矩形,但它们的颜色永远不会改变。所有这些都直接取自这里的教程:http://msdn.microsoft.com/en-us/library/windows/apps/dn495655.aspx唯一的区别是我的网格不是页面的顶部元素。 问题可能是第一个参数

VisualStateManager.GoToState(this, CurrentViewState, true);

是“这个”。但我不知道它应该是什么,不允许将其设置为网格或其中一个矩形。

        IList<VisualStateGroup> visualStateGroups = VisualStateManager.GetVisualStateGroups(this);
        int count= visualStateGroups.Count;      

计数为 0。

最佳答案

我发现:VisualStateManager 必须是 Page 的直接子项的直接子项。

<Page>
    <Grid>
        <VisualStateManager...>
        </VisualStateManager...>

        <!-- deep in the hierarchy somewhere -->
                     <Grid>
                          <Rectangle/>
                          <Rectangle/>
                          <Rectangle/>
                     </Grid>
    </Grid>
</Page>

我尝试将其添加为 Page 的直接子级,但没有成功。我完全不明白这背后的逻辑是什么。

关于c# - VisualStateManager GoToState 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27303644/

相关文章:

c# - 如何使用C#获取word文档中的段落编号?

c# - 按下后退按钮时跳过页面,WP7

c# - 解决 DataGrid 列宽调整不一致的问题

javascript - Windows 商店应用程序 - javascript - 元素变量

c# - 同一实体类型的一对一和一对多

c# - Zip 文件导致的 SharpZipLib 问题\而不是/

c# - 如何将 List<SomeType> 转换为 List<AnotherType>?

c# - 在网格中使用 2 个不同的 ViewModel

xaml - 相当于 WinRT 中的可编辑组合框?

javascript - 在 ListVIew 中动态绑定(bind)项目