gpt4 book ai didi

android - 如何处理 Xamarin.Forms/XLabs 中的屏幕旋转/方向?

转载 作者:行者123 更新时间:2023-11-29 01:28:17 28 4
gpt4 key购买 nike

我正在尝试使用此处详述的 XLabs 方法确定屏幕何时旋转(在 Android 中)How to handle screen rotation/orientation in Xamarin Forms?我遇到了麻烦。

我覆盖了 MainActivity 中的 OnConfigurationChanged 方法

        public override void OnConfigurationChanged (Android.Content.Res.Configuration newConfig)
{
base.OnConfigurationChanged (newConfig);

var xapp = Resolver.Resolve<IXFormsApp> ();
if (xapp == null)
return;

switch (newConfig.Orientation) {
case Android.Content.Res.Orientation.Landscape:
xapp.Orientation = XLabs.Enums.Orientation.Landscape;
break;
case Android.Content.Res.Orientation.Portrait:
//xapp.Orientation = XLabs.Enums.Orientation.Portrait;
break;
default:
break;
}
}

我在 IXFormsApp 中的 Orientation 变量(即 xapp.Orientation)上遇到了问题。 XLabs 文档将其列为“ protected 集”,编译器也是如此:

MainActivity.cs(109,5,109,21): error CS0200: Property or indexer 'XLabs.Platform.Mvvm.IXFormsApp.Orientation' cannot be assigned to -- it is read only

它不会自动设置(当我检查它的使用位置时,它总是设置为“无”),所以我想知道如何使用它,实际上,如何使用 XLabs/IXFormsApp 确定旋转?

在相关说明中,我还尝试设置 Rotation 处理程序(不知道为什么,但我想我会试一试),但结果不正常。

            xapp.Rotation += (sender, args) =>
{
switch (args.Value)
{
case XLabs.Enums.Orientation.Landscape:
//xapp.Orientation = XLabs.Enums.Orientation.Landscape;
...
break;
case XLabs.Enums.Orientation.Portrait:
...
break;
default:
break;
}
};

如果我在 Android 代码中尝试,我会收到以下错误:

MainActivity.cs(60,4,60,22): error CS0019: Operator '+=' cannot be applied to operands of type 'System.EventHandler<XLabs.EventArgs<XLabs.Enums.Orientation>>' and 'lambda expression'

但是,如果我在表单代码(使用结果的地方)中设置它,就没问题(尽管处理程序似乎从未真正被调用过)。有谁知道为什么会这样?

最佳答案

我过去使用过两种不同的解决方案。


第一种方法是创建一个 PageBase 类,我的所有页面都继承自该类,而 PageBase 继承自常规页面。

我的PageBase 有两个抽象方法(因此它的子类必须填写),它们是UpdateLandscape 和UpdatePortait。 children 将填写这些方法,以根据页面是以横向还是纵向模式布局来布局页面。

正如丹尼尔所说,页面有一个方法 OnSizeAllocated。我让 PageBase 覆盖它并相应地调用 UpdateLandscape 和 UpdatePortait。

如果像您所说的那样,您只是想检查它何时旋转,那么上面的方法就可以正常工作,因为每当您旋转手机时都会为页面调用 OnSizeAllocated。


如果您因为希望代码能够随时检查而正在检查横向与纵向,那么下面的第二个解决方案也适用。

我解决的第二种方法是使用依赖服务来填充一个 IDeviceInfo 接口(interface),并通过检查 DeviceInfo.IsPortait() 是 true 还是 false 来编写所有动态的东西(这样我也让 DeviceInfo 有一个 Width 和高度,因此我可以随时请求屏幕尺寸)。

在 Android 上,我这样填写我的 Android 代码:

[assembly: Dependency (typeof(Namespace.DeviceInfoProvider))]
namespace Namespace
{
public class DeviceInfoProvider : IDeviceInfoProvider
{
public bool IsPortait () { return DeviceInfoManager.Width < DeviceInfoManager.Height; }
public int GetWidth () { return DeviceInfoManager.Width; }
public int GetHeight () { return DeviceInfoManager.Height; }
}

public static class DeviceInfoManager
{
public static MainActivity MainActivity { get; set; }
public static int Width { get { return MainActivity.GetWidth (); } }
public static int Height { get { return MainActivity.GetHeight (); } }
}
}

然后在 MainActivity 中我给了它这些方法:

public int GetWidth() {
return (int)(Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density);
}

public int GetHeight() {
return (int)(Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density);
}

而在iOS端,我是这样填写的:

[assembly: Dependency (typeof(Namespace.DeviceInfoProvider))]
namespace Namespace {

public class DeviceInfoProvider : IDeviceInfoProvider {
public bool IsPortait() { return UIScreen.MainScreen.Bounds.Width < UIScreen.MainScreen.Bounds.Height; }

public int GetWidth() { return (int)UIScreen.MainScreen.Bounds.Width; }

public int GetHeight() { return (int)UIScreen.MainScreen.Bounds.Height; }
}
}

就个人而言,我更喜欢用第二种方式编写它并让它检查“如果我们处于纵向模式,这里是不同之处”。这样人像和风景没有区别的东西只需要写一次,只有不同的地方写两次。

关于android - 如何处理 Xamarin.Forms/XLabs 中的屏幕旋转/方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32642732/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com