How do you set screen orientation programatically?

W

WPCentral Question

Hi all,

I am new to wp8 developing and would like to set my app to only work in landscape mode is this possible?
 

azcruz

Active member
Jul 29, 2013
3,417
0
36
Visit site
Here's the stripped down version of one of my code...

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using SolidColorBrush = System.Windows.Media.SolidColorBrush;

namespace <yourapp namespace>
{
    public partial class SettingsPage : PhoneApplicationPage
    {
        private static AppSettings settings;

        public SettingsPage()
        {
            InitializeComponent();
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            settings = new AppSettings();

            bool locked = settings.OrientationLockSetting;
            bool useAccent = settings.UseThemeColorSetting;

            SetOrientation(locked);
            base.OnNavigatedTo(e);
        }
        private void SetOrientation(bool locked)
        {
            this.SupportedOrientations = locked ? SupportedPageOrientation.Portrait : SupportedPageOrientation.PortraitOrLandscape;
        }
    }
}

You can replace this

this.SupportedOrientations = locked ? SupportedPageOrientation.Portrait : SupportedPageOrientation.PortraitOrLandscape;

with this

this.SupportedOrientations = SupportedPageOrientation.Portrait;
 

Members online

Forum statistics

Threads
323,276
Messages
2,243,561
Members
428,053
Latest member
JoshRos