.NET life

HUGON Jérôme
Microsoft Certified Technology Specialist Microsoft Certified Application Developer Microsoft Certified Professional

Change screen orientation of a Microsoft Surface application

Surface

Category Surface  | Publication Date : 1/18/2010

The property Microsoft.Surface.ApplicationLauncher.Orientation lets you know the orientation of the application. The event Microsoft.Surface.ApplicationLauncher.OrientationChanged from the SDK is triggered when the user changes its direction by touching one of the access button.

The principle is to rotate the application with a RotateTransform and an angle calculated according to the current orientation. RotateTransform is set on the root element of SurfaceWindow so that the change be passed on the full application.

 

 

<s:SurfaceWindow x:Class="SandBox.SurfaceWindow1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:s="http://schemas.microsoft.com/surface/2008"
    Title="SandBox">
    <Grid Name="my_grid">
        <Grid.LayoutTransform>
            <RotateTransform x:Name="rtOrientation" Angle="0"/>
        </Grid.LayoutTransform>
        <TextBlock Name="tbOrientation" HorizontalAlignment="Center"/>
    </Grid>
</s:SurfaceWindow>

 

 

It remains for us to subscribe to the event ApplicationLauncher.OrientationChanged in the method AddActivationHandlers  and into the associated event handler, we can change the angle of the object RotateTransform based on the current orientation obtained with the by the Orientation property of ApplicationLauncher.

 

 

private void AddActivationHandlers()
{

ApplicationLauncher.ApplicationActivated += OnApplicationActivated; ApplicationLauncher.ApplicationPreviewed += OnApplicationPreviewed; ApplicationLauncher.ApplicationDeactivated += OnApplicationDeactivated;
ApplicationLauncher.OrientationChanged += ApplicationLauncher_OrientationChanged; } private void ApplicationLauncher_OrientationChanged(object sender, OrientationChangedEventArgs e) { this.rtOrientation.Angle = ApplicationLauncher.Orientation == UserOrientation.Top ? 180 : 0; this.tbOrientation.Text = ApplicationLauncher.Orientation.ToString(); }