How to force a single page to be displayed in landscape mode?

Josh 21 Reputation points
2021-02-22T23:47:01.003+00:00

Hello, I am an amateur developer working on my first Xamarin application. In my situation, I have a page that contains a video and two buttons that I'd like to lock in landscape mode, but I don't want the other pages to also be locked in landscape as well (in fact, most of the rest of the application should be locked in portrait mode)

Can you please give me pointers on how to do that? I've been working with links like:
https://heartbeat.fritz.ai/force-an-orientation-on-a-single-page-in-xamarin-forms-b9c0c5295367

But I cannot get this to work because MessagingCenter "does not exist in the current context" inside MainActivity.cs

A huge thank you in advance

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

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 71,596 Reputation points Microsoft Vendor
    2021-02-23T01:41:46.203+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    MessagingCenter "does not exist in the current context" inside MainActivity.cs

    Please copy MessagingCenter is placed below LoadApplication(new App()); like following code.

       [Activity(Label = "App52", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )]  
           public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity  
           {  
               protected override void OnCreate(Bundle savedInstanceState)  
               {  
                   TabLayoutResource = Resource.Layout.Tabbar;  
                   ToolbarResource = Resource.Layout.Toolbar;  
                   base.OnCreate(savedInstanceState);  
                   Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
                   global::Xamarin.Forms.Forms.Init(this, savedInstanceState);  
                   LoadApplication(new App());  
         
         
                   Xamarin.Forms.MessagingCenter.Subscribe<App, string>(App.Current, "AllowLandscape", (snd, arg) =>  
                   {  
                       RequestedOrientation = ScreenOrientation.Landscape;  
                   });  
                   Xamarin.Forms.MessagingCenter.Subscribe<App, string>(App.Current, "PreventLandscape", (snd, arg) =>  
                   {  
                       RequestedOrientation = ScreenOrientation.Portrait;  
                   });  
         
         
         
               }  
               public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)  
               {  
                   Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
         
                   base.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
               }  
           }  
    

    Then, copy following code in the Forms contentpage.

       protected override void OnAppearing()  
               {  
                   base.OnAppearing();  
         
                   MessagingCenter.Send<App, string>((App)App.Current, "AllowLandscape", "");  
               }  
         
               protected override void OnDisappearing()  
               {  
                   base.OnDisappearing();  
         
                   MessagingCenter.Send<App, string>((App)App.Current, "PreventLandscape", "");  
               }  
    

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful