是否有机会强制更改基于设备屏幕分辨率的屏幕方向?
有适用于平板电脑的应用程序,但我也想在移动设备上运行该应用程序。我有几页,需要在电话上更改方向以使其横向显示,因为那样我的列表视图有点混乱。
谢谢您的任何建议,对于英语不好,我深表歉意。
答案 0 :(得分:1)
您可以使用 MessagingCenter 来实现。
例如,从Forms中的 MainPage 导航到 PageTwo 时,PageTwo
的内容页面如下:
public partial class PageTwo : ContentPage
{
public PageTwo()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Send(this, "allowLandScape");
}
//during page close setting back to portrait
protected override void OnDisappearing()
{
base.OnDisappearing();
MessagingCenter.Send(this, "quitLandScape");
}
}
在 Android 中,需要按以下步骤修改MainActivity:
[Activity(Label = "ForceOrientation", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Portrait)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
//allowing the device to change the screen orientation based on the rotation
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
MessagingCenter.Subscribe<PageTwo>(this, "allowLandScape", sender =>
{
RequestedOrientation = ScreenOrientation.Landscape;
});
//during page close setting back to portrait
MessagingCenter.Subscribe<PageTwo>(this, "quitLandScape", sender =>
{
RequestedOrientation = ScreenOrientation.Portrait;
});
}
}
在 iOS 中,我们需要创建一个 PageTwoRenderer ,如下所示:
[assembly: ExportRenderer(typeof(PageTwo), typeof(PageTwoRenderer))]
namespace ForceOrientation.iOS
{
public class PageTwoRenderer : PageRenderer
{
public PageTwoRenderer()
{
MessagingCenter.Subscribe<PageTwo>(this, "allowLandScape", sender =>
{
UIDevice.CurrentDevice.SetValueForKey(NSNumber.FromNInt((int)(UIInterfaceOrientation.LandscapeLeft)), new NSString("orientation"));
});
//during page close setting back to portrait
MessagingCenter.Subscribe<PageTwo>(this, "quitLandScape", sender =>
{
UIDevice.CurrentDevice.SetValueForKey(NSNumber.FromNInt((int)(UIInterfaceOrientation.Portrait)), new NSString("orientation"));
});
}
}
}
=============================更新================ ============
我们可以使用 Xamarin.Essentials: Device Information 来检查设备是Phone
,Tablet
还是其他设备。
DeviceInfo.Idiom 关联一个常量字符串,该常量字符串映射到应用程序正在运行的设备的类型。可以使用DeviceIdiom结构检查这些值:
修改后的 PageTwo 如下:
protected override void OnAppearing()
{
base.OnAppearing();
var idiom = DeviceInfo.Idiom;
if (idiom == DeviceIdiom.Phone)
{
MessagingCenter.Send(this, "allowLandScape");
}
}
//during page close setting back to portrait
protected override void OnDisappearing()
{
base.OnDisappearing();
var idiom = DeviceInfo.Idiom;
if (idiom == DeviceIdiom.Phone)
{
MessagingCenter.Send(this, "quitLandScape");
}
}
答案 1 :(得分:0)
您可以在这几页上强制设置方向
// on IOS -> AppDelegate.cs
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, UIWindow forWindow)
{
if (Device.Idiom == TargetIdiom.Phone ) {
{
return UIInterfaceOrientationMask.Landscape;
}
else
{
return UIInterfaceOrientationMask.Portrait;
}
}
// and on Android -> MainActivity.cs do the same if else in here
protected override void OnCreate(Bundle savedInstanceState)
{
if (Device.Idiom == TargetIdiom.Phone)
{
RequestedOrientation = ScreenOrientation.Landscape;
}
else
{
RequestedOrientation = ScreenOrientation.Portrait;
}
}
您可以在文档https://docs.microsoft.com/en-us/xamarin/xamarin-forms/platform/device#deviceidiom
中进一步了解有关设备类的信息