Logo
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
EPL Standings
 
 
Windows Phone

Windows Phone 8 : Orientation and the PhoneApplicationPage Class (part 2) - PhoneApplicationPage Orientation Property

4/28/2014 1:32:13 AM

PhoneApplicationPage Orientation Property

The PhoneApplicationPage includes an Orientation dependency property, which is shown in the following excerpt (take note of the set accessor):

public PageOrientation Orientation
{
    get
    {
        return (PageOrientation)base.GetValue(OrientationProperty);
    }
    [EditorBrowsable(EditorBrowsableState.Never)]
    set
    {
        if (Frame.IsInDesignMode())
        {
            base.SetValue(OrientationProperty, value);
        }
    }
}

You see that changing the page orientation at runtime is not as straightforward as it might first appear. The Orientation property’s set accessor has an effect only at design time and not at runtime. At runtime, the Orientation property indicates the physical orientation of the device, or the orientation of the emulator window. Setting the dependency property directly is also futile and has no effect on runtime page orientation either.

The Orientation property can be used in data binding expressions and allows you to adjust the layout depending on the availability of space. You can maximize space utilization by hiding or revealing content when the orientation changes. For example, when changing to a landscape orientation where horizontal space is more abundant, a TextBlock can be shown in the title section of an application. Conversely, in portrait mode, the TextBlock can be hidden to conserve space, as demonstrated by the following example:

<TextBlock Text="Application Title"
            Visibility="{Binding ElementName=Page, Path=Orientation,
            Converter={StaticResource PageOrientationToVisibilityConverter},
            ConverterParameter=Landscape}" />

You can see that the Orientation property of the PhoneApplicationPage is used to set the Visibility property of the TextBlock using a custom IValueConverter called PageOrientationToVisibilityConverter (see Listing 2), which is located in the WPUnleashed project in the downloadable sample code.

The converter’s ConvertTo method translates the PageOrientation enum value to a System.Windows.Visibility enum value. The ConverterParameter from the previous excerpt indicates when to show the UIElement. If the PageOrientation value is a portrait orientation, for example, and the ConverterParameter is equal to Portrait, then Visibility.Visible will be returned.

LISTING 2. PageOrientationToVisibilityConverter Class


public class PageOrientationToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        var orientation = (PageOrientation)value;
        string showWhenOrientation
            = ArgumentValidator.AssertNotNullAndOfType<string>(
                parameter, "parameter").ToLower();

        if (showWhenOrientation != "portrait"
            && showWhenOrientation != "landscape")
        {
            throw new ArgumentException(
                "ConverterParameter must be either Portrait or Landscape.");
        }

        bool show;
        switch (orientation)
        {
            case PageOrientation.Portrait:
            case PageOrientation.PortraitDown:
            case PageOrientation.PortraitUp:
                show = showWhenOrientation == "portrait";
                break;
            case PageOrientation.Landscape:
            case PageOrientation.LandscapeLeft:
            case PageOrientation.LandscapeRight:
                show = showWhenOrientation == "landscape";
                break;
            default:
                throw new ArgumentException("Unknown orientation: "
                    + orientation);
        }

        return show ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

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
- Microsoft Visio 2010 : Working with Text (part 3) - Text Resizing Behavior
- Customizing OneNote 2010 : Customizing the Ribbon (part 1)
- Windows Server 2003 : Windows Firewall (part 2) - Service Pack Firewall Modifications - Modifications
- Microsoft Excel 2010 : Calculating the Mode (part 3) - Getting the Mode of Categories with a Formula - Accommodating a Function’s Arguments
- Microsoft Dynamics CRM 2011 : Using Advanced Find (part 1) - Performing Advanced Find Queries
- Managing Windows Licensing and Activation : Managing Volume License Activation (part 3) - Managing licensing and activation, Implementing KMS activation
- Deploying Applications Using Group Policy and SCCM 2007 : Deploying Applications Using SCCM 2007 (part 1)
- Windows Server 2012 : Managing Users with Local Security and Group Policies (part 3) - Troubleshooting Group Policy Applications
- Windows Server 2012 : Provisioning and managing shared storage (part 7) - Managing shared storage - Managing volumes, Managing shares
- Microsoft Project 2010 : Fine-Tuning Task Details (part 7) - Entering Fixed Costs
Top 10
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 3) - Creating IPv4 DHCP Scopes
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 2) - Installing DHCP Server and Server Tools
- Windows Server 2012 : DHCP,IPv6 and IPAM - Exploring DHCP (part 1)
- Windows Server 2012 : DHCP,IPv6 and IPAM - Understanding the Components of an Enterprise Network
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 3) - Translating Text with the Mini Translator
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 2) - Translating a Word or Phrase with the Research Pane
- Microsoft OneNote 2010 : Using the Research and Translate Tools (part 1) - Setting Options for the Research Task Pane, Searching with the Research Task Pane
- Microsoft OneNote 2010 : Doing Research with Linked Notes (part 2) - Ending a Linked Notes Session, Viewing Linked Notes
- Microsoft OneNote 2010 : Doing Research with Linked Notes (part 1) - Beginning a Linked Notes Session
- Microsoft OneNote 2010 : Doing Research with Side Notes (part 3) - Moving Side Notes to Your Existing Notes
 
 
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro