【问题标题】:How to change the orientation of view in Xamarin.Forms without rotating the device?如何在不旋转设备的情况下更改 Xamarin.Forms 中的视图方向?
【发布时间】:2018-07-10 14:46:12
【问题描述】:

我需要在不旋转设备的情况下更改 Xamarin.Forms 中视图的方向,以便视图应该像在横向模式下一样布局。

有什么方法可以实现吗?在 Xamarin.Forms 中还是在 Xamarin.iOS 中?

【问题讨论】:

    标签: xamarin xamarin.forms xamarin.ios


    【解决方案1】:

    对于依赖服务中的iOS,编写以下方法

    public void ChangeLandscapeOrientation()
     {
         UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeLeft), new NSString("orientation"));
         UINavigationController.AttemptRotationToDeviceOrientation();
     }
    

    在需要时调用 ChangeLandscapeOrientation 方法。

    AppDelegate.cs

    public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, UIWindow forWindow)
    {
    
        var mainPage = Xamarin.Forms.Application.Current.MainPage;
    
        if (mainPage is YourPage || (mainPage is NavigationPage &&
                                       ((NavigationPage)mainPage).CurrentPage is YourPage) || (mainPage.Navigation != null &&
                                                                                                 mainPage.Navigation.ModalStack.LastOrDefault() is YourPage))
        {
            return UIInterfaceOrientationMask.Landscape;
        }
        else
        {
            return UIInterfaceOrientationMask.Portrait;
        }
    }
    

    希望对你有帮助!

    【讨论】:

    • 此代码适用于模拟器,但不适用于设备。有什么想法吗?
    • @Divakar 我还添加了要在 AppDelegate 中编写的代码。试试看!
    • 在应用程序加载和手动更改方向时,该覆盖甚至都没有命中
    • 你是说断点吧?是的,我已经把断点放在那里并检查了。未调用覆盖。
    • @Divakar 这是一种默认的覆盖方法,即使没有定位您的设备或模拟器也会被调用。你能检查一下你的 info.plist 支持的界面方向吗?
    猜你喜欢
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多