【问题标题】:Building adaptive layout with RelativePanel使用 RelativePanel 构建自适应布局
【发布时间】:2015-10-20 18:36:16
【问题描述】:

您好,我希望当视力小于 720(例如电话)时,我希望网格 2 低于网格 1。我尝试通过它的面板和自适应触发器如下:

  <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <RelativePanel >
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="VisualStateGroup">
                <VisualState x:Name="NarrowView">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="0" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="Grid2.(RelativePanel.Below)" Value="Grid1"/>
                        <Setter Target="Grid1.(RelativePanel.Above)" Value="Grid2"/>
                    </VisualState.Setters>
                </VisualState>
                <VisualState x:Name="WideView">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="720" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="Grid2.(RelativePanel.RightOf)" Value="Grid1"/>
                        <Setter Target="Grid1.(RelativePanel.LeftOf)" Value="Grid2"/>
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>

        <Grid >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>

            <Grid Grid.Column="0" x:Name="Grid1" >
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
                <TextBox Grid.Row="0" FontSize="20" PlaceholderText="NOME" Style="{StaticResource ResourceKey=TextBoxStyle}"/>
            </Grid>
            <Grid Grid.Column="1" x:Name="Grid2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <TextBlock Grid.Row="0" FontSize="17" Text="Note" Foreground="#222222" Margin="20,15" ></TextBlock>
                <TextBox Grid.Row="2" MaxLength="0" FontSize="17" Height="500" PlaceholderText="AGGIUNGI NOTA" Style="{StaticResource ResourceKey=TextBoxStyle}"></TextBox>
            </Grid>
        </Grid>
    </RelativePanel>
</Grid>

但它不起作用。 谢谢

【问题讨论】:

    标签: xaml uwp


    【解决方案1】:

    您需要解决两件事:

    1) 要使 VisualState 正常工作,请将 VisualStateManager 放在根 Grid 的第一个子项下:

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <VisualStateManager.VisualStateGroups>
    ......
            </VisualStateManager.VisualStateGroups>
    ......
    
    </Grid>
    

    2)您不需要添加Columns,只需将您的两个Grids放在RelativePanel下即可:

    <RelativePanel>
                <Grid x:Name="Grid1">
                    ......
                </Grid>
                <Grid x:Name="Grid2">
                    ......
                </Grid>
            </RelativePanel>
    

    完成的xaml代码:

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="VisualStateGroup">
                    <VisualState x:Name="NarrowView">
                        <VisualState.StateTriggers>
                            <AdaptiveTrigger MinWindowWidth="0" />
                        </VisualState.StateTriggers>
                        <VisualState.Setters>
                            <Setter Target="Grid2.(RelativePanel.Below)" Value="Grid1"/>
                        </VisualState.Setters>
                    </VisualState>
                    <VisualState x:Name="WideView">
                        <VisualState.StateTriggers>
                            <AdaptiveTrigger MinWindowWidth="720" />
                        </VisualState.StateTriggers>
                        <VisualState.Setters>
                            <Setter Target="Grid2.(RelativePanel.RightOf)" Value="Grid1"/>
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <RelativePanel>
                <Grid x:Name="Grid1">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <TextBox Grid.Row="0" FontSize="20" PlaceholderText="NOME" />
                </Grid>
                <Grid x:Name="Grid2">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <TextBlock Grid.Row="0" FontSize="17" Text="Note" Foreground="#222222" Margin="20,15" ></TextBlock>
                    <TextBox Grid.Row="2" MaxLength="0" FontSize="17" Height="500" PlaceholderText="AGGIUNGI NOTA" ></TextBox>
                </Grid>
            </RelativePanel>
        </Grid>
    

    【讨论】:

    • 完美。现在可以扩展相关面板内的元素,就像在网格的列上发生的那样?
    【解决方案2】:

    我找到了一个没有 relativePanel 的解决方案,如果有人关心的话:

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="VisualStateGroup">
                    <VisualState x:Name="NarrowView">
                        <VisualState.StateTriggers>
                            <AdaptiveTrigger MinWindowWidth="0" />
                        </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="text.(Grid.ColumnSpan)" Value="2" />
                        <Setter Target="text1.(Grid.ColumnSpan)" Value="2" />
                        <Setter Target="text2.(Grid.ColumnSpan)" Value="2" />
                        <Setter Target="text1.(Grid.Row)" Value="1" />
                        <Setter Target="text1.(Grid.Column)" Value="0" />
                        <Setter Target="text2.(Grid.Row)" Value="2" />
                        <Setter Target="text2.(Grid.Column)" Value="0" />
                    </VisualState.Setters>
                    </VisualState>
                    <VisualState x:Name="WideView">
                        <VisualState.StateTriggers>
                            <AdaptiveTrigger MinWindowWidth="860" />
                        </VisualState.StateTriggers>
                        <VisualState.Setters>
                            <Setter Target="text.(Grid.ColumnSpan)" Value="1" />
                            <Setter Target="text1.(Grid.Row)" Value="0" />
                            <Setter Target="text1.(Grid.Column)" Value="1" />
                            <Setter Target="text2.(Grid.Row)" Value="1" />
                            <Setter Target="text2.(Grid.Column)" Value="1" />
                    </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
        <TextBox x:Name="text" PlaceholderText="NOME" />
        <TextBlock x:Name="text1" Grid.Row="0" Grid.Column="1" Text="Note"></TextBlock>
        <TextBox x:Name="text2" Grid.Row="1" Grid.Column="1" PlaceholderText="AGGIUNGI NOTA" ></TextBox>
    </Grid>
    

    【讨论】:

    • 改变 Grid 的 Row 和 Column 属性是实现这种效果的传统方式:)
    • 唯一的问题是每一列都有相同的行布局。通过在每列中放置一个网格,我有一个独立的布局,但我不能再通过 statetrigger 管理它们?
    猜你喜欢
    • 2016-08-10
    • 2022-09-27
    • 2018-09-20
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 2012-12-10
    • 1970-01-01
    • 2020-04-22
    相关资源
    最近更新 更多