how to set orientation of one page in maui?

mc 6,076 Reputation points
2025-10-20T01:18:42.5+00:00

I have a page and I want to it be landscape or portrait. and other page keep its own orientation.

when user open this page I want the page be landscape

how to do it?

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

Answer accepted by question author
  1. Starry Night 600 Reputation points
    2025-10-20T07:33:34.19+00:00

    You can use Invoke platform code to achieve this.Platform code can be invoked from cross-platform code by using conditional compilation, or by using partial classes and partial methods.

    Here I try to use partial classes and partial methods to achieve this(Based on the official sample, I added the test code ,which could help you understand this concept.).

    Please refer to the following code:

        public enum DeviceOrientation
        {
            Undefined,
            Landscape,
            Portrait
        }
    
    namespace InvokePlatformCodeDemos.Services.PartialMethods
    {
        public partial class DeviceOrientationService
        {
            public partial DeviceOrientation GetOrientation();
            public partial void SetDeviceOrientation(DisplayOrientation deviceOrientation);
        }
    }
    
    

    In Android

    public partial class DeviceOrientationService
    {
        public partial DeviceOrientation GetOrientation()
        {
            IWindowManager windowManager = Android.App.Application.Context.GetSystemService(Context.WindowService).JavaCast<IWindowManager>();
            SurfaceOrientation orientation = windowManager.DefaultDisplay.Rotation;
            bool isLandscape = orientation == SurfaceOrientation.Rotation90 || orientation == SurfaceOrientation.Rotation270;
            return isLandscape ? DeviceOrientation.Landscape : DeviceOrientation.Portrait;
        }
    
        private static readonly IReadOnlyDictionary<DisplayOrientation, ScreenOrientation> _androidDisplayOrientationMap =
            new Dictionary<DisplayOrientation, ScreenOrientation>
            {
                [DisplayOrientation.Landscape] = ScreenOrientation.Landscape,
                [DisplayOrientation.Portrait] = ScreenOrientation.Portrait,
            };
    // method SetDeviceOrientation
        public partial void SetDeviceOrientation(DisplayOrientation displayOrientation)
        {
            var currentActivity = ActivityStateManager.Default.GetCurrentActivity();
            if (currentActivity is not null)
            {
                if (_androidDisplayOrientationMap.TryGetValue(displayOrientation, out ScreenOrientation screenOrientation))
                {
                    currentActivity.RequestedOrientation = screenOrientation;
                }
            }
        }
    }
    

    In iOS

    public partial class DeviceOrientationService
    {
        public partial DeviceOrientation GetOrientation()
        {
            UIInterfaceOrientation orientation = UIApplication.SharedApplication.StatusBarOrientation;
            bool isPortrait = orientation == UIInterfaceOrientation.Portrait || orientation == UIInterfaceOrientation.PortraitUpsideDown;
            return isPortrait ? DeviceOrientation.Portrait : DeviceOrientation.Landscape;
        }
    
    
        private static readonly IReadOnlyDictionary<DisplayOrientation, UIInterfaceOrientation> _iosDisplayOrientationMap =
           new Dictionary<DisplayOrientation, UIInterfaceOrientation>
           {
               [DisplayOrientation.Landscape] = UIInterfaceOrientation.LandscapeLeft,
               [DisplayOrientation.Portrait] = UIInterfaceOrientation.Portrait,
           };
         // implement method SetDeviceOrientation
        public partial void SetDeviceOrientation(DisplayOrientation displayOrientation)
        {
            if (UIDevice.CurrentDevice.CheckSystemVersion(16, 0))
            {
                var scene = (UIApplication.SharedApplication.ConnectedScenes.ToArray()[0] as UIWindowScene);
                if (scene != null)
                {
                    var uiAppplication = UIApplication.SharedApplication;
                    var test = UIApplication.SharedApplication.KeyWindow?.RootViewController;
                    if (test != null)
                    {
                        test.SetNeedsUpdateOfSupportedInterfaceOrientations();
                        scene.RequestGeometryUpdate(
                            new UIWindowSceneGeometryPreferencesIOS(UIInterfaceOrientationMask.Portrait), error => { });
                    }
                }
            }
            else
            {
                UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
            }
        }
    }
    
    
    

    And use it like this:

    namespace InvokePlatformCodeDemos;
    using PartialMethodsDeviceOrientationService = InvokePlatformCodeDemos.Services.PartialMethods.DeviceOrientationService;
    public partial class NewPage : ContentPage
    {
    	public NewPage()
    	{
    		InitializeComponent();
    	}
        protected override void OnAppearing()
        {
            base.OnAppearing();
            // set the Device Orientation here
            var deviceOrientationService2 = new PartialMethodsDeviceOrientationService();
            deviceOrientationService2.SetDeviceOrientation(DisplayOrientation.Landscape);
        }
    }
    

    For more information, you can refer to : NET MAUI – Locking Orientation into Portrait or Landscape and Invoke platform code.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Michael Le (WICLOUD CORPORATION) 3,585 Reputation points Microsoft External Staff
    2025-10-20T06:42:08.9233333+00:00

    Hello @mc ,

    To set a specific page to landscape orientation while keeping other pages in their default orientation, you'll need to use platform-specific code since orientation is controlled at the native platform level.

    What you need to do is simple:

    • Create a service interface to handle orientation changes
    • Implement this service your platform of choice
    • Use this service in the specific page where you want to enforce landscape orientation

    I recommended using this approach as a reference:

    Step 1: Create an Orientation Service Interface

    public interface IOrientationService
    {
        void SetOrientation(DeviceOrientation orientation);
    }
     
    public enum DeviceOrientation
    {
        Portrait,
        Landscape,
        Unspecified
    }
    

    Step 2: Implement the Service for Android

    Create Platforms/Android/OrientationService.cs:

    using Microsoft.Maui.Controls;
    using Android.Content.PM;
     
    namespace OrientationDemo.Platforms.Android
    {
        public class OrientationService : IOrientationService
        {
            public void SetOrientation(DeviceOrientation orientation)
            {
                try
                {
                    var activity = Platform.CurrentActivity as MainActivity;
                    if (activity == null)
                    {
                        System.Diagnostics.Debug.WriteLine("Activity is null - orientation change skipped");
                        return;
                    }
                   
                    activity.RequestedOrientation = orientation switch
                    {
                        DeviceOrientation.Landscape => ScreenOrientation.Landscape,
                        DeviceOrientation.Portrait => ScreenOrientation.Portrait,
                        _ => ScreenOrientation.Unspecified
                    };
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine($"Failed to change orientation: {ex.Message}");
                }
            }
        }
    }
    

    Step 3: Register the Service

    In your MauiProgram.cs:

    public static class MauiProgram
    {
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            // ... your existing configuration
     
    #if ANDROID
            builder.Services.AddSingleton<IOrientationService, Platforms.Android.OrientationService>();
    #endif
     
            return builder.Build();
        }
    }
    

    Step 4: Use in Your Page

    public partial class YourLandscapePage : ContentPage
    {
        private readonly IOrientationService _orientationService;
       
        public YourLandscapePage(IOrientationService orientationService = null)
        {
            InitializeComponent();
            _orientationService = orientationService ?? (Application.Current?.Handler?.MauiContext?.Services?.GetService<IOrientationService>());
        }
       
        protected override void OnAppearing()
        {
            base.OnAppearing();
            _orientationService?.SetOrientation(DeviceOrientation.Landscape);
        }
       
        protected override void OnDisappearing()
        {
            base.OnDisappearing();
            _orientationService?.SetOrientation(DeviceOrientation.Unspecified);
        }
    }
    

    Step 5: Update Android Manifest

    Ensure your Platforms/Android/AndroidManifest.xml includes orientation changes in configChanges:

    <activity
        android:name="crc64fcf28c0e24b4cc31.MainActivity"
        android:exported="true"
        android:launchMode="singleTop"
        android:theme="@style/Maui.SplashTheme"
        android:configChanges="screenSize|orientation|uiMode|screenLayout|density|smallestScreenSize|colorMode|navigationBarColor">
    </activity>
    

    I hope this helps. Please let me know if you have any questions.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.