Windows 10 UWP中的可视化状态管理器在页面加载时未应用初始状态
本文关键字:加载 初始状态 应用 管理器 UWP 状态 可视化 Windows | 更新日期: 2023-09-27 18:17:07
我有一个相对面板的页面,根据宽度重新组织。然而,除非宽度> 720px,否则它似乎不会在加载时应用任何状态。如果我在加载后调整页面大小,这两种状态都可以工作。
一个解决方法是检查页面加载的窗口大小并手动选择状态,但我相信这应该自动处理?我有其他的页面,我不确定我做了什么不同。这是我的代码的简化版本,我设置了红色/蓝色背景,这样我就可以知道是否/哪个状态被应用
<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更改,则窄/宽状态确实正确地应用于加载,但当然我失去了我想要的方向更改。
你可以尝试只是替换整个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>