【问题标题】:Screen rotation for specific screens in Xamarin.FormsXamarin.Forms 中特定屏幕的屏幕旋转
【发布时间】:2020-06-17 20:05:04
【问题描述】:

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

但我找不到正确的方法。

【问题讨论】:

  • 可以吗?
  • 抱歉@LeoZhu-MSFT 优先级已更改。我会在 2-3 天内给你反馈
  • 好的,祝你好运!

标签: android xamarin.forms screen-orientation


【解决方案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
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-21
    相关资源
    最近更新 更多