【问题标题】:How to change orientation of some pages in iOS 16 in Xamarin Forms?如何在 Xamarin Forms 中更改 iOS 16 中某些页面的方向?
【发布时间】:2022-12-15 14:28:37
【问题描述】:

我想在 Xamarin Forms 中更改我的应用程序中某些页面的方向。

我正在使用 Visual Studio 2019 for Mac 和 Xcode 12.4 版

我用过 DependencyService。它在 iOS 15 之前工作正常,但无法在 iOS 16 中旋转。

我正在共享示例代码以供参考。

AppDelegate.cs

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, [Transient] UIWindow forWindow)
    {
        if (App.IsAllowRotation)
        {
            return UIInterfaceOrientationMask.Landscape;
        }

        else
        {
            return UIInterfaceOrientationMask.Portrait;
        }
    }

iOS 中的服务

public class OrientationService : IOrientationHandler
{

    public void Landscape()
    {
        UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeRight), new NSString("orientation"));
    }

    public void Portrait()
    {
        UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
    }
}

直到 iOS 15 它工作正常。对于 iOS 16,我正在尝试通过添加以下代码来更新服务:

public class OrientationService : IOrientationHandler
{

    public void Landscape()
    {

        if (UIDevice.CurrentDevice.CheckSystemVersion(16, 0))
        {
            
            var windowScene = (UIApplication.SharedApplication.ConnectedScenes.ToArray()[0] as UIWindowScene);
           
            if (windowScene != null)
            {
                var nav = UIApplication.SharedApplication.KeyWindow?.RootViewController;
                if (nav != null)
                {                        
                    nav.SetNeedsUpdateOfSupportedInterfaceOrientations();

                    windowScene.RequestGeometryUpdate(
                        new UIWindowSceneGeometryPreferencesIOS(UIInterfaceOrientationMask.Portrait),
                        error => { }
                    );
                }
            }
        }
        else
        {
            UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
        }
    }

    public void Portrait()
    {
        if (UIDevice.CurrentDevice.CheckSystemVersion(16, 0))
        {

            var windowScene = (UIApplication.SharedApplication.ConnectedScenes.ToArray()[0] as UIWindowScene);

            if (windowScene != null)
            {
                var nav = UIApplication.SharedApplication.KeyWindow?.RootViewController;
                if (nav != null)
                {
                    // Tell the os that we changed orientations so it knows to call GetSupportedInterfaceOrientations again

                    nav.SetNeedsUpdateOfSupportedInterfaceOrientations();

                    windowScene.RequestGeometryUpdate(
                        new UIWindowSceneGeometryPreferencesIOS(UIInterfaceOrientationMask.Portrait),
                        error => { }
                    );
                }
            }
        }
        else
        {
            UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
        }
    }
 }

我收到方法错误 - SetNeedsUpdateOfSupportedInterfaceOrientations(), 请求几何更新, UIWindowSceneGeometryPreferencesIOS

我可以知道需要任何命名空间吗?

Xamarin Forms iOS 应用中的 Service 和 AppDelegate 中的 iOS 16 需要做哪些更改?

【问题讨论】:

  • 错误消息的确切文本是什么?
  • @ToolmakerSteve 1. Services/OrientationService.cs(29,29):错误 CS1061:“UIViewController”不包含“SetNeedsUpdateOfSupportedInterfaceOrientations”的定义,并且找不到接受“UIViewController”类型的第一个参数的可访问扩展方法“SetNeedsUpdateOfSupportedInterfaceOrientations” (您是否缺少 using 指令或程序集引用?)
  • 2. Services/OrientationService.cs(33,33):错误 CS0246:找不到类型或命名空间名称“UIWindowSceneGeometryPreferencesIOS”(是否缺少 using 指令或程序集引用?) 3.Services/OrientationService.cs(37 ,37): 错误 CS1061: 'UIWindowScene' 不包含 'RequestGeometryUpdate' 的定义,并且找不到接受类型为 'UIWindowScene' 的第一个参数的可访问扩展方法 'RequestGeometryUpdate'(您是否缺少 using 指令或程序集引用?)

标签: ios xamarin.forms xamarin.ios screen-rotation


【解决方案1】:

SetNeedsUpdateOfSupportedInterfaceOrientations()RequestGeometryUpdateUIWindowSceneGeometryPreferencesIOS这几个方法是iOS16 Release Note中UIKit新添加的方法。我在XCode14.1和VS2022都试过了,都没有报错。根据XCode release version,XCode14.0开始支持iOS16。建议您将XCode版本更新到最新版本再试。

【讨论】:

  • 谢谢。根据您的评论,我将更新 Xcode 版本并尝试。
  • 好的,等你的更新。
  • 如果我在装有 iOS 12.5 的 iPhone 上运行应用程序,那么我还必须安装 Xcode 14 和 VS 2022 吗?
  • 如果您需要为 iOS16 编写程序,您可以更新您的版本以使用一些更新的方法和控件。
  • 好的。我会更新。
猜你喜欢
  • 2022-10-08
  • 2019-07-24
  • 1970-01-01
  • 2023-01-30
  • 2021-02-24
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 2019-06-10
相关资源
最近更新 更多