我希望使用平板电脑友好的用户界面扩展应用。由于手机UI在纵向方向上工作得更好,现有应用程序被锁定为纵向;平板电脑版本没有这个限制。
这适用于Android;主要活动决定是否允许所有设备方向(在平板电脑上)或将应用程序锁定为纵向(在手机上)。问题在于在iOS上实现方向逻辑。
我知道Xamarin.Forms有一个bug,可以阻止从iOS设备上的代码控制设备方向。我们提出了一个可能的解决方法:所有构建目标都是重复的(一个用于手机,另一个用于平板电脑),手机和平板电脑构建分别上传到App Store。
这真的是实现成语特定设备方向的最优雅方式,还是有办法解决Xamarin.Forms错误而没有太多麻烦?
答案 0 :(得分:0)
这是魔术:
<强> AppDelegate.cs 强>
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, UIWindow forWindow)
{
//Tablet orientation adjustment
if (Device.Idiom == TargetIdiom.Tablet)
return UIInterfaceOrientationMask.Landscape;
else
return UIInterfaceOrientationMask.Portrait;
}
然后为ContentPage
创建 CustomRenderer 并覆盖OnElementChangedMethod
:
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
Page _page = Element as Page;
_page.Appearing += (s, ea) =>
{
UIInterfaceOrientation orientain = Device.Idiom == TargetIdiom.Phone ? UIInterfaceOrientation.Portrait : UIInterfaceOrientation.LandscapeLeft;
NSNumber number = NSNumber.FromInt32((int)orientain);
UIDevice.CurrentDevice.SetValueForKey(number, new NSString("orientation"));
UIViewController.AttemptRotationToDeviceOrientation();
};
}
}