Logo
PREGNANCY
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
 
 
Windows Phone

Windows Phone 8 : Orientation and the PhoneApplicationPage Class (part 1) - OrientationChanged Event

4/28/2014 1:31:29 AM

The PhoneApplicationPage class includes two orientation related properties: SupportedOrientations and Orientation.

The SupportedOrientations attribute allows you to restrict the orientation of the page and, if set to either Portrait or Landscape, prevent the orientation from being changed when the device is rotated. If the page is designed to support both portrait and landscape, set SupportedOrientation to PortraitOrLandscape, which allows the orientation to be switched automatically when the device is rotated.

The Orientation property indicates the actual orientation of the page and can be set only at design time.

When you create a new PhoneApplicationPage within Visual Studio, both SupportedOrientations and Orientation are set, by default, to Portrait in XAML.

1. OrientationChanged Event

Both the PhoneApplicationFrame and PhoneApplicationPage include an OrientationChanged event that allows you to detect when the orientation of the page changes. In addition, the PhoneApplicationPage includes an OnOrientationChanged virtual method. When the page orientation changes, the method is called, allowing you to update the UI, trigger animations, and so forth. See the following excerpt:

protected override void OnOrientationChanged(
                            OrientationChangedEventArgs e)
{
    base.OnOrientationChanged(e);
    /* Update UI, trigger animations etc. */
}

The OnOrientationChanged method is called before other OrientationChanged event handlers.


Note

The ActualWidth and ActualHeight of the page are not changed until after the OrientationChanged event has been raised.

To change the size of a UI element based on the dimensions of the page after the orientation change occurs use the Dispatcher.


By using the Dispatcher to invoke layout changes, the correct height and width of the page can be determined after the OrientationChanged event has been handled, as shown in the following excerpt:

protected override void OnOrientationChanged(OrientationChangedEventArgs e)
{
    Debug.WriteLine("Orientation changed to " + e.Orientation.ToString("G"));

    Dispatcher.BeginInvoke(
        delegate
        {
            Debug.WriteLine(string.Format(
                "Using dispatcher: ActualWidth: {0}, ActualHeight: {1}",
                ActualWidth, ActualHeight));
        });

    Debug.WriteLine(string.Format(
        "Without dispatcher: ActualWidth: {0}, ActualHeight: {1}",
        ActualWidth, ActualHeight));

    base.OnOrientationChanged(e);
}

The following is the output when switching orientations:

Orientation changed to LandscapeLeft
Without dispatcher: ActualWidth: 0, ActualHeight: 0
Using dispatcher: ActualWidth: 800, ActualHeight: 480
Orientation changed to PortraitUp
Without dispatcher: ActualWidth: 800, ActualHeight: 480
Using dispatcher: ActualWidth: 480, ActualHeight: 800

The OrientionChanged event is always raised before the page is loaded. This explains the zero values for the ActualWidth and ActualHeight in the previous output. The Dispatcher allows the correct width and height values to be obtained because by the time each value is read, the page has already loaded and the properties are populated with the correct values.

The OrientationChangedEventArgs class contains an Orientation property, which is an enum of type PageOrientation, indicating the new page orientation. PageOrientation has the following values:

- Landscape

- LandscapeLeft

- LandscapeRight

- None

- Portrait

- PortraitDown

- PortraitUp

The only values you will ever see in the OrientationChangedEventArgs are, however, LandscapeLeft, LandscapeRight, and PortraitUp. These values indicate the location of the display in relation to the phone hardware buttons (see Figure 1).

Image

FIGURE 1 Valid device orientations.

Although PortraitDown exists as an enum value, at the time of writing, no device supports this orientation, nor does the emulator.

To determine whether the OrientationChangedEventArgs.Orientation value is either landscape or portrait, the value can be ANDed with the PageOrientation.Landscape or PageOrientation.Portrait values, respectively. The PageOrientationExtensions class in the downloadable sample code includes two extension methods for performing this directly on a PageOrientation value (see Listing 1).

LISTING 1. PageOrientationExtensions Class


public static class PageOrientationExtensions
{
    public static bool IsLandscape(this PageOrientation pageOrientation)
    {
        return (pageOrientation & PageOrientation.Landscape) != 0;
    }

    public static bool IsPortrait(this PageOrientation pageOrientation)
    {
        return (pageOrientation & PageOrientation.Portrait) != 0;
    }
}

Other -----------------
- Windows Phone 8 : Working with the Windows Phone Software (part 9) - Copying Phone Content to Your PC or Tablet
- Windows Phone 8 : Working with the Windows Phone Software (part 8) - Removing Multimedia Content - Removing Pictures from Your Phone
- Windows Phone 8 : Working with the Windows Phone Software (part 7) - Removing Multimedia Content - Removing a Video from Your Phone
- Windows Phone 8 : Working with the Windows Phone Software (part 6) - Removing Multimedia Content - Removing Music from Your Phone
- Windows Phone 8 : Working with the Windows Phone Software (part 5) - Using the Photo Interface
- Windows Phone 8 : Working with the Windows Phone Software (part 4) - Adding Content from Nonstandard Locations
- Windows Phone 8 : Working with the Windows Phone Software (part 3) - Adding an Album to Your Phone,Adding a Musical Artist to Your Phone
- Windows Phone 8 : Working with the Windows Phone Software (part 2) - Adding Videos to Your Phone,Adding a Song to Your Phone
- Windows Phone 8 : Working with the Windows Phone Software (part 1) - Adding Photos to Your Phone
- Windows Phone 8 : Configuring Basic Device Settings - Phone Storage
- Windows Phone 8 : Configuring Basic Device Settings - Battery Saver
- Windows Phone 8 : Configuring Basic Device Settings - Backing Up Your Phone (part 5) - Restoring Your Backups
- Windows Phone 8 : Configuring Basic Device Settings - Backing Up Your Phone (part 4) - Determining Backup Quality for Your Photos and Videos
- Windows Phone 8 : Configuring Basic Device Settings - Backing Up Your Phone (part 3) - Backing Up Text Messages
- Windows Phone 8 : Configuring Basic Device Settings - Backing Up Your Phone (part 2) - Removing Your Backup
- Windows Phone 8 : Configuring Basic Device Settings - Backing Up Your Phone (part 1) = Backing Up App Lists and Settings
- Windows Phone 8 : Configuring Basic Device Settings - Providing Feedback
- Windows Phone 8 : Configuring Basic Device Settings - About Your Phone
- Windows Phone 8 : Configuring Basic Device Settings - Find My Phone
- Windows Phone 8 : Configuring Basic Device Settings - Accessibility (part 2) - Enabling the Screen Magnifier, Using Speech for Phone Accessibility
 
 
Most view of day
- Windows Phone 8 : Scheduled Tasks - To-Do List Scheduled Task Sample (part 4) - Creating the To-Do Item Shell Tile, Saving a To-Do Item
- Microsoft Visio 2010 : Adding a Photo to a Document
- Windows Server 2003 on HP ProLiant Servers : The Pilot
- Windows Server 2012 : Configuring IPsec (part 5) - Configuring connection security rules - Creating an authentication exemption rule, Creating a server-to-server rule, Creating a tunnel rule
- Windows Server 2012 : Provisioning and managing shared storage (part 5) - Provisioning SMB shares - Creating general-purpose SMB shares
- Microsoft Dynamics AX 2009 : Integration with Microsoft Office - Sending email using Outlook
- Windows Small Business Server 2011 : Disaster Planning - Preparing for a Disaster, Restoring from Backup
- Nginx HTTP Server : Basic Nginx Configuration - A configuration for your profile
- SharePoint 2010 : Building Composite Solutions (part 1) - External Lists, External Data Columns
- Microsoft Visio 2010 : Sharing and Publishing Diagrams - Saving Visio-Created Websites on a SharePoint Server
Top 10
- Windows Server 2012 : Configuring IPsec (part 7) - Configuring connection security rules - Monitoring IPsec
- Windows Server 2012 : Configuring IPsec (part 6) - Configuring connection security rules - Creating a custom rule, Configuring authenticated bypass
- Windows Server 2012 : Configuring IPsec (part 5) - Configuring connection security rules - Creating an authentication exemption rule, Creating a server-to-server rule, Creating a tunnel rule
- Windows Server 2012 : Configuring IPsec (part 4) - Configuring connection security rules - Types of connection security rules, Creating an isolation rule
- Windows Server 2012 : Configuring IPsec (part 3) - Configuring IPsec settings - Customizing IPsec tunnel authorizations, Configuring IPsec settings using Windows PowerShell
- Windows Server 2012 : Configuring IPsec (part 2) - Configuring IPsec settings - Customizing IPsec defaults
- Windows Server 2012 : Configuring IPsec (part 1) - Understanding connection security
- Microsoft Project 2010 : Linking Tasks (part 8) - Auditing Task Links,Using the Task Inspector
- Microsoft Project 2010 : Linking Tasks (part 7) - Creating Links by Using the Mouse,Working with Automatic Linking Options
- Microsoft Project 2010 : Linking Tasks (part 6) - Creating Links by Using the Entry Table
 
 
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro