首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS上Xamarin形式的特定于惯用语言的设备定向

iOS上Xamarin形式的特定于惯用语言的设备定向
EN

Stack Overflow用户
提问于 2016-11-11 19:31:27
回答 1查看 562关注 0票数 2

我想用平板电脑友好的用户界面扩展一个应用程序。由于手机用户界面在纵向方向上的表现要好得多,所以现有的应用程序被锁定在纵向;而平板电脑版本则没有这一限制。

这在Android上运行良好;主要活动决定在启动时是允许所有设备方向(在平板电脑上)还是将应用程序锁定到肖像(在手机上)。问题还在于在iOS上实现定向逻辑。

我知道Xamarin.Forms有一个bug,它可以防止iOS设备上的代码控制设备方向。我们提出了一个可能的解决方法:所有构建目标都是重复的(一个实例用于手机,另一个实例用于平板电脑),手机和平板电脑的构建分别上载到App Store。

这真的是实现特定于习惯用法的设备定向的最优雅的方法吗?还是有一种方法可以在不太麻烦的情况下绕过Xamarin.Forms bug?

EN

回答 1

Stack Overflow用户

发布于 2016-11-11 22:03:53

下面是它的魔力:

AppDelegate.cs

代码语言:javascript
复制
    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

代码语言:javascript
复制
    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();
            };
        }

    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40547092

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档