Forum Xamarin.Forms

How to force a single page to display in landscape mode?

ChessmanJoshChessmanJosh Member
edited February 22 in Xamarin.Forms

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:
heartbeat . fritz . ai / force-an-orientation-on-a-single-page-in-xamarin-forms-b9c0c5295367 (remove the spaces, I can't post links yet)

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

Best Answer

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    edited February 23 Accepted Answer

    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 the 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", "");
            }
    

    Click the pop button, then the screen will be Landscape.


    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    edited February 23 Accepted Answer

    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 the 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", "");
            }
    

    Click the pop button, then the screen will be Landscape.


    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

Sign In or Register to comment.