0

I have tried setting this code on MainActivity.cs

[Activity(Label = "PG_ELearning.Droid", Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Portrait)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(bundle);

        global::Xamarin.Forms.Forms.Init(this, bundle);

        LoadApplication(new App());
    }
}

But this locked screen rotation for all the screens. I want some screens(In which I am playing video) should have landscape mode. All screens are in Xamarin PCL shared code. I do visited these links:

http://www.appliedcodelog.com/2017/05/force-landscape-or-portrait-for-single.html

How to detect screen orientation of the device in Xamarin.Forms?

But I am not able to find the correct approach.

halfer
  • 19,824
  • 17
  • 99
  • 186
pallav bohara
  • 6,199
  • 6
  • 24
  • 45

1 Answers1

1

you could use MessagingCenter to send your screen direction request:

like this(for example on Android,Ios is similar to this):

in the MainActivity,regist MessagingCenter,(the name and value you could custom)

MessagingCenter.Subscribe<string, int>("direction", "indext", (sender, args) => {
            switch (args)
            {
                case 0:
                    RequestedOrientation = ScreenOrientation.Portrait; //mandatory vertical screen
                    break;
                case 1:
                    RequestedOrientation = ScreenOrientation.Unspecified;//the default value
                    break;
            }
        });

and in your Pages send message :

protected override void OnAppearing()
  {
       base.OnAppearing();
       MessagingCenter.Send<string, int>("direction", "indext", num);//num = 0:Mandatory vertical screen,num = 1 :restore default
  }
Leo Zhu
  • 15,726
  • 1
  • 7
  • 23