Force Landcape Mode only on some views .net Maui

Dustin 41 Reputation points
2022-09-26T14:02:24.153+00:00

I want to force landscape mode only on some selected views and not for the whole App.
I was only able to find a way to force the whole app into landscape mode.
I also want to have the forced landscape only ontablets not on phones.

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

1 answer

Sort by: Most helpful
  1. Anonymous
    2022-09-27T04:35:31.643+00:00

    Hello,

    I want to force landscape mode only on some selected views and not for the whole App.

    Do you want to set landscape mode in specific ContentPage?
    If so, you can invoke native platform code to implement it.

    You can use Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.RequestedOrientation = Android.Content.PM.ScreenOrientation.Locked; and Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.RequestedOrientation = Android.Content.PM.ScreenOrientation.Landscape; to lock the page to Landcape Mode.

    You can use Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.RequestedOrientation = Android.Content.PM.ScreenOrientation.User; to reset it like following code.

       public class DeviceOrientationService  
           {  
               public void SetOrientation()  
               {  
       #if ANDROID  
       Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.RequestedOrientation =    Android.Content.PM.ScreenOrientation.Locked;  
        Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.RequestedOrientation = Android.Content.PM.ScreenOrientation.Landscape;  
       #endif  
               }  
              public void RestSetOrientation()  
               {  
       #if ANDROID  
       Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.RequestedOrientation =    Android.Content.PM.ScreenOrientation.User;  
                
       #endif  
               }  
           }  
    

    For example, you have page called page1, you need to Force Landcape Mode, you can execute deviceOrientationService.SetOrientation(); in OnAppearing method and deviceOrientationService.RestSetOrientation(); in OnDisappearing method.

       protected override void OnAppearing()  
           {  
               base.OnAppearing();  
               DeviceOrientationService deviceOrientationService = new DeviceOrientationService();  
               deviceOrientationService.SetOrientation();  
           }  
         
       override protected void OnDisappearing()  
           {  
               base.OnDisappearing();  
               DeviceOrientationService deviceOrientationService = new DeviceOrientationService();  
               deviceOrientationService.RestSetOrientation();  
           }  
    

    I also want to have the forced landscape only ontablets not on phones.

    You can use get the device type from the MAUI in app.xaml.cs. If the type is Tablet, you can forced landscape like following code.

       public App()  
           {  
               InitializeComponent();  
              MainPage = new AppShell();  
              DetectDeviceType();  
          }  
         
         
         
          private void DetectDeviceType()  
           {  
               if (DeviceInfo.Current.Idiom == DeviceIdiom.Tablet)  
                     
               {  
                   Console.WriteLine("The current device is a Tablet");  
                   DeviceOrientationService deviceOrientationService = new DeviceOrientationService();  
                   deviceOrientationService.SetOrientation();  
               }  
                    
           }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

Your answer

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