[英]How to change orientation from Portrait to All (Landscape/Portrait both) for the single ContentPage in xamarin.forms for IOS?
I want to change the orientation of the single page. 我想更改单个页面的方向。 My whole application should be run in portrait but I want to make one page as Landscape/portrait (both). 我的整个应用程序都应该以纵向运行,但是我想将页面作为“横向/纵向”(两者都)。
I have taken reference from below links 我从下面的链接中获取了参考
http://www.appliedcodelog.com/2017/05/force-landscape-or-portrait-for-single.html http://www.appliedcodelog.com/2017/05/force-landscape-or-portrait-for-single.html
Code is working fine in Android but not in IOS. 代码在Android上运行良好,但在IOS中运行不正常。
AppDelegate.cs AppDelegate.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;
}
But this condition is never going to be true in any case. 但是这种情况在任何情况下都不会成立。 Eventually I can not get the page from navigation stack. 最终我无法从导航堆栈中获取页面。
So, I use easy way to do the same thing, 所以,我用简单的方法做同样的事情,
I want to make "CartoonDetail_demo2" this page as Landscape/Portrait. 我要在此页面上将“ CartoonDetail_demo2”设置为“横向/纵向”。
In OnAppearing() method of the "CartoonDetail_demo2" page I have set one flag as true. 在“ CartoonDetail_demo2”页面的OnAppearing()方法中,我将一个标志设置为true。
And OnDisappering() method of the "CartoonDetail_demo2" page I have set one flag as false. 在“ CartoonDetail_demo2”页面的OnDisappering()方法中,我将一个标志设置为false。
use this flag in AppDelegate.cs. 在AppDelegate.cs中使用此标志。
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, [Transient] UIWindow forWindow) { if (AppConstants.IsOrientationAll) { return UIInterfaceOrientationMask.All; } return UIInterfaceOrientationMask.Portrait; }
Now, Condition become true whenever I go to the page "CartoonDetail_demo2". 现在,每当我转到页面“ CartoonDetail_demo2”时,条件就变为真。
But It is not working and my page is always in Portrait Orientation . 但是它不起作用,我的页面始终处于纵向方向 。
When I simply call this method without any condition then whole application is going in "All" (Landscape/Portrait) orientation. 当我简单地无条件调用此方法时,整个应用程序将处于“全部”(横向/纵向)方向。
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, [Transient] UIWindow forWindow) { return UIInterfaceOrientationMask.All; 公共重写UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application,[Transient] UIWindow forWindow){return UIInterfaceOrientationMask.All; } }
By default, My whole application in Portrait Orientation. 默认情况下,“我的整个应用程序”为“纵向方向”。
I have also tried few other demo but none of are going to working 我也尝试了其他几个演示,但都没有用
https://xamarindevelopervietnam.wordpress.com/2017/05/23/how-to-rotate-specific-page-in-xamarin-forms/ https://xamarindevelopervietnam.wordpress.com/2017/05/23/how-to-rotate-specific-page-in-xamarin-forms/
should I use the plugin Plugin.DeviceOrientation? 我应该使用插件Plugin.DeviceOrientation吗?
in AppDelegate.cs 在AppDelegate.cs中
public bool allowRotation;
And rewrite the method 并重写方法
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, [Transient] UIWindow forWindow)
{
if(allowRotation==true)
{
return UIInterfaceOrientationMask.Landscape;
}
else
{
return UIInterfaceOrientationMask.Portrait;
}
}
Then you can call the following method in dependency service 然后可以在依赖服务中调用以下方法
in iOS 在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"));
}
}
in Android 在Android中
public class OrientationService : IOrientationService
{
public void Landscape()
{
((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Landscape;
}
public void Portrait()
{
((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Portrait;
}
}
For more detail you can refer this similar issue . 有关更多详细信息,您可以参考此类似问题 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.