通过visualState触发器更改WrapGrid的maximumRowsOrColums值不起作用

时间:2015-07-15 11:46:59

标签: c# xaml windows-10 universal visualstates

我正在为我的win 10通用应用程序进行自适应布局。出于某种原因,当我尝试在visualState触发器中更改wrapGrid的maximumRowsOrColumns值时,它无法正常工作。

如果用户更改了窗口大小,它确实有效,但是在启动程序时,似乎adaptiveTtrger对该值没有影响。

以下是一些代码:

<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>

1 个答案:

答案 0 :(得分:0)

如果您希望触发器在启动时工作,您可以尝试强制应用程序以特定的宽度和高度启动。这样可以触发您的视觉状态。下面是你需要放在yourfile.xaml.cs中的代码片段:

ApplicationView.PreferredLaunchWindowingMode != ApplicationViewWindowingMode.Auto;
ApplicationView.PreferredLaunchViewSize = new Size { Width = 600, Height = 500 };
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;

另一个好的编码实践是从你的&lt;中取出你的Visual State Manager XAML。 Hub&gt;&lt; /集线器&GT;标记并将它们放在代码的末尾(但在&lt; Grid&gt;&lt; / Grid&gt;标记内)

    <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>