适用于 iPad 的多个窗口Multiple windows for iPad

iOS 13 现在支持 iPad 上相同应用的并行窗口。iOS 13 now supports side-by-side windows for the same app on iPad. 这可以实现 windows 之间的新体验和拖放式交互。This enables new experiences and drag-and-drop interactions between windows. 本文档介绍了如何设置应用程序以支持此功能,并介绍了这些新功能。This document shows you how to setup your application to support this feature, and introduces these new features.

项目配置Project Configuration

若要为多个窗口配置你的项目info.plist ,请NSUserActivityTypes修改以声明你的应用程序将处理此类型的UIApplicationSceneManifest活动, UIApplicationSupportsMultipleScenes并为多个UISceneConfigurations窗口启用并将你的带有情节提要的场景。To configure your project for multiple windows, amend the info.plist to declare NSUserActivityTypes telling iOS your app will handle activities of this type, and UIApplicationSceneManifest to enable UIApplicationSupportsMultipleScenes for multiple windows and UISceneConfigurations to associate your scene with a storyboard.

<key>NSUserActivityTypes</key>
<array>
    <string>com.xamarin.Gallery.openDetail</string>
</array>
<key>UIApplicationSceneManifest</key>
<dict>
    <key>UIApplicationSupportsMultipleScenes</key>
    <true/>
    <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>

打开多个窗口Opening Multiple Windows

在 iPad 上打开并运行应用程序后,有几种方法可用于打开该应用程序的多个窗口,iOS 处理是默认的。With your app open and running on an iPad, there are a few ways to open multiple windows of that app that iOS handles be default.

  • 应用公开-点击停靠中的应用图标,以便在应用处于打开状态时进入此模式。App Expose - tap the app icon in the dock to enter this mode while your app is open.
  • 滑过-将应用图标从正在运行的应用的顶部上方拖动到浮动窗口。Slide Over - drag the app icon from the dock over the top of your running app to have a floating window.
  • 拆分屏幕-将应用图标从停靠拖动到 iPad 屏幕的边缘,以创建一个新的并排窗口。Split Screen - drag the app icon from the dock to the edge of the iPad screen to create a new side-by-side window.

您的应用程序中提供了用于进入多窗口模式的其他交互。Additional interactions to enter a multiple window mode are available from within your application.

从应用中拖动-在应用中使用拖动交互,就像在NSUserActivity前面的示例中拖动应用图标一样启动新的。Drag from App - use a drag interaction within your app to start a new NSUserActivity just like dragging your app icon in previous examples.

当使用拖放交互时,您可以创建一个NSUserActivity并关联要传递到您要求 iOS 打开的新窗口中的数据。When using a drag-and-drop interaction, you create an NSUserActivity and associate the data to be passed along to the new window you're asking iOS to open for you.

public UIDragItem [] GetItemsForBeginningDragSession (UICollectionView collectionView, IUIDragSession session, NSIndexPath indexPath)
{
    var selectedPhoto = GetPhoto (indexPath);

    var userActivity = selectedPhoto.OpenDetailUserActivity ();
    var itemProvider = new NSItemProvider (UIImage.FromFile (selectedPhoto.Name));
    itemProvider.RegisterObject (userActivity, NSItemProviderRepresentationVisibility.All);

    var dragItem = new UIDragItem (itemProvider) {
        LocalObject = selectedPhoto
    };

    return new [] { dragItem };
}

在上面的代码中, selectedPhoto模型对象具有返回调用OpenDetailUserActivity()NSUserActivity方法。In the code above, the selectedPhoto model object has a method to return an NSUserActivity called OpenDetailUserActivity(). 拖动笔势完成后, UIDragItemuserActivity通过NSItemProvider提供。When the drag gesture is complete, the UIDragItem with present the userActivity via the NSItemProvider.

显式操作-按钮或链接上的用户笔势提供了打开新窗口的功能。Explicit Actions - user gestures on buttons or links offer the ability to open a new window.

从中, RequestSceneSessionActivation UISceneSession UIApplication你可以通过调用来启动新的。From the UIApplication you can start a new UISceneSession by calling RequestSceneSessionActivation. 如果现有场景已经存在,则应使用该场景。If an existing scene already exists, you should use that. 默认情况下,将为您创建一个新的场景。By default a new scene will be created for you.

pubic void ItemSelected(UICollectionView collectionView, NSIndexPath indexPath)
{
    var userActivity = selectedPhoto.OpenDetailUserActivity ();

    UIApplication.SharedApplication.RequestSceneSessionActivation(
        sceneSession: null,
        userActivity: userActivity,
        options: null,
        errorHandler: null
    );
}

在此示例中, userActivity是传递RequestSceneSessionActivation给方法的唯一值,以便基于显式用户操作打开应用程序的新窗口ItemSelected ; 在本例中为的处理程序UICollectionViewIn this example, the userActivity is the only value passed to the RequestSceneSessionActivation method in order to open a new window of the application based on an explicit user action; in this case an ItemSelected handler of a UICollectionView.