【问题标题】:VisualStateManager in XAML won't update with new StateXAML 中的 VisualStateManager 不会使用新状态更新
【发布时间】:2026-01-07 10:05:02
【问题描述】:

我正在尝试向 Windows 8.1 中的 Xaml/VB 应用程序添加一些屏幕大小断点。我正在为我的主视图处理代码隐藏中的 SizeChanged 事件,并调用 VisualStateManager.GoToState 这应该传播到位于我的 .xaml 文件中的预定义视觉状态。这是一个小例子的全部代码,它应该改变小屏幕上的背景颜色,但没有。

MainView.xaml

<Page x:Name="PageRoot"
    x:Class="WinStoreMvvmTemplate.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Grid Background="Red" x:Name="ContentGrid"></Grid>

    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup>
            <VisualState x:Name="SmallLayout">
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames 
                        Storyboard.TargetName="ContentGrid"
                        Storyboard.TargetProperty="Background">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="Blue"/>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
</Page>

MainView.xaml.vb

Public NotInheritable Class MainView : Inherits Page

    Private Sub WindowSizeChanged(sender As Object, e As SizeChangedEventArgs) _
            Handles Me.SizeChanged
        If e.NewSize.Width < 340 Then
            VisualStateManager.GoToState(Me, "SmallLayout", True)
        End If
    End Sub

End Class

事件肯定在代码端触发,GoToState 方法肯定被调用。

知道为什么 xaml 没有接受这些更改吗?

【问题讨论】:

    标签: .net vb.net xaml winrt-xaml windows-8.1


    【解决方案1】:

    这很愚蠢,但 looks likeVisualStateManager.VisualStateGroups 需要位于 Page 的子元素内,在本例中为 Grid。我会接受任何其他可以解释原因的答案。

    MainView.xaml.vb

    <Page x:Name="PageRoot"
        x:Class="WinStoreMvvmTemplate.View.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
        <Grid Background="Red" x:Name="ContentGrid">
    
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup>
                    <VisualState x:Name="RegularLayout"/>
                    <VisualState x:Name="SmallLayout">
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames 
                                Storyboard.TargetName="ContentGrid"
                                Storyboard.TargetProperty="Background">
                                <DiscreteObjectKeyFrame KeyTime="0" Value="Blue"/>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
    
        </Grid>
    </Page>
    

    【讨论】:

    • +1 也从未找到“为什么”的答案,但想知道......除了它被隐式应用于Element 而不是视图。更详细的解释会很棒。
    • 我可能会提出另一个问题,但是当我运行应用程序时,我可以让一切正常工作,但无法让设计人员像在 8.0 模板中那样更改视图状态。我想知道为什么。 LayoutAwarePage 的设计器支持非常棒。
    最近更新 更多