Search code examples
windows-phone-7windows-phone-7.1screen-orientation

How to use Screen Orientation on a Windows phone 7 to display text?


I'm learning how to use Supported Orientations to change values or texts in a textblock. What I want is that when the device is tilted to landscape mode, the textblock should display "Bye" and when it is tilted to portrait mode, it should say "Welcome"

I want to know what relational operators should be used inside the if() statement so that it gives the correct output.

What should I use inside if()?

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

How can I use orientation to change any values I want?


Solution

  • you can use either the OrientationChanged event of the PhoneApplicationPage class, or override the OnOrientationChanged method if you're writing code in your page class.

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