【发布时间】:2015-09-21 06:34:41
【问题描述】:
我正在尝试使用此处详述的 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 文档将其列为“受保护集”,编译器也是如此:
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'
但是,如果我在表单代码(使用结果的地方)中设置它,那很好(尽管处理程序似乎从未真正被调用过)。有谁知道为什么会这样?
【问题讨论】:
标签: android xamarin xamarin.forms screen-orientation xlabs