iOS上的Xamarin表单如何设置页面的屏幕方向?

jmi*_*has 8 ios xamarin.forms

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

我试图使用一个hack,我在ViewDidAppear中找到了一个"假的"UIViewController,它覆盖了GetSupportedInterfaceOrientations,只返回Landscape.这种作品.肉看起来像这样:

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

await PresentViewControllerAsync(c, false);
await DismissViewControllerAsync(false);
Run Code Online (Sandbox Code Playgroud)

如果我在Dismiss行设置断点,我可以在模拟器中看到视图确实变为横向,模拟器窗口实际上是自行旋转的.当我继续时,会抛出有关视图过渡的错误或ViewDidAppear再次触发,并且原始页面以纵向显示.

所以,我现在或多或少都迷失了.我尝试了很多不同的事情并没有成功.我无法弄清楚为什么没有开箱即用的机制.如果没有方向处理,这种昂贵的工具集/框架似乎是不完整

谢谢.

Cha*_*uys 17

我为此使用了依赖服务.

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

接口

public interface IOrientationHandler
{
    void ForceLandscape();

    void ForcePortrait();
}
Run Code Online (Sandbox Code Playgroud)

从Forms调用接口

DependencyService.Get<IOrientationHandler>().ForceLandscape();
Run Code Online (Sandbox Code Playgroud)

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"));
        }
    }
Run Code Online (Sandbox Code Playgroud)

Android实施

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

    public void ForcePortrait()
    {
        GetActivity().RequestedOrientation = ScreenOrientation.Portrait;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

BaseDependency实现

 public class BaseDependencyImplementation : Object
    {
        public Activity GetActivity()
        {
            var activity = (Activity)Forms.Context;
            return activity;
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

  • 今天尝试了此操作,但我仍然能够在iOS中进入横向模式。 (2认同)

Mur*_*ack 9

来自Chad Bonthuys的方法就像一个魅力.我有两个小小的补充.

Android:如果您旋转并离开视图,其他视图现在也将处于请求的方向.打电话吧

GetActivity().RequestedOrientation = ScreenOrientation.Unspecified;
Run Code Online (Sandbox Code Playgroud)

在离开.这将允许其他视图再次旋转.

iOS:视图将被旋转,但用户仍然可以将其旋转回来.如果覆盖,可以锁定它

GetSupportedInterfaceOrientations()

AppDelegate.cs

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations (UIApplication application, [Transient] UIWindow forWindow)
{
    if (Xamarin.Forms.Application.Current == null || Xamarin.Forms.Application.Current.MainPage == null)
    {
        return UIInterfaceOrientationMask.All;
    }

    var mainPage = Xamarin.Forms.Application.Current.MainPage;

    if (mainPage is YOUR_VIEW||
            (mainPage is NavigationPage && ((NavigationPage)mainPage).CurrentPage is YOUR_VIEW) ||
            (mainPage.Navigation != null && mainPage.Navigation.ModalStack.LastOrDefault () is YOUR_VIEW))
    {
        return UIInterfaceOrientationMask.Landscape;
    }

    return UIInterfaceOrientationMask.All;
}
Run Code Online (Sandbox Code Playgroud)

只需将YOUR_VIEW替换为您要锁定的ContentPage即可


Fre*_*ger 0

如果您只想强制一页或整个应用程序横向,您不写吗?
我想您已经知道,您可以在项目设置中强制整个应用程序的方向(例如仅横向)?