微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 如何在嵌套元素上设置可视状态?

我正在尝试使用visualstatemanager属性设置一个值.问题是我需要在嵌套元素(称为variableSizedWrapGrid)上设置此值,但它不响应相应的状态.这个想法是当用户更改平板电脑的方向(LANDSCAPE到PORTRAIT)时,应该更改此元素的方向.

<GroupStyle.Panel>
    <ItemsPanelTemplate>
        <VariableSizedWrapGrid x:Name="variableSizedWrapGrid" Orientation="Vertical" Background="Blue" Width="660" ItemHeight="120" ItemWidth="220" Margin="0,80,0">

            <visualstatemanager.VisualStateGroups>
                <VisualStateGroup>
                    <VisualState x:Name="FullScreenPortrait">
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Orientation)">
                                <discreteObjectKeyFrame KeyTime="0" Value="Horizontal" />
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup>
            </visualstatemanager.VisualStateGroups>

        </VariableSizedWrapGrid>

    </ItemsPanelTemplate>
</GroupStyle.Panel>

我正在为Windows 8开发Windows Metro应用程序.

解决方法

我相信这是LayoutAwarePage的用途.如果您将LayoutAwarePage作为基类,并且希望控件在旋转平板电脑时切换VisualStates,则将控件的Loaded事件挂钩到LayoutAwarePage上的StartLayoutUpdates方法.

如您所见,每当状态发生变化时,它将运行“InvalidateVisualState”,它将遍历接收LayoutUpdates的所有控件,并手动将其visualstatemanager设置为适当的状态.

所以在你所拥有的代码中,你需要为景观设置一个VisualState – 我相信它将被称为FullScreenLandscape但我不记得了 – 然后将StartLayoutUpdates添加到VariableSizedWrapGrid控件的Loaded事件中.这假设您的页面是LayoutAwarePage.老实说,如果你想这样做,你应该这样做.由于这个原因,他们已经包含了所有样板代码.

希望有所帮助.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐