Xamarin.Forms-根据设备分辨率更改设备方向

时间:2020-07-12 12:55:09

标签: c# visual-studio xamarin xamarin.forms

是否有机会强制更改基于设备屏幕分辨率的屏幕方向?

有适用于平板电脑的应用程序,但我也想在移动设备上运行该应用程序。我有几页,需要在电话上更改方向以使其横向显示,因为那样我的列表视图有点混乱。

谢谢您的任何建议,对于英语不好,我深表歉意。

2 个答案:

答案 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 来检查设备是PhoneTablet还是其他设备

DeviceInfo.Idiom 关联一个常量字符串,该常量字符串映射到应用程序正在运行的设备的类型。可以使用DeviceIdiom结构检查这些值:

  • DeviceIdiom.Phone –电话
  • DeviceIdiom.Tablet –平板电脑
  • DeviceIdiom.Desktop –桌面
  • DeviceIdiom.TV –电视
  • DeviceIdiom.Watch –观看
  • DeviceIdiom.Unknown –未知

修改后的 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

中进一步了解有关设备类的信息
相关问题