对于大学项目,我正在研究一个显示当前天气信息的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>
界面概念:
答案 0 :(得分:0)
UI元素不会因为您在其上设置MainGrid
附加属性而变得可滚动。这些附加属性仅对在其模板中包含ScrollViewer的元素有用( ScrollViewer将接受附加属性的值)。
如果您希望VisualStateManager.VisualStateGroups
可滚动,则需要将其包装在ScrollViewer中。您还需要将MainGrid
附加属性从VisualStateManager.VisualStateGroups
提升到ScrollViewer中。 <{1}}必须附加到模板根目录,否则它将无效。
<Page>
<ScrollViewer>
<VisualStateManager.VisualStateGroups>
...
</VisualStateManager.VisualStateGroups>
<Grid x:Name="MainGrid">
...
</Grid>
</ScrollViewer>
</Page>