制作一个可滚动响应的网格视图

本文关键字:响应 滚动 网格 视图 一个 | 更新日期: 2023-09-27 18:12:52

对于一个大学项目,我正在开发一个UWP应用程序,该应用程序显示当前天气信息,并具有类似Cortana的界面来控制智能家居的某些部分。

界面由三列组成,其中一行用于桌面视图,这三列在移动设备上对齐为一列中的三行。这部分是我通过遵循教程并修改代码来满足我的特定需求来解决的。

我现在有一个问题,列,当作为一行被缩小到适合1/3的屏幕。我将硬编码的大小设置为500,以查看是否可以使用固定值,因为内部元素的大小不会改变太多。然而,当我这样做的时候,视图是不可滚动的,第二行和整个第三行的大部分是不可见的用户。我已经尝试通过将下面的代码添加到主网格来解决这个问题,但没有结果。

<Grid x:Name="MainGrid" ScrollViewer.VerticalScrollMode="Enabled" ScrollViewer.VerticalScrollBarVisibility="Visible">

TL;DR:我如何使我的响应网格布局,使它是可滚动的,当网格变得太大

XAML代码:

<Page
x:Class="UWPWeather.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UWPWeather"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Loaded="Page_Loaded">
<Grid x:Name="MainGrid" ScrollViewer.VerticalScrollMode="Enabled" ScrollViewer.VerticalScrollBarVisibility="Visible">
    <Grid.Background>
        <ImageBrush Stretch="Fill" ImageSource="Assets/blue-sea-horizon.jpg"/>
    </Grid.Background>
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup>
            <VisualState x:Name="WideState">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="1100" />
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="MainGrid.RowDefinitions[1].Height" Value="0*"/>
                    <Setter Target="MainGrid.RowDefinitions[2].Height" Value="0*"/>
                    <Setter Target="MainGrid.ColumnDefinitions[1].Width" Value="1*"/>
                    <Setter Target="MainGrid.ColumnDefinitions[2].Width" Value="1*"/>
                    <Setter Target="FirstGrid.Margin" Value="0 0 0 0"/>
                    <Setter Target="SecondGrid.Margin" Value="0 0 0 0"/>
                    <Setter Target="SecondGrid.(Grid.Column)" Value="1"/>
                    <Setter Target="SecondGrid.(Grid.Row)" Value="0"/>
                </VisualState.Setters>
            </VisualState>
            <VisualState x:Name="NarrowState">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="0" />
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="FirstGrid.Margin" Value="0 0 0 6"/>
                    <Setter Target="SecondGrid.Margin" Value="0 6 0 0"/>
                    <Setter Target="MainGrid.RowDefinitions[0].Height" Value="500"/>
                    <Setter Target="MainGrid.RowDefinitions[1].Height" Value="500"/>
                    <Setter Target="MainGrid.RowDefinitions[2].Height" Value="500"/>
                    <Setter Target="MainGrid.ColumnDefinitions[1].Width" Value="0*"/>
                    <Setter Target="MainGrid.ColumnDefinitions[2].Width" Value="0*"/>
                    <Setter Target="SecondGrid.(Grid.Column)" Value="0"/>
                    <Setter Target="SecondGrid.(Grid.Row)" Value="1"/>
                    <Setter Target="ThirdGrid.(Grid.Column)" Value="0"/>
                    <Setter Target="ThirdGrid.(Grid.Row)" Value="1"/>
                </VisualState.Setters>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>

    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>

        <Grid x:Name="FirstGrid" Grid.Column="0" Grid.Row="0" Background="Transparent">
            <Button x:Name="StartRecognition" HorizontalAlignment="Center" VerticalAlignment="Center" Height="200" Width="200" Click="StartVoiceRecog">
                <Button.Background>
                    <ImageBrush Stretch="Uniform" ImageSource="Assets/microphone.png"/>
                </Button.Background>
            </Button>
        </Grid>
        <Grid x:Name="SecondGrid" Grid.Column="1" Grid.Row="0" Background="Transparent">
            <StackPanel Orientation="Vertical" HorizontalAlignment="Center" Padding="0,50,0,0">
                <TextBlock x:Name="LocationTextBlock" FontSize="36" Foreground="White" HorizontalAlignment="Center" TextWrapping="Wrap" />
                <StackPanel Orientation="Horizontal" Padding="5">
                    <Image x:Name="ResultImage" Width="200" Height="200" HorizontalAlignment="Left"/>
                    <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" x:Name="TempTextBlock" FontSize="60" Foreground="White"/>
                </StackPanel>
                <TextBlock x:Name="DescriptionTextBlock" Padding="5" FontSize="36" Foreground="White" HorizontalAlignment="Center" />
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Padding="5">
                    <Image x:Name="WindDirection" Width="20" Height="20" />
                    <TextBlock x:Name="WindSpeed" FontSize="12" Foreground="White"/>
                </StackPanel>
                <TextBlock x:Name="Pressure" Padding="5" FontSize="12" Foreground="White" HorizontalAlignment="Center" />
                <TextBlock x:Name="Humidity" Padding="5" FontSize="12" Foreground="White" HorizontalAlignment="Center" />
                <StackPanel Orientation="Horizontal" Padding="5" HorizontalAlignment="Center">
                    <Image x:Name="SunRiseImage" Width="20" Height="20"/>
                    <TextBlock x:Name="Sunrise" FontSize="12" Foreground="White"  />
                </StackPanel>
                <StackPanel Orientation="Horizontal" Padding="5" HorizontalAlignment="Center" >
                    <Image x:Name="SunsetImage" Width="20" Height="20"/>
                    <TextBlock x:Name="Sunset" FontSize="12" Foreground="White" />
                </StackPanel>
                <Button x:Name="UpdateWeather" Padding="5" HorizontalAlignment="Center" VerticalAlignment="Center" Height="49" Width="39" Click="UpdateWeatherOnClick">
                    <Button.Background>
                        <ImageBrush Stretch="Fill" ImageSource="Assets/012_restart-512.png"/>
                    </Button.Background>
                </Button>
            </StackPanel>
        </Grid>
    <Grid x:Name="ThirdGrid" Grid.Column="2" Grid.Row="0" Background="Transparent">
    </Grid>
</Grid>

界面概念:

界面宽

界面狭窄

制作一个可滚动响应的网格视图

UI元素不会因为设置了ScrollViewer.VerticalScrollMode="Enabled"附加属性而变成可滚动的。这些附加属性只对模板中包含ScrollViewer的元素有用( ScrollViewer将接受附加属性的值)。

如果您希望您的MainGrid是可滚动的,您需要将其包装在ScrollViewer中。您还需要将VisualStateManager.VisualStateGroups附加属性从MainGrid提升到ScrollViewer中。VisualStateManager.VisualStateGroups必须附加到模板根目录,否则将不起作用。

<Page>
    <ScrollViewer>
        <VisualStateManager.VisualStateGroups>
            ...
        </VisualStateManager.VisualStateGroups>
        <Grid x:Name="MainGrid">
            ...
        </Grid>
    </ScrollViewer>
</Page>