使用全像攝影 DirectX 應用程式中的 XAMLUsing XAML with holographic DirectX apps

注意

本文與舊版 WinRT 原生 Api 相關。This article relates to the legacy WinRT native APIs. 針對新的原生應用程式專案,建議使用 OPENXR APIFor new native app projects, we recommend using the OpenXR API.

本主題說明在 DirectX 應用程式中切換 2D XAML 視圖與沉浸式視圖 之間的影響,以及如何有效率地使用 XAML 視圖和沉浸式視圖。This topic explains the impact of switching between 2D XAML views and immersive views in your DirectX app, and how to make efficient use of both a XAML view and immersive view.

XAML 視圖切換總覽XAML view switching overview

在 HoloLens 上,可在稍後顯示 2D XAML 視圖的沉浸式應用程式,必須先初始化該 XAML 視圖,並立即從該處切換到沉浸式視圖。On HoloLens, an immersive app that may later display a 2D XAML view needs to initialize that XAML view first and immediately switch to an immersive view from there. XAML 會先載入,您的應用程式才能進行任何動作,這會增加您的啟動時間。XAML will load before your app can do anything, which adds a small increase to your startup time. XAML 會在應用程式進程維持在背景時,繼續佔用記憶體空間。XAML will continue to occupy memory space in your app process while it stays in the background. 啟動延遲和記憶體使用量的數量,取決於您的應用程式在切換至原生視圖之前的 XAML 用途。The amount of startup delay and memory usage depends on what your app does with XAML before switching to the native view. 如果您在 XAML 啟動程式碼中的第一次沒有執行任何動作,但開始您的沉浸式觀賞,則影回應該會很小。If you do nothing in your XAML startup code at first except start your immersive view, the impact should be minor. 此外,因為您的全像是直接在沉浸式的觀賞中進行,所以您會在該轉譯上避免任何與 XAML 相關的限制。Also, because your holographic rendering is done directly to the immersive view, you'll avoid any XAML-related restrictions on that rendering.

CPU 和 GPU 的記憶體使用量計數。The memory usage counts for both CPU and GPU. Direct3D 11 可以交換虛擬圖形記憶體,但它可能無法交換部分或所有的 XAML GPU 資源,而且可能會有明顯的效能衝擊。Direct3D 11 can swap virtual graphics memory, but it might not be able to swap out some or all of the XAML GPU resources, and there might be a noticeable performance hit. 無論何種方式,您都不需要載入任何 XAML 功能,也不需要載入更多應用程式的空間,並提供更好的體驗。Either way, not loading any XAML features you don't need will leave more room for your app and provide a better experience.

XAML 視圖切換工作流程XAML view switching workflow

從 XAML 直接進入沉浸式模式的應用程式工作流程,如下所示:The workflow for an app that goes directly from XAML to immersive mode is like so:

  • 應用程式會在 2D XAML 視圖中啟動。The app starts up in the 2D XAML view.
  • 應用程式的 XAML 啟動順序會偵測目前的系統是否支援全像轉譯:The app’s XAML startup sequence detects if the current system supports holographic rendering:
  • 如果是,應用程式會建立沉浸式視圖,並立即將它帶到前景。If so, the app creates the immersive view and brings it to the foreground right away. 在 Windows Mixed Reality 裝置上不需要的任何動作都會略過 XAML 載入,包括 XAML 視圖中的任何轉譯類別和資產載入。XAML loading is skipped for anything not necessary on Windows Mixed Reality devices, including any rendering classes and asset loading in the XAML view. 如果應用程式使用 XAML 進行鍵盤輸入,則仍應建立該輸入頁面。If the app is using XAML for keyboard input, that input page should still be created.
  • 如果沒有,則 XAML view 可以正常地繼續使用商務。If not, the XAML view can continue with business as usual.

跨兩個視圖呈現圖形的秘訣Tip for rendering graphics across both views

如果您的應用程式需要針對 Windows Mixed Reality 中的 XAML 視圖在 DirectX 中執行某種程度的轉譯,最好的做法是建立一個可同時使用兩個視圖的轉譯器。If your app needs to implement some amount of rendering in DirectX for the XAML view in Windows Mixed Reality, your best bet is to create one renderer that can work with both views. 轉譯器應該是一個可從這兩個視圖存取的實例,而且應該在2D 和全像攝影轉譯之間切換。The renderer should be one instance that can be accessed from both views, and it should switch between 2D and holographic rendering. 如此一來,GPU 資產只會載入一次,可減少載入時間、記憶體影響,以及切換視圖時交換的資源數量。This way GPU assets only load once, which reduces load times, memory impact, and the amount of swapped resources when switching views.