【问题标题】:Xamarin.Forms - change device orientation based on device resolutionXamarin.Forms - 根据设备分辨率更改设备方向
【发布时间】:2020-07-12 12:55:09
【问题描述】:

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

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

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

【问题讨论】:

  • 你的目标是ios和android吗?
  • 是的,有机会吗?
  • 嗨,marek,你能通过这个appliedcodelog.com/2017/05/…
  • @marek 嗨,你解决了吗?如果回答有帮助,记得在有空的时候标记一下:-)

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


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

【讨论】:

  • 如果设备只是手机,我认为他需要这样做。
  • @junior-jiang-msft 我只需要它用于屏幕尺寸较小的手机或设备。在平板电脑上一切正常。
  • @marek Okey,您可以使用DeviceInfo.Idiom 来检查设备是手机还是平板电脑。我会更新答案。
【解决方案2】:

您可以在这几个页面上强制设置方向

// 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 中查看更多有关设备类的信息

【讨论】:

  • @marek 嗨,如果回答有帮助,记得在有时间的时候标记一下。 :-)
猜你喜欢
  • 2021-12-18
  • 1970-01-01
  • 2012-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-28
  • 1970-01-01
相关资源
最近更新 更多