iOS上的Xamarin表单如何设置页面的屏幕方向? [英] Xamarin Forms on iOS How To Set Screen Orientation for Page?

查看:105
本文介绍了iOS上的Xamarin表单如何设置页面的屏幕方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以标题说明了一切.我在这一点上与iOS有关.我试图为我的基本页面"LandscapeContentPage"起诉一个自定义渲染器,希望它将强制将其渲染为横向.我一直没有成功.

So the title says it all. Im concerned at this point with iOS. I have tried to sue a custom renderer for my base page "LandscapeContentPage" which would hopefully force it to be rendered as landscape. I have been unsuccessful.

我尝试使用一种黑客手段,该技巧在ViewDidAppear中您发现一个呈现伪造" UIViewController的地方,该UIViewController覆盖了GetSupportedInterfaceOrientations以仅返回Landscape.这种作品.肉看起来像这样:

I attempted to use a hack I found where in ViewDidAppear you present a "fake" UIViewController that overrides the GetSupportedInterfaceOrientations to only return Landscape. This sort of works. The meat looks like this:

var c = new LandscapeViewController();
c.View.BackgroundColor = UIColor.Cyan;

await PresentViewControllerAsync(c, false);
await DismissViewControllerAsync(false);

如果在"Dismiss"行上设置了一个断点,则可以在模拟器中看到视图确实变为横向,则模拟器窗口实际上会自动旋转.当我继续操作时,要么引发视图转换错误,要么再次触发ViewDidAppear,并且原始页面以纵向显示.

If I set a breakpoint at the Dismiss line I can see in the simulator that the view did change to landscape, the simulator window actually rotates on its own. When I continue either an error is thrown about view transitions or the ViewDidAppear fires again and the original page is displayed in portrait orientation.

因此,我在这一点上或多或少地迷路了.我尝试了很多不同的事情,但成功却为零.我不知道为什么没有为此使用的现成机制.如果没有方向处理,那么昂贵的工具集/框架似乎是不完整的.

So, I'm more or less lost at this point. I have tried so many different things and had zero success. I can't figure out why there isn't an out of the box mechanism for this. Such an expensive toolset/framework seems incomplete without orientation handling.

谢谢.

推荐答案

我为此使用了依赖项服务.

I used a dependency service for this.

https://developer.xamarin.com/guides/cross-platform/xamarin-forms/dependency-service/

public interface IOrientationHandler
{
    void ForceLandscape();

    void ForcePortrait();
}

从窗体调用界面

DependencyService.Get<IOrientationHandler>().ForceLandscape();

IOS实施

public class OrientationHandlerImplementation : IOrientationHandler
    {
        public void ForceLandscape()
        {
            UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeLeft), new NSString("orientation"));
        }

        public void ForcePortrait()
        {
            UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
        }
    }

Android实现

public class OrientationHandler : BaseDependencyImplementation, IOrientationHandler
{
    public void ForceLandscape()
    {
        GetActivity().RequestedOrientation = ScreenOrientation.Landscape;
    }

    public void ForcePortrait()
    {
        GetActivity().RequestedOrientation = ScreenOrientation.Portrait;
    }
}

编辑-根据请求添加了基本依赖实现

Edit - Added Base Dependency Implementation as per request

 public class BaseDependencyImplementation : Object
    {
        public Activity GetActivity()
        {
            var activity = (Activity)Forms.Context;
            return activity;
        }
    }

如果您想为整个应用设置方向,可以在应用设置/清单文件中完成.

If you want to set orientation for the whole app it can be done in the app settings / manifest file.

这篇关于iOS上的Xamarin表单如何设置页面的屏幕方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆