本文介绍了Window.MakeKeyAndVisible()在iOS 13及更高版本的操作系统中未显示新窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My requirement is to open new UIWindow instead story board.Used below code to do this,

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

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

在iOS低于13的版本中运行良好,但在13或更高版本中无法正常运行.

It's working fine in iOS below 13 versions, but not in 13 and above.

已注意到iOS 13及更高版本是针对多屏幕的新更新,并引入了场景委托,因此需要将所有应用程序委托内容移至场景委托.但是在我的情况下,应用程序委托中没有代码,在启动屏幕故事板之后也没有打开此窗口.

Have noted that iOS 13 and above were new update for multi screen, and have introduced scene delegate, so need to move all app delegate stuffs to scene delegate. But in my case there is no code stuff in app delegate and opening this window after my launch screen story board.

真的需要在现有应用程序中添加场景委托吗?如果可以,如何在xamarin iOS中将场景委托正确地集成到现有应用程序中?

is it really required to add scene delegate in existing application? if so how to properly integrate scene delegate in existing application in xamarin iOS?

推荐答案

您可以按照以下步骤进行操作(在iOS 13之前)Xamarin.iOS项目支持 SceneDelegate :

You can follow bellow steps to make previous(Before iOS 13) Xamarin.iOS project supports SceneDelegate used :

首先,创建一个新的 Xamarin.iOS 项目,复制 AppDelegate.cs SceneDelegate.cs 文件到现有项目的根文件夹.( AppDelegate.cs 将替换现有的 AppDelegate.cs 文件,您可以移动 SceneDelegate.cs )

First , create a new Xamarin.iOS projecty , copy AppDelegate.cs and SceneDelegate.cs file to root folder of existing project .(AppDelegate.cs will replcae existing AppDelegate.cs file , you can movelogic code to SceneDelegate.cs)

第二,将跟踪代码添加到 Info.plist :

Second , Adding follow code to 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/zh-cn/xamarin/ios/platform/ios13/multi-window-ipad#project-configuration

More info can refer to this official document : https://docs.microsoft.com/en-us/xamarin/ios/platform/ios13/multi-window-ipad#project-configuration

在iOS 13上, Apple 使用 SceneDelegate.cs 管理窗口的生命周期.因此,您可以在中编写代码SceneDelegate.cs 如下:

From iOS 13 , Apple use SceneDelegate.cs to manage the lifecycle of window .Therefore, you can write code in SceneDelegate.cs as follow :

[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

Also can refer to this : https://github.com/xamarin/xamarin-macios/issues/7289

这篇关于Window.MakeKeyAndVisible()在iOS 13及更高版本的操作系统中未显示新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 06:31