首页 > 解决方案 > Xamarin.Forms - 根据设备分辨率更改设备方向

问题描述

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

有平板电脑的应用程序,但我也想在移动设备上运行应用程序。我有几页,我需要在手机上将方向更改为横向,因为那时我的列表视图有点乱。

谢谢你的任何建议,我为糟糕的英语道歉:)

标签: c#visual-studioxamarinxamarin.forms

解决方案


您可以使用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");
    }
       
}

推荐阅读