Xamarin.Forms 中特定屏幕的屏幕旋转

Screen rotation for specific screens in Xamarin.Forms

我试过在 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());
    }
}

但是所有屏幕的锁屏旋转。我想要一些屏幕(我正在播放视频)应该有横向模式。所有屏幕都在 Xamarin PCL 共享代码中。 我确实访问了这些链接:

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

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

但是我找不到正确的方法。

您可以使用MessagingCenter发送您的屏幕方向请求:

像这样(比如在Android,Ios上类似这样):

MainActivity中,注册MessagingCenter,(名称和值你可以自定义)

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;
            }
        });

并在您的 Pages 中发送消息:

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