Windows Phone 8页面方向

时间:2015-02-27 15:54:40

标签: c# windows-phone-8

如何在加载页面或加载页面时检测方向?我实现了OrientationChanged方法。但是当我将第一页设置为横向时,第二页不会触发此方法。页面处于横向模式,但UI不是。我的意思是页面方向正常,但是要触发mus 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;
    }
}

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:4)

只需将代码放在一个不同的方法中,然后从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;
    }
}

答案 1 :(得分:0)

您是否将事件连接到事件处理程序?你应该在构造函数中有类似的东西......

this.OrientationChanged += PhoneApplicationPage_OrientationChanged;