Logo
HOW TO
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
 
 
Windows Phone

Windows Phone 8 : Orientation and the PhoneApplicationPage Class - PhoneApplicationPage Orientation Property

8/1/2014 4:24:32 AM

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 1), 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 1. 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 : The Multimedia Experience - Xbox Music Pass (part 2) - Playing Music from the Xbox Music Pass
- Windows Phone 8 : The Multimedia Experience - Xbox Music Pass (part 1) - Connecting Your Phone to Xbox Music Pass
- Windows Phone 8 : Working with File Explorer (part 3) - Copying Multimedia Content to Your PC
- Windows Phone 8 : Working with File Explorer (part 2) - Removing Media from Your Phone
- Windows Phone 8 : Working with File Explorer (part 1) - Adding Media to Your Phone
- Windows Phone 8 : Orientation and the PhoneApplicationPage Class (part 5) - Animating the Entire Page When Orientation Changes
- Windows Phone 8 : Orientation and the PhoneApplicationPage Class (part 4) - Animating Page Elements When the Page Orientation Changes
- Windows Phone 8 : Orientation and the PhoneApplicationPage Class (part 3) - Setting Page Orientation at Runtime
- Windows Phone 8 : Orientation and the PhoneApplicationPage Class (part 2) - PhoneApplicationPage Orientation Property
- Windows Phone 8 : Orientation and the PhoneApplicationPage Class (part 1) - OrientationChanged Event
 
 
REVIEW
- First look: Apple Watch

- 10 Amazing Tools You Should Be Using with Dropbox
 
VIDEO TUTORIAL
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
 
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS
Popular keywords
HOW TO Swimlane in Visio Visio sort key Pen and Touch Creating groups in Windows Server Raid in Windows Server Exchange 2010 maintenance Exchange server mail enabled groups Debugging Tools Collaborating
Top 10
- Microsoft Excel : How to Use the VLookUp Function
- Fix and Tweak Graphics and Video (part 3) : How to Fix : My Screen Is Sluggish - Adjust Hardware Acceleration
- Fix and Tweak Graphics and Video (part 2) : How to Fix : Text on My Screen Is Too Small
- Fix and Tweak Graphics and Video (part 1) : How to Fix : Adjust the Resolution
- Windows Phone 8 Apps : Camera (part 4) - Adjusting Video Settings, Using the Video Light
- Windows Phone 8 Apps : Camera (part 3) - Using the Front Camera, Activating Video Mode
- Windows Phone 8 Apps : Camera (part 2) - Controlling the Camera’s Flash, Changing the Camera’s Behavior with Lenses
- Windows Phone 8 Apps : Camera (part 1) - Adjusting Photo Settings
- MDT's Client Wizard : Package Properties
- MDT's Client Wizard : Driver Properties
 
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro