Windows Universal响应式设计重新定位

时间:2015-07-12 09:33:22

标签: xaml windows-10 win-universal-app windows-10-mobile

所以,我刚刚开始使用Windows应用程序,有一些我无法按照自己的意愿工作(可能是因为我找不到任何样本,而且Channel9视频没有覆盖我的情况)。

this article开始,我决定“重新定位”技术是适合我的应用程序从大屏幕移动到较小屏幕的技术。

我所做的是使用StackPanel并使用两个AdaptiveTrigger更改其方向(一个用于0宽度,另一个用于720,基于表here)。

这种方法有效,但我会用一些丑陋的油漆编辑截图来说明一些问题。

当我处于BigScreen情况时会发生这种情况,那里有足够的空间让A和B同时在同一行上。这里的问题是B应该占据剩余的全部宽度,覆盖所有蓝色部分。

BigScreen

第二个问题与调整大小有关。当没有足够的空间时,绿色部分会被切割而不是调整大小(您可以看到右侧边框消失)。在使用StackPanel使布局响应之前,没有发生这种情况。

HalfScreen

最后,当我们处于SmallScreen情况时,方向变为垂直方向,我们遇到的问题与第一个相同:绿色部分的高度不会填满屏幕。

SmallScreen

以下是页面使用的XAML

<Page
    x:Class="Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WifiAnalyzerFinal.Views"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:mvvm="using:Mvvm"
    mc:Ignorable="d">        

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="SmallScreen">
            <VisualState>
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="0"/>
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="StackPanel.Orientation" 
                            Value="Vertical"/>
                </VisualState.Setters>
            </VisualState>
            </VisualStateGroup>
            <VisualStateGroup x:Name="BigScreen">
            <VisualState>
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="720"/>
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="StackPanel.Orientation" 
                            Value="Horizontal"/>
                    <Setter Target="Rect.Width" 
                            Value="200"/>
                        <Setter Target="Rect.Height" 
                            Value="Auto"/>
                    </VisualState.Setters>
            </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <StackPanel Orientation="Vertical"
                    Background="Blue"
                    x:Name="StackPanel">
            <Rectangle Fill="Red" 
                       Height="50"
                       x:Name="Rect"
                       Width="Auto"/>
            <ListView ItemsSource="{Binding Stuff}"
                      HorizontalAlignment="Stretch"
                      HorizontalContentAlignment="Stretch"
                      VerticalAlignment="Stretch"
                      Background="Green"
                      Width="Auto"
                      BorderBrush="DarkGreen"
                      BorderThickness="5"
                      Padding="5">
                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                        <Setter Property="Margin" Value="0,0,0,5"/>
                    </Style>
                </ListView.ItemContainerStyle>
            </ListView>
        </StackPanel>
    </Grid>
</Page>

请注意,如果没有StackPanel绿色部分适合页面,则覆盖所有可用区域。不幸的是,我无法找到更好的解决方案,因为没有样本告诉我们应该如何实现这种技术。我也尝试使用新的RelativePanel,但似乎AdaptiveTrigger的{​​{1}}不适用于Setter等附加属性。

是否有人成功应用此技术而不必使用代码隐藏?

编辑:

我使用RelativePanel.RightOf包含2行2列,Grid将所有内容从行移到列,反之亦然。

2 个答案:

答案 0 :(得分:4)

可以通过setter更改RelativePanel附加属性值。语法如下:

<Setter Target="SomeXAMLObject.(RelativePanel.RightOf)" Value="SomeOtherXAMLObject" />

答案 1 :(得分:0)

为什么不尝试使用网格(而不是StackPanel)并使用比例尺寸定义行,如:

`<Grid>
 <Grid.RowDefinitions>
  <RowDefinition width="2*"/>
  <RowDefinition width="3*"/>
  <RowDefinition width="1*"/>
 </Grid.RowDefinitions>
</Grid>`