当页面已加载或页面正在加载时,如何检测方向?我实现了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;
}
}我怎样才能解决这个问题?
发布于 2015-02-27 16:34:58
只需将代码放在一个不同的方法中,并从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;
}
}发布于 2015-02-27 16:42:58
你把事件连接到事件处理程序了吗?构造函数中应该有类似于以下内容的内容.
this.OrientationChanged += PhoneApplicationPage_OrientationChanged;https://stackoverflow.com/questions/28769128
复制相似问题