Skip to main content
 首页 » 编程设计

c#之Windows Phone 8 页面方向

2024年02月06日15Free-Thinker

如何在页面已加载或正在加载时检测方向?我实现了 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; 
    } 
} 

我该如何解决这个问题?

请您参考如下方法:

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

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