【发布时间】: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