在xaml Windows应用程序中基于方向检测更改布局

时间:2015-12-18 10:14:23

标签: xaml win-universal-app windows-8.1-universal

我正在尝试根据横向和纵向方向改变我的观点。

我尝试了几个地方的代码,例如:

  1. How to detect orientation changes and change layout?

  2. https://social.msdn.microsoft.com/Forums/en-US/a2064993-e4a9-4a58-8498-ef03ed9403f4/how-to-set-grid-row-property-in-visualstatemanager-in-windows8-app?forum=winappswithcsharp

  3. 但它对我不起作用。不确定我做错了什么。我正在从模拟器中测试它。

     <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="1*" />
                <RowDefinition Height="1*" />
                <RowDefinition Height="18*" />
            </Grid.RowDefinitions>
    <Grid/>
    <Grid Grid.Row=1/>
    <Grid Grid.Row="2">
        <Grid.RowDefinitions>
            <RowDefinition Height="4*" />
            <RowDefinition Height="14*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid Grid.Row="0" x:Name="PageTotals" />
    
        <Grid Grid.Row="1" x:Name="PageDetails" />
    
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="ApplicationViewStates">
                <VisualState x:Name="Filled"/>
                <VisualState x:Name="FullScreenPortrait"/>
                <VisualState x:Name="Snapped"/>
                <VisualState x:Name="FullScreenLandscape">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PageTotals"
                                               Storyboard.TargetProperty="(Grid.Column)">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="0"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PageTotals"
                                               Storyboard.TargetProperty="(Grid.RowSpan)">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="2"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PageDetails"
                                               Storyboard.TargetProperty="(Grid.Column)">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="1"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PageDetails"
                                               Storyboard.TargetProperty="(Grid.RowSpan)">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="2"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
    

1 个答案:

答案 0 :(得分:0)

{8}中的VisualStates引入了LayoutAwarePage的自动触发器。但是在Windows 8.1中,Snapped屏幕的概念已被删除,指导是设计您的应用对于320px(在清单中设置)或500px(默认值)作为最小大小。方向更改的自动触发器也已移除

然而,有些开发人员试图重现Windows 8的行为,例如this post from Jeremy Likness。你仍然可以在现有的8.1 ​​API上编写/使用类似于他的实现的代码来测试方向,如果你的应用是全屏的,那么你自己使用VisualStateManager.GoToState转到所需的状态。

private static void SetLayout(Control control)
{
    var orientation = ApplicationView.GetForCurrentView().Orientation;
    string newMode;

    if (orientation == ApplicationViewOrientation.Landscape)
    {
        newMode = ApplicationView.GetForCurrentView().IsFullScreen ? "FullScreenLandscape" : "Filled";
    }
    else
    {
        newMode = Window.Current.Bounds.Width <= 500 ? "Snapped" : "FullScreenPortrait";
    }

    if (newMode == GetLastOrientation(control))
    {
        return;
    }

    VisualStateManager.GoToState(control, newMode, true);
    SetLastOrientation(control, newMode);
}

来源:Jeremy's post