iOS上的Xamarin表单如何设置页面的屏幕方向?
所以标题说明了一切。 我关注iOS的这一点。 我试图为我的基页“LandscapeContentPage”起诉一个自定义渲染器,希望强制它渲染为横向。 我没有成功。
我试图使用一个hack,我在ViewDidAppear中找到了一个“假的”UIViewController,它覆盖了GetSupportedInterfaceOrientations,只返回Landscape。 这种作品。 肉看起来像这样:
var c = new LandscapeViewController(); c.View.BackgroundColor = UIColor.Cyan; await PresentViewControllerAsync(c, false); await DismissViewControllerAsync(false);
如果我在Dismiss行设置断点,我可以在模拟器中看到视图确实变为横向,模拟器窗口实际上是自行旋转的。 当我继续时,会抛出有关视图过渡的错误或ViewDidAppear再次触发,并且原始页面以纵向显示。
所以,我现在或多或少都迷失了。 我尝试了很多不同的事情并没有成功。 我无法弄清楚为什么没有开箱即用的机制。 如果没有方向处理,这种昂贵的工具集/框架似乎是不完整
谢谢。
我为此使用了依赖服务。
https://developer.xamarin.com/guides/cross-platform/xamarin-forms/dependency-service/
接口
public interface IOrientationHandler { void ForceLandscape(); void ForcePortrait(); }
从Forms调用接口
DependencyService.Get().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; } }
编辑 – 根据请求添加基本依赖项实现
BaseDependency实现
public class BaseDependencyImplementation : Object { public Activity GetActivity() { var activity = (Activity)Forms.Context; return activity; } }
如果要为整个应用设置方向,可以在应用设置/清单文件中完成。
来自Chad Bonthuys的方法就像一个魅力。 我有两个小小的补充。
Android:如果您旋转并离开视图,其他视图现在也将处于请求的方向。 打电话吧
GetActivity().RequestedOrientation = ScreenOrientation.Unspecified;
在离开。 这将允许其他视图再次旋转。
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; }
只需将YOUR_VIEW替换为您要锁定的ContentPage即可
你不写,如果你只想强制横向为一页或整个应用程序?
我想你已经知道,你可以在项目设置中强制整个应用程序的方向(例如只有横向)?
以下解决方案适用于依赖服务。
假设我们想要有三个页面,而只有第二页我们需要横向定位,下面的代码对我有用。
将以下方法添加到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.Portrait; } var mainPage = Xamarin.Forms.Application.Current.MainPage; if (mainPage is SecondPage || (mainPage is NavigationPage && ((NavigationPage)mainPage).CurrentPage is SecondPage) || (mainPage.Navigation != null && mainPage.Navigation.ModalStack.LastOrDefault() is SecondPage)) { return UIInterfaceOrientationMask.Landscape; } return UIInterfaceOrientationMask.Portrait; }
添加自定义渲染器以更改方向。
[assembly: ExportRenderer(typeof(MainPage), typeof(MainPagePageRenderer))] namespace App2.iOS { public class MainPagePageRenderer : PageRenderer { public override void ViewWillDisappear(bool animated) { base.ViewWillDisappear(animated); UIDevice.CurrentDevice.SetValueForKey(NSNumber.FromNInt((int)(UIInterfaceOrientation.LandscapeLeft)), new NSString("orientation")); } } }
在这里,我们在应用程序中有三页MainPage,SecondPage和ThirdPage,我们正在尝试将横向定位到SecondPage。 我们需要为MainPage创建一个自定义渲染器,因为我们需要在转到SecondPage时更改方向。 我们在AppDelegate中为SecondPage修复了方向,因此即使我们旋转手机也不会改变方向。
现在,纵向方向被锁定为MainPage和ThirdPage以及LandscapePage for SecondPage。
我们可能需要为每个页面创建尽可能多的自定义渲染器以修复方向。
谢谢。