如何将 IOS 的 xamarin.forms 中单个内容页面的方向从纵向更改为全部(横向/纵向两者)?



我想更改单个页面的方向。我的整个应用程序应该以纵向运行,但我想将一个页面作为横向/纵向(两者(。

我从下面的链接中获取了参考

http://www.appliedcodelog.com/2017/05/force-landscape-or-portrait-for-single.html

代码在Android中运行良好,但在IOS中无法正常工作。

应用代表.cs

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, [Transient] UIWindow forWindow)
{
var mainPage = Xamarin.Forms.Application.Current.MainPage;
if (mainPage is CartoonDetail_demo2 ||
(mainPage is NavigationPage && ((NavigationPage)mainPage).CurrentPage is CartoonDetail_demo2) ||
(mainPage.Navigation != null && mainPage.Navigation.NavigationStack.LastOrDefault() is CartoonDetail_demo2))
{
return UIInterfaceOrientationMask.All;
}
return UIInterfaceOrientationMask.Portrait;
}

但无论如何,这种情况永远不会是真的。最终我无法从导航堆栈中获取页面。

所以,我用简单的方法来做同样的事情,

我想将此页面"CartoonDetail_demo2"为横向/纵向。

  1. 在"CartoonDetail_demo2"页面的OnAppearing((方法中,我设置了一个标志为true。

  2. 和"CartoonDetail_demo2"页面的 OnDisappering(( 方法,我已经将一个标志设置为 false。

  3. 在 AppDelegate.cs 中使用此标志。

public override UIInterfaceOrientationMask 
GetSupportedInterfaceOrientations(UIApplication application, [Transient] 
UIWindow forWindow)
{
if (AppConstants.IsOrientationAll)
{
return UIInterfaceOrientationMask.All;
}
return UIInterfaceOrientationMask.Portrait;
}

现在,每当我转到"CartoonDetail_demo2"页面时,条件就会变为真。

但它不起作用,我的页面始终处于纵向方向

  1. 当我在没有任何条件的情况下简单地调用此方法时,整个应用程序将朝着"全部"(横向/纵向(方向进行。

    public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, [transient] UIWindow forWindow( { 返回 UIInterfaceOrientationMask.All; }

  2. 默认情况下,我的整个应用程序为纵向。

我也尝试了其他一些演示,但没有一个可以正常工作

https://xamarindevelopervietnam.wordpress.com/2017/05/23/how-to-rotate-specific-page-in-xamarin-forms/

我应该使用插件插件.设备方向吗?

in AppDelegate.cs

public bool allowRotation; 

并重写方法

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, [Transient] UIWindow forWindow)
{
if(allowRotation==true)
{
return UIInterfaceOrientationMask.Landscape;
}
else
{
return UIInterfaceOrientationMask.Portrait;
}
} 

然后可以在依赖服务中调用以下方法

在 iOS 中

public class OrientationService : IOrientationService
{
public void Landscape()
{
AppDelegate appDelegate = (AppDelegate)UIApplication.SharedApplication.Delegate;
appDelegate.allowRotation = true;
UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeLeft), new NSString("orientation"));   
}
public void Portrait()
{
AppDelegate appDelegate = (AppDelegate)UIApplication.SharedApplication.Delegate;
appDelegate.allowRotation = true;
UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
}
}

在安卓中

public class OrientationService : IOrientationService
{
public void Landscape()
{
((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Landscape;
}
public void Portrait()
{
((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Portrait;
}
}

有关更多详细信息,您可以参考此类似问题。

最新更新