我正在为Win 10通用应用程序进行自适应布局。由于某些原因,当我尝试在visualState触发器中更改wrapGrid的maximumRowsOrColumns值时,它无法正常工作。如果用户更改窗口大小,它实际上可以工作,但是在启动程序时,adaptiveTrigger似乎对该值没有影响。这是一些代码:<Hub> <VisualStateManager.VisualStateGroups> <VisualStateGroup> <VisualState x:Name="wideView"> <VisualState.StateTriggers> <AdaptiveTrigger MinWindowWidth="720" /> </VisualState.StateTriggers> <VisualState.Setters> <Setter Target="WrapPanel.MaximumRowsOrColumns" Value="3" /> </VisualState.Setters> </VisualState> <VisualState x:Name="narrowView"> <VisualState.Setters> <Setter Target="WrapPanel.MaximumRowsOrColumns" Value="2" /> </VisualState.Setters> <VisualState.StateTriggers> <AdaptiveTrigger MinWindowWidth="0" /> </VisualState.StateTriggers> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <HubSection> <DataTemplate> <GridView> <GridView.ItemsPanel> <ItemsPanelTemplate> <WrapGrid Orientation="Horizontal" x:Name="WrapPanel"/> </ItemsPanelTemplate> </GridView.ItemsPanel> <GridViewItem> <Rectangle Width="250" Height="100" Fill="Red" Margin="6" /> </GridViewItem> <GridViewItem> <Rectangle Width="250" Height="100" Fill="Yellow" Margin="6" /> </GridViewItem> <GridViewItem> <Rectangle Width="250" Height="100" Fill="Green" Margin="6" /> </GridViewItem> <GridViewItem> <Rectangle Width="250" Height="100" Fill="Blue" Margin="6" /> </GridViewItem> </GridView> </DataTemplate> </HubSection></Hub> 最佳答案 如果您希望触发器在启动时起作用,则可以尝试强制应用以特定的宽度和高度启动。这样可以触发您的视觉状态。以下是您需要放入yourfile.xaml.cs中的代码段:ApplicationView.PreferredLaunchWindowingMode != ApplicationViewWindowingMode.Auto;ApplicationView.PreferredLaunchViewSize = new Size { Width = 600, Height = 500 };ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;同样好的编码做法是从 标记中取出Visual State Manager XAML,并将其放置在代码的末尾(但应位于 标记内)。 <Hub> <HubSection x:Name="HubSection" Header="Test"> <DataTemplate> <GridView> <GridView.ItemsPanel> <ItemsPanelTemplate> <WrapGrid Orientation="Horizontal" x:Name="WrapPanel1" MaximumRowsOrColumns="3"/> </ItemsPanelTemplate> </GridView.ItemsPanel> <GridViewItem > <Rectangle Width="250" Height="100" Fill="Red" Margin="6" /> </GridViewItem> <GridViewItem> <Rectangle Width="250" Height="100" Fill="Yellow" Margin="6" /> </GridViewItem> <GridViewItem> <Rectangle Width="250" Height="100" Fill="Green" Margin="6" /> </GridViewItem> <GridViewItem> <Rectangle Width="250" Height="100" Fill="Blue" Margin="6" /> </GridViewItem> <GridViewItem> <Rectangle Width="250" Height="100" Fill="Orange" Margin="6" /> </GridViewItem> </GridView> </DataTemplate> </HubSection> </Hub> <VisualStateManager.VisualStateGroups> <VisualStateGroup> <VisualState x:Name="wideView"> <VisualState.StateTriggers> <AdaptiveTrigger MinWindowWidth="700" /> </VisualState.StateTriggers> <VisualState.Setters> <Setter Target="WrapPanel1.MaximumRowsOrColumns" Value="3"/> <Setter Target="HubSection.Header" Value="WideView" /> </VisualState.Setters> </VisualState> <VisualState x:Name="narrowView"> <VisualState.StateTriggers> <AdaptiveTrigger MinWindowWidth="0" /> </VisualState.StateTriggers> <VisualState.Setters> <Setter Target="WrapPanel1.MaximumRowsOrColumns" Value="2" /> <Setter Target="HubSection.Header" Value="Vertical" /> </VisualState.Setters> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups></Grid> 10-07 23:10