2

我正在为我的 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>
4

1 回答 1

0

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

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

另外一个好的编码习惯是从 <Hub></Hub> 标记中取出 Visual State Manager XAML,并将它们放在代码的末尾(但在 <Grid> </Grid> 标记内)

    <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>
于 2015-11-04T02:10:19.900 回答