我尝试在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?
但是我找不到正确的方法。任何帮助都会很棒!。
答案 0 :(得分: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
}