Windows 10 UWP中的可视状态管理器未在页面加载

时间:2015-10-12 01:10:19

标签: c# xaml win-universal-app uwp

我有一个页面,其中包含一个相对面板,可根据宽度重新组织。但是,除非宽度> 1,否则它似乎不会在加载时应用任何状态。 720像素。如果我在加载页面后调整页面大小,则两个状态都可以正常工作。

解决方法是检查加载页面上的窗口大小并手动选择状态,但我相信这应该自动处理?我有其他页面可行,我不确定我做的不同。这是我的代码的简化版本,我有它设置红色/蓝色背景,所以我可以判断是否/正在应用哪个状态

<Page.Resources>
    <converters:HighlightConverter x:Key="HighlightConverter"/>
</Page.Resources>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <gui:MainAppBar x:Name="mainAppBar" Grid.Row="0"/>

    <ScrollViewer Grid.Row="1">
        <RelativePanel>

            <StackPanel x:Name="ZonesContainer" Margin="12,12,0,0">
                <TextBlock Text="Zones"/>
                <ItemsControl x:Name="ZonesPanel">
                    <ItemsControl.ItemContainerStyle>
                        <Style TargetType="ContentPresenter">
                            <Setter Property="Margin" Value="6"/>
                        </Style>
                    </ItemsControl.ItemContainerStyle>
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <ItemsWrapGrid x:Name="ZonesWrapGrid" Orientation="Vertical"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <StackPanel x:Name="Panel" Orientation="Horizontal">
                            </StackPanel>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>

            <StackPanel x:Name="SourcesContainer" RelativePanel.RightOf="ZonesContainer" Margin="12,12,0,0">
                <GridView x:Name="SourcesPanel" Header="Sources">
                </GridView>
            </StackPanel>

            <StackPanel x:Name="NetworkServicesContainer" RelativePanel.Below="SourcesContainer" RelativePanel.AlignLeftWith="SourcesContainer" Margin="12,12,0,0">
                <GridView x:Name="NetworkServicesPanel" Header="Network">
                </GridView>
            </StackPanel>

        </RelativePanel>
    </ScrollViewer>

    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="WindowStates">
            <VisualState x:Name="WideState">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="720" />
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="ZonesContainer.Background" Value="Blue"/>
                </VisualState.Setters>
            </VisualState>
            <VisualState x:Name="NarrowState">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="0" />
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="ZonesContainer.Background" Value="Red"/>
                    <Setter Target="SourcesContainer.(RelativePanel.Below)" Value="ZonesContainer" />
                    <Setter Target="SourcesContainer.(RelativePanel.AlignLeftWith)" Value="ZonesContainer" />
                    <Setter Target="NetworkServicesContainer.(RelativePanel.Below)" Value="SourcesContainer" />
                    <Setter Target="ZonesWrapGrid.Orientation" Value="Horizontal" />
                </VisualState.Setters>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>

</Grid>

  • 更新

我已更新代码以显示缺少的ZonesWrapGrid,它似乎确实相关。当我调整页面大小时它会切换ZonesWrapGrid方向时,视觉状态可以正常工作,只是没有设置加载状态。

但是,如果我从可视状态管理器中删除ZonesWrapGrid更改,则窄/宽状态会在加载时正确应用,但当然我会丢失我想要的方向更改。

1 个答案:

答案 0 :(得分:2)

您可以尝试更换整个ItemsPanelTemplate吗?在资源中创建两者:

<ItemsPanelTemplate x:Key="VerticalWrapGrid">
    <ItemsWrapGrid Orientation="Vertical"/>
</ItemsPanelTemplate>
<ItemsPanelTemplate x:Key="HorizontalWrapGrid">
    <ItemsWrapGrid Orientation="Horizontal"/>
</ItemsPanelTemplate>

然后在需要时交换:

<VisualState x:Name="WideState">
    <VisualState.StateTriggers>
        <AdaptiveTrigger MinWindowWidth="720" />
    </VisualState.StateTriggers>
    <VisualState.Setters>
        <Setter Target="ZonesPanel.ItemsPanel" Value="{StaticResource HorizontalWrapGrid}" />
    </VisualState.Setters>
</VisualState>
<VisualState x:Name="NarrowState">
    <VisualState.StateTriggers>
        <AdaptiveTrigger MinWindowWidth="0" />
    </VisualState.StateTriggers>
    <VisualState.Setters>
        <Setter Target="ZonesPanel.ItemsPanel" Value="{StaticResource VerticalWrapGrid}" />
    </VisualState.Setters>
</VisualState>