Windows Phone 8 页面方向

Windows Phone 8 Page Orientation

如何在页面已加载或正在加载时检测方向?我实现了 OrientationChanged 方法。但是当我将第一页设置为横向时,第二页不会触发此方法。页面处于横向模式,但 UI 不是。我的意思是页面方向没问题,但是不能触发 OrientationChanged 吗?我在此方法中更改了 UI 对象的外观。如果未触发,UI 将显示为纵向模式。

private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
    if (e.Orientation == PageOrientation.Landscape || e.Orientation == PageOrientation.LandscapeLeft || e.Orientation == PageOrientation.LandscapeRight)
    {
        SwitchPanel.Margin = new Thickness(12, 100, 250, 0);
        StatusPanel.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
    }
    else
    {
        SwitchPanel.Margin = new Thickness(12, 100, 12, 0);
        StatusPanel.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
    }
}

我该如何解决这个问题?

只需将您的代码放在一个不同的方法中,然后从 OrientationChangedLoaded 事件中调用此方法:

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
    this.SetOrientation(this.Orientation);
}

private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
    this.SetOrientation(e.Orientation);
}

private void SetOrientation(PageOrientation orientation)
{
    if (orientation == PageOrientation.Landscape || orientation == PageOrientation.LandscapeLeft || orientation == PageOrientation.LandscapeRight)
    {
        SwitchPanel.Margin = new Thickness(12, 100, 250, 0);
        StatusPanel.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
    }
    else
    {
        SwitchPanel.Margin = new Thickness(12, 100, 12, 0);
        StatusPanel.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
    }
}

您是否将事件连接到事件处理程序?您的构造函数中应该有类似于以下内容的内容....

this.OrientationChanged += PhoneApplicationPage_OrientationChanged;