【问题标题】:Window.MakeKeyAndVisible() is not showing new window in iOS 13 and above OSWindow.MakeKeyAndVisible() 未在 iOS 13 及更高版本操作系统中显示新窗口
【发布时间】:2020-05-13 20:05:26
【问题描述】:

我的要求是打开新的 UIWindow 而不是故事板。 使用下面的代码来做到这一点,

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

                window.RootViewController = new MainNavigationController();
                window.MakeKeyAndVisible();

在 iOS 13 以下版本可以正常工作,但在 13 及以上版本中不行。

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

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

【问题讨论】:

    标签: c# swift xamarin xamarin.ios ios13


    【解决方案1】:

    是否真的需要在现有应用程序中添加场景委托?如果是这样,如何在 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://docs.microsoft.com/en-us/xamarin/ios/platform/ios13/multi-window-ipad#project-configuration

    从 iOS 13 开始,Apple 使用SceneDelegate.cs 来管理 window 的生命周期。因此,您可以在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

    【讨论】:

    • 谢谢@Junior,但在我的场景中,仅在启动屏幕后打开新的 UIWindow()。将 UIApplicationSupportsMultipleScenes 值更改为 false 对我有用,已在参考链接中注明,再次感谢。
    • @Naveenraj Okey,很高兴这有效。这是一个示例代码,您可以根据需要修改配置。关键点是UISceneConfigurations,它将在项目中使用SceneDelegate。如果回答有帮助,记得在有时间的时候标记一下:-)
    猜你喜欢
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 2020-01-06
    • 2014-06-25
    相关资源
    最近更新 更多