2012-06-26 37 views
0

我在学习如何使用支持的方向来更改文本块中的值或文本。我想要的是,当设备倾斜到横向模式时,文本块应显示“再见”,当它倾斜到肖像模式时,应该说“欢迎”如何在Windows Phone 7上使用屏幕方向显示文字?

我想知道应该使用什么关系运算符在if()语句中,以便它提供正确的输出。

我应该在if()内使用什么?

  1. if(Orientation.Equals(SupportedOrientation.Portrait)) { // display "Welcome"}
  2. if(SupportedOrientation.Equals(SupportedPageOrientation.Portrait)) {// display "Welcome"}

如何使用方向来改变我想要的任何值?

回答

1

如果您要在页面类中编写代码,则可以使用PhoneApplicationPage类的OrientationChanged事件或覆盖OnOrientationChanged方法。

this.OrientationChanged += new EventHandler<OrientationChangedEventArgs>(MainPage_Orientationchanged) 

void MainPage_OrientationChanged(object sender, OrientationChangedEventArgs e) 

    { 
    if (orientation == PageOrientation.LandscapeLeft || 
    orientation == PageOrientation.LandscapeRight) 

    { 

    textblock.text = bye; 

    } 

if (orientation == PageOrientation.PortraitLeft || 
    orientation == PageOrientation.PortraitRight) 

{ 

    textblock.text = welcome; 
} 

} 
+0

谢谢!那工作。我有更多的问题,而不是LandscapeLeft和LandscapeRight,我修改代码为 如果(orientation == PageOrientation.Landscape),我试图执行,它不起作用!为什么它不工作?不“景观”包括左右两侧的景观? –

+0

事件仅在将方向更改为左侧或右侧风景后才会触发。如果向左转动,则事件将检查横向方向是左向还是右向,因为这是方向控制的默认行为。如果你只是简单地提到景观,那么方向控制无法找到运动是从右边还是左边,这就是为什么它不起作用。对于在页面中使用许多UI控件的情况,我们应该对左右横向方向进行不同的控制调整,所以它的必须要检查左右 –

+0

好吧!那么我们什么时候使用Orientation.Landscape? –

相关问题