Work With Portrait & Landscape Modes in Windows Phone Apps

This article shows how to work with portrait and landscape modes in Windows Phone Apps.

So to make your app work with both landscape and portrait orientations in your Windows Phone app, use the following procedure.

Step 1

Design your XAML page that supports the Portrait orientation like this. The two key points here to make your application support both landscape and portrait is you to add the following two properties to the declarations on the top.

  1. SupportedOrientations="PortraitOrLandscape" Orientation="Portrait"  
  2. OrientationChanged="PhoneApplicationPage_OrientationChanged"  



Figure 1: Hamster

  1. <phone:PhoneApplicationPage  
  2. x:Class="PhoneApp3.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
  6.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"  
  7.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  8.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  9. mc:Ignorable="d"  
  10. FontFamily="{StaticResource PhoneFontFamilyNormal}"  
  11. FontSize="{StaticResource PhoneFontSizeNormal}"  
  12. Foreground="{StaticResource PhoneForegroundBrush}"  
  13. SupportedOrientations="PortraitOrLandscape" Orientation="Portrait"  
  14. shell:SystemTray.IsVisible="False" OrientationChanged="PhoneApplicationPage_OrientationChanged">  
  15.     <phone:PhoneApplicationPage.ApplicationBar>  
  16.         <shell:ApplicationBar IsVisible="True" IsMenuEnabled="False" BackgroundColor="#2A2927" ForegroundColor="#EFCA31">  
  17.             <shell:ApplicationBarIconButton IconUri="/Assets/AppBar/check.png" Text="Login" Click="ApplicationBarIconButton_Click_1"/>  
  18.             <shell:ApplicationBar.MenuItems>  
  19.                 <shell:ApplicationBarMenuItem Text="MenuItem 1"/>  
  20.                 <shell:ApplicationBarMenuItem Text="MenuItem 2"/>  
  21.             </shell:ApplicationBar.MenuItems>  
  22.         </shell:ApplicationBar>  
  23.     </phone:PhoneApplicationPage.ApplicationBar>  
  24.     <Grid x:Name="LayoutRoot" Background="#2A2927">  
  25.         <Grid.RowDefinitions>  
  26.             <RowDefinition Height="Auto"/>  
  27.             <RowDefinition Height="Auto"/>  
  28.             <RowDefinition Height="Auto"/>  
  29.         </Grid.RowDefinitions>  
  30.         <Grid.ColumnDefinitions>  
  31.             <ColumnDefinition Width ="Auto" />  
  32.         </Grid.ColumnDefinitions>  
  33.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">  
  34.             <TextBlock Text="Hamster" Margin="9,0,0,0" Style="{StaticResource PhoneTextTitle1Style}" Foreground="#EFCA31" FontWeight="Light" FontFamily="\Fonts\billabong.ttf#billabong" FontStretch="Expanded" TextWrapping="Wrap" FontSize="100"/>  
  35.         </StackPanel>  
  36.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">  
  37.             <TextBox HorizontalAlignment="Left" Height="85" Margin="0,196,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="456" Padding="5" x:Name="ubox"/>  
  38.             <TextBox HorizontalAlignment="Left" Height="85" Margin="0,306,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="456" Padding="5" x:Name="pbox"/>  
  39.             <TextBlock HorizontalAlignment="Left" Margin="12,169,0,0" TextWrapping="Wrap" Text="username" VerticalAlignment="Top" Foreground="#EFCA31" FontSize="25" x:Name="uname"/>  
  40.             <TextBlock HorizontalAlignment="Left" Margin="12,280,0,0" TextWrapping="Wrap" Text="password" VerticalAlignment="Top" Foreground="#EFCA31" FontSize="25" x:Name="password"/>  
  41.         </Grid>  
  42.     </Grid>  
  43. </phone:PhoneApplicationPage>  
Step 2

Now you need to go to the PhoneApplicationPage_OrientationChanged event and add the following code.
  1. private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e) {  
  2.     PageOrientation orientation = e.Orientation;  
  3.     if ((orientation & PageOrientation.Landscape) == PageOrientation.Landscape) {  
  4.         TitlePanel.Visibility = Visibility.Visible;  
  5.         ContentPanel.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;  
  6.         uname.Margin = new Thickness(12, 10, 0, 0);  
  7.         password.Margin = new Thickness(12, 149, 0, 0);  
  8.         pbox.Margin = new Thickness(0, 188, 0, 0);  
  9.         ubox.Margin = new Thickness(0, 52, 0, 0);  
  10.   
  11.   
  12.     } else if ((orientation & PageOrientation.Portrait) == PageOrientation.Portrait) {  
  13.         TitlePanel.Visibility = Visibility.Visible;  
  14.         ContentPanel.Margin = new Thickness(12, 0, 12, 0);  
  15.         uname.Margin = new Thickness(12, 169, 0, 0);  
  16.         password.Margin = new Thickness(12, 280, 0, 0);  
  17.         pbox.Margin = new Thickness(0, 306, 0, 0);  
  18.         ubox.Margin = new Thickness(0, 196, 0, 0);  
  19.     }  
  20.   
  21.   
  22. }  
So according the page orientations you can provide the values for each of your controls the way they look in both oreintations. If you can observe, I have given the same values for both the XAML and C# page for portrait mode to provide landscape mode values in the code behind you again need to design the same page in landscape mode like this.



Figure 2: Device

So by changing the orientation you again need to design the XAML page and take those values (margin, row, columns, height, width and so on) put behind the C# file of landscape mode. Now just revert back to portrait mode. Now it's done, your app now supports both orientations. Cheers!

Note

Here the reason we are changing the orientation to landscape mode and redesigning the app is to only get the values of the controls and their alignment, since you cannot exactly provide the values behind code for landscape mode.

Note

Try to use the best matching colors always, don't use bright colors when designing an app, look at other famous apps, how they are designed and how they choose colors. In our subconscious, colors have a heavy impact on the person's mood. This effect varies depending on people and their culture, but there is a set of primary colours with a clear influence marked on a wide range of people, for example Blue of the sky gives people a sense of comfort and tranquility and Green is of gardens and provides a sense of happiness and pleasure.