所以标题说明了一切.我关注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)
DependencyService.Get<IOrientationHandler>().ForceLandscape();
Run Code Online (Sandbox Code Playgroud)
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)
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)
编辑 - 根据请求添加基本依赖项实现
public class BaseDependencyImplementation : Object
{
public Activity GetActivity()
{
var activity = (Activity)Forms.Context;
return activity;
}
}
Run Code Online (Sandbox Code Playgroud)
如果要为整个应用设置方向,可以在应用设置/清单文件中完成.
来自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即可
归档时间: |
|
查看次数: |
8209 次 |
最近记录: |