Window.MakeKeyAndVisible() 在 iOS 13 及更高版本的操作系统中未显示新窗口



我的要求是打开新的UIWindow而不是故事板。 在下面的代码中用于执行此操作,

UIWindow window = new UIWindow(UIScreen.MainScreen.Bounds);
UIApplication.SharedApplication.KeyWindow.WindowLevel = UIWindowLevel.Normal + 0.1f;
window.RootViewController = new MainNavigationController();
window.MakeKeyAndVisible();

它在 13 版本以下的 iOS 中运行良好,但在 13 及以上版本中则不然。

已经注意到iOS 13及更高版本是多屏的新更新,并引入了场景委托,因此需要将所有应用程序委托的内容移动到场景委托。但就我而言,应用程序委托中没有代码内容,并且在启动屏幕故事板后打开此窗口。

是否真的需要在现有应用程序中添加场景委托? 如果是这样,如何在 Xamarin iOS 中的现有应用程序中正确集成场景委托?

是否真的需要在现有应用程序中添加场景委托? 如果是这样,如何在 Xamarin iOS 中的现有应用程序中正确集成场景委托?

您可以按照以下步骤使以前的(iOS 13之前(Xamarin.iOS项目支持SceneDelegate使用:

首先,创建一个新的Xamarin.iOS项目,将AppDelegate.cs 和 SceneDelegate.cs文件复制到现有项目的根文件夹中。(AppDelegate.cs会重新AppDelegate.cs文件,可以移动 逻辑代码到SceneDelegate.cs(

其次,在Info.plist中添加以下代码:

<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<false/>
<key>UISceneConfigurations</key>
<dict>
<key>UIWindowSceneSessionRoleApplication</key>
<array>
<dict>
<key>UISceneConfigurationName</key>
<string>Default Configuration</string>
<key>UISceneDelegateClassName</key>
<string>SceneDelegate</string>
<key>UISceneStoryboardFile</key>
<string>Main</string>
</dict>
</array>
</dict>
</dict>

更多信息可以参考此官方文档:https://learn.microsoft.com/en-us/xamarin/ios/platform/ios13/multi-window-ipad#project-configuration

从iOS 13开始,苹果使用SceneDelegate.cs来管理窗口的生命周期。因此,您可以按如下方式编写SceneDelegate.cs代码:

[Export ("scene:willConnectToSession:options:")]
public void WillConnect (UIScene scene, UISceneSession session, UISceneConnectionOptions connectionOptions)
{
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see UIApplicationDelegate `GetConfiguration` instead).
UIWindowScene windowScene = new UIWindowScene(session, connectionOptions);
var storyboard = UIStoryboard.FromName("Main", null);
var registerController = storyboard.InstantiateViewController("ViewControllerSecond") as ViewControllerSecond;
this.Window = new UIWindow(windowScene);
this.Window.RootViewController = registerController;
this.Window.MakeKeyAndVisible();
}

也可以参考这个: https://github.com/xamarin/xamarin-macios/issues/7289

最新更新