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

问题描述 投票:0回答:1

我已经尝试在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?

但我无法找到正确的方法。任何帮助都会很棒!

android xamarin.forms screen-orientation
1个回答
1
投票

您可以使用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
  }
© www.soinside.com 2019 - 2024. All rights reserved.