我正在为我的 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>
我是一名优秀的程序员,十分优秀!