通用Windows 10自适应布局问题

时间:2015-11-01 17:54:23

标签: c# xaml uwp

以下是UWP(Windows 10 XAM-C#)的代码似乎没问题但是在为Phone和TabletPC调整窗口大小时无效。网格内部有一个相对的面板伸展。怎么了?需要帮助和非常感谢。

  <Grid Background="Black">

  <Grid.RowDefinitions>

   <RowDefinition Height="Auto" />

   <RowDefinition Height="*" />



     </Grid.RowDefinitions>

       <Grid Name="TitleGrid" Background="Green" Grid.Row="0" >

     <VisualStateManager.VisualStateGroups>

      <VisualStateGroup>

       <VisualState x:Name="Phone" >

        <VisualState.StateTriggers>

        <AdaptiveTrigger MinWindowWidth="0" />

            </VisualState.StateTriggers>

       <VisualState.Setters>

         <Setter Target="CityName.FontSize" Value="14 " />

         <Setter Target="TitleItem.FontSize" Value="14" />

         <Setter Target="CalButton.Width" Value="100" />

    </VisualState.Setters>

   </VisualState>

  </VisualStateGroup>

  <VisualStateGroup>

     <VisualState x:Name="Tablet" >

       <VisualState.StateTriggers>

            <AdaptiveTrigger MinWindowWidth="600" />

      </VisualState.StateTriggers>

    <VisualState.Setters>

           <Setter Target="CityName.FontSize" Value="20" />

           <Setter Target="TitleItem.FontSize" Value="20" />

           <Setter Target="CalButton.Width" Value="150" />

    </VisualState.Setters>

   </VisualState>

 </VisualStateGroup>

 </VisualStateManager.VisualStateGroups>

 <RelativePanel Grid.Row="0" HorizontalAlignment="Stretch">

  <Button Name="HamburgerButton" RelativePanel.AlignLeftWithPanel="True" 

   FontFamily="Segoe MDL2 Assets" 

   Content="&#xE700;" 

   Width="40" 

   Height="40" 

   HorizontalAlignment="Center"

   Foreground="White"

   Click="HamburgerButton_Click" Margin="0,10,0,0" />

  <Button Width="40" Height="40" Name="BackButton" FontFamily="Segoe MDL2    Assets"  Content="&#xE0C4;" RelativePanel.RightOf="HamburgerButton" VerticalAlignment="Center" Click="BackButton_Click" />

  <TextBlock Name="CityName"  Text="NC-Raleigh" Foreground="White"  FontWeight="Bold" RelativePanel.RightOf="BackButton" Margin="10,15,0,0"  />

 <TextBlock Name="TitleItem" Text="ShipList" Foreground="White" FontWeight="Bold" Margin="10,15,0,0" RelativePanel.RightOf="CityName" />

  <CalendarDatePicker Name="CalButton" RelativePanel.RightOf="TitleItem"  Margin="10,15,0,0" PlaceholderText="Today"  />

</RelativePanel>

</Grid>

1 个答案:

答案 0 :(得分:3)

首先你的XAML格式不正确,你已经开始两次Grid标签,但最后没有关闭它。

其次,您的视觉状态是在不同的组中定义的,而它们应该在一个组中,因为我们只有两种不同的状态。

请在下面找到更新后的XAML

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">


        <VisualStateManager.VisualStateGroups>

            <VisualStateGroup>
                <VisualState x:Name="Phone" >

                    <VisualState.StateTriggers>

                        <AdaptiveTrigger MinWindowWidth="1" />

                    </VisualState.StateTriggers>

                    <VisualState.Setters>

                        <Setter Target="CityName.FontSize" Value="14 " />

                        <Setter Target="TitleItem.FontSize" Value="14" />

                        <Setter Target="CalButton.Width" Value="100" />

                    </VisualState.Setters>

                </VisualState>
                <VisualState x:Name="Tablet" >

                    <VisualState.StateTriggers>

                        <AdaptiveTrigger MinWindowWidth="600" />

                    </VisualState.StateTriggers>

                    <VisualState.Setters>

                        <Setter Target="CityName.FontSize" Value="20" />

                        <Setter Target="TitleItem.FontSize" Value="20" />

                        <Setter Target="CalButton.Width" Value="150" />

                    </VisualState.Setters>

                </VisualState>
            </VisualStateGroup>

        </VisualStateManager.VisualStateGroups>

        <Grid x:Name="TitleGrid" Background="Green" Grid.Row="0" >


            <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <RelativePanel Grid.Row="0" HorizontalAlignment="Stretch">

                    <Button x:Name="HamburgerButton" RelativePanel.AlignLeftWithPanel="True" 

                        FontFamily="Segoe MDL2 Assets" 

                        Content="&#xE700;" 

                        Width="40" 

                        Height="40" 

                        HorizontalAlignment="Center"

                        Foreground="White"

                        Click="HamburgerButton_Click" Margin="0,10,0,0" />

                    <Button Width="40" Height="40" x:Name="BackButton" FontFamily="Segoe MDL2    Assets"  Content="&#xE0C4;" RelativePanel.RightOf="HamburgerButton" VerticalAlignment="Center" Click="BackButton_Click" />

                    <TextBlock x:Name="CityName"  Text="NC-Raleigh" Foreground="White"  FontWeight="Bold" RelativePanel.RightOf="BackButton" Margin="10,15,0,0"  />

                    <TextBlock x:Name="TitleItem" Text="ShipList" Foreground="White" FontWeight="Bold" Margin="10,15,0,0" RelativePanel.RightOf="CityName" />

                    <CalendarDatePicker x:Name="CalButton" RelativePanel.RightOf="TitleItem"  Margin="10,15,0,0" PlaceholderText="Today"  />

                </RelativePanel>

            </Grid>

    </Grid>