简体   繁体   English

如何在IOS的xamarin.forms中将单个ContentPage的方向从纵向更改为全部(横向/纵向)。

[英]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”设置为“横向/纵向”。

  1. In OnAppearing() method of the "CartoonDetail_demo2" page I have set one flag as true. 在“ CartoonDetail_demo2”页面的OnAppearing()方法中,我将一个标志设置为true。

  2. And OnDisappering() method of the "CartoonDetail_demo2" page I have set one flag as false. 在“ CartoonDetail_demo2”页面的OnDisappering()方法中,我将一个标志设置为false。

  3. 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 . 但是它不起作用,我的页面始终处于纵向方向

  1. 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; } }

  2. 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.

相关问题 iOS方向在特定屏幕上同时支持横向和纵向-并非所有屏幕 - iOS Orientation Support both Landscape and Portrait on specific screen - NOT ALL Screens 如何在iOS应用程序中同时管理横向和纵向方向? - How to manage both landscape & portrait orientation in iOS application? ios iframe只有在方向从纵向更改为横向后才能展开 - ios iframe unscrollable only after orientation change from portrait -> landscape 通过单击 iOS 中的按钮将设备方向从纵向更改为横向 - Change the device orientation from portrait to landscape on a button click in iOS iOS定向问题从横向到纵向 - iOS Orientation issue from landscape to portrait 方向改变从横向到纵向的工作都很奇怪 - orientation change working strangely from landscape to portrait 将Adwhirl广告方向从纵向更改为横向 - Change Adwhirl ad orientation from portrait to landscape 使用 Xamarin 表单在 iOS 16 中将横向更改为纵向后,屏幕不会更新其大小 - Screen is not updating its size after changing landscape to portrait orientation in iOS 16 using Xamarin forms 从横向更改为纵向时 Xamarin iOS 应用程序崩溃 - Xamarin iOS app crashes when changing from Landscape orientation to Portrait orientation iOS:纵向/横向网格 - iOS: grid in portrait / landscape orientation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM