根据 xaml Windows 应用中的方向检测更改布局

change layout based on orientation detect in xaml Windows app

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

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

  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

但这对我不起作用。不知道我做错了什么。?我正在通过模拟器对其进行测试。

 <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>

VisualStates 的自动触发器是 Windows 8 中的 LayoutAwarePage 引入的。但是对于 Windows 8.1,Snapped 屏幕的概念已被删除,指南是将您的应用程序设计为 320 像素(清单中的设置)或 500 像素(默认)作为最小尺寸。方向更改的自动触发器也已 删除

然而,有一些开发人员试图重现 Windows 8 行为,例如 this post from Jeremy Likness。您仍然可以 write/use 编写类似于他在现有 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.