【问题标题】:VisualStateManager GoToState does not workVisualStateManager GoToState 不起作用
【发布时间】:2014-12-04 20:41:25
【问题描述】:

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

        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 唯一的区别是我的 Grid 不是 Page 的顶部元素。 问题可能是第一个参数

VisualStateManager.GoToState(this, CurrentViewState, true);

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

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

计数为 0。

【问题讨论】:

  • 将你的 VSM 移到你试图操纵它的元素之上,也就是... 将它移到你的矩形之上,甚至可以移到你的列定义之上,就像它在 tut 中显示的那样。哦,将“CurrentViewState”更改为您正在切换的视觉状态名称......
  • 不幸的是没有工作。
  • 您将 currentviewstate 更改为喜欢 Portrait 并且没有?我想我必须在一分钟内真正阅读你的全部内容。
  • 是的,CurrentViewState 是“横向”或“纵向”。
  • 是的,如果我花时间真正阅读它并看到“我的网格不是顶级元素”会有所帮助,哈哈,很高兴你得到了你的补救措施。

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


【解决方案1】:

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

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

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

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

【讨论】:

  • 非常感谢,我本可以在这上面浪费时间!
  • 现在我只需要弄清楚如果VisualStateManager 是UserControl 的一部分,这是否也适用... UserControl 是否需要是直接子项? VSM 是否需要是 UserControl 中的直接子级? ...当我找到东西时会更新
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-12
  • 2019-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-08
相关资源
最近更新 更多