Is it possible to allow detection of device orientation change for only one page

Wei Wen 1,126 Reputation points
2021-09-27T14:43:00.12+00:00

My project is set to only allow portrait. But on one page, I want it to be able to detect user rotation of the device from either portrait to landscape, or from landscape and portrait, and then change the page's orientation correspondingly. I did some search, and found that I can use MessagingCenter to notify, for example, MainActivity in Android to change the orientation to the desired orientation. But that is still not what I want. That implementation is not able to detect user's rotation of the device.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,372 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 78,836 Reputation points Microsoft Vendor
    2021-09-28T06:15:13.127+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Is it possible to allow detection of device orientation change for only one page

    If you set the ScreenOrientation = ScreenOrientation.Portrait in the MainActivity.cs to keep the only allow portrait. This answer is Yes

    If you want to allow detection of device orientation change for only one page, You can an interface in PCL.

       public interface IDetectScreen  
           {  
               void EnableDetectOrientation();  
               void CloseDetectOrientation();  
           }  
    

    Then achieve it in the Dependency service in android platform.

       [assembly: Xamarin.Forms.Dependency(typeof(DetectScreenService))]  
         
       namespace AudioDemo.Droid  
       {  
           class DetectScreenService : IDetectScreen  
           {  
               public void CloseDetectOrientation()  
               {  
                   
                   MainActivity.Intance.RequestedOrientation = Android.Content.PM.ScreenOrientation.Portrait;  
         
               }  
         
               public void EnableDetectOrientation()  
               {  
                   MainActivity.Intance.RequestedOrientation = Android.Content.PM.ScreenOrientation.Sensor;  
         
               }  
           }  
       }  
    

    MainActivity.Instance. is a public static value .

       public static MainActivity Instance;  
         
               protected override void OnCreate(Bundle savedInstanceState)  
               {  
                   base.OnCreate(savedInstanceState);  
         
       //set the value to Intance   
         
                   Instance= this;  
         
         
                   Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
                   global::Xamarin.Forms.Forms.Init(this, savedInstanceState);  
                   LoadApplication(new App());  
         
                    
               }  
    

    To use the above, subclass ContentPage and override the OnAppearing and OnDisappearing methods and subscribe to the DeviceDisplay.MainDisplayInfoChanged event to detect and test that the change occurred as in the following code sample:

       public partial class Page1 : ContentPage  
           {  
               public Page1()  
               {  
                   InitializeComponent();  
         
                   DeviceDisplay.MainDisplayInfoChanged += OnMainDisplayInfoChanged;  
         
               }  
               private void OnMainDisplayInfoChanged(object sender, DisplayInfoChangedEventArgs e)  
               {  
                   var displayInfo = e.DisplayInfo;  
         
                   var rotation = displayInfo.Rotation;  
                   var displayOrientation = displayInfo.Orientation;  
         
         
                   DisplayAlert("info", rotation + " " + displayOrientation, "OK");  
               }  
               protected override void OnAppearing()  
               {  
                   base.OnAppearing();  
                   DependencyService.Get<IDetectScreen>().EnableDetectOrientation();  
               }  
         
               protected override void OnDisappearing()  
               {  
                   base.OnDisappearing();  
                   DependencyService.Get<IDetectScreen>().CloseDetectOrientation();  
         
         
               }  
           }  
    

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    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.


0 additional answers

Sort by: Most helpful

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.