桌面應用程式不支援 WinRT API

雖然你可以在 C# 或 C++ 桌面應用程式中使用大多數 Windows 執行階段(WinRT)API(參見 Windows 執行階段(WinRT)命名空間),但有兩組主要的 WinRT API 不支援桌面應用程式,或有限制:

  • 這些 API 依賴於僅設計用於 Universal Windows Platform(UWP)應用程式的使用者介面(UI)功能。
  • 需要套件識別資料的 API (請參閱需要套件識別資料的功能)。 此類 API 僅在使用 MSIX 封裝的傳統型應用程式中才支援。

本文提供這兩組 WinRT API 的詳細資料。 如果可用,本文會建議替代 API,以達到與傳統型應用程式中不受支援 API 相同的功能。 大多數替代 API 可在 WinUI 3 或 Windows SDK 中的 WinRT COM 介面中取得。

Note

使用 .NET 的應用程式可以利用本文列出的部分 WinRT COM 介面所提供的類別實作。 這些類別比直接使用 WinRT COM 介面更容易使用。 如需可用類別實作的詳細資訊,請參閱從 .NET 應用程式呼叫 Interop API。 請注意,這些類別需要 .NET 6 SDK 或更新版本。

具有僅限 UWP UI 功能相依性的 API

某些 WinRT API 專為 UWP 應用程式中的 UI 案例所設計。 由於執行緒模式和其他平台差異,這些 API 在傳統型應用程式中的行為不正確。 傳統型應用程式不支援這些 API 和其他與其具有相依性的 WinRT API。

核心不支援的類別

這些 WinRT 類別不支援於桌面應用程式:

Class 替代 API
ApplicationView None
CoreApplicationView 改用 WinUI 提供的 Window 類別。
CoreApplicationViewTitleBar 與其使用 ExtendViewIntoTitleBar 屬性,不如使用 WinUI 提供的 Window.ExtendsContentIntoTitleBar 屬性。
CoreDispatcher 改用 WinUI 提供的 Microsoft.UI.Xaml.Window.DispatcherQueue 屬性。

請注意,Windows.UI.Xaml.Window.DispatcherWindows.UI.Xaml.DependencyObject.Dispatcher 屬性會在傳統型應用程式中傳回 null
CoreWindow 另請參閱下列實作 IInitializeWithWindow 的類別一節。

與其使用 GetKeyState 方法,不如使用 WinUI 提供的 InputKeyboardSource.GetKeyStateForCurrentThread 方法。

與其使用 PointerCursor 屬性,不如使用 WinUI 提供的 UIElement.ProtectedCursor 屬性。 您必須有 UIElement 的子類別才能存取該屬性。
UserActivity 請改用 IUserActivitySourceHostInterop COM 介面 (在 useractivityinterop.h 中)。

針對桌面應用程式不支援的其他 WinRT API,請參閱本主題中的不支援的成員

具有 XxxForCurrentView 方法的類別

許多 WinRT 類別都有靜態 GetForCurrentViewCreateForCurrentView 方法,例如 UIViewSettings.GetForCurrentView。 這些 XxxForCurrentView 方法與 ApplicationView 類型具有隱含相依性,在傳統型應用程式中不受支援。 因為在桌面應用程式中不支援 ApplicationView,因此不支援 XxxForCurrentView 這些方法。 某些不支援的 XxxForCurrentView 方法不僅會傳回 null,也會擲回例外狀況。

Note

在桌面應用程式中,支援 CoreInputView.GetForCurrentView,且即使沒有 CoreWindow 也能使用它。 您可以使用該方法在任何執行緒上擷取 CoreInputView 物件;如果該執行緒具有前景視窗,則該物件將會產生事件。

在桌面應用程式中支援下列類別;但若要在桌面應用程式中獲取其中一個的實例,您需要使用與 GetForCurrentViewCreateForCurrentView 方法不同的機制。 針對下列將 COM 介面列為替代 API 的類別,C# 開發人員也可以取用這些 WinRT COM 介面 (請參閱從 .NET 應用程式呼叫 Interop API)。 該清單可能不完整。

Class 替代 API
AccountsSettingsPane 請改用 IAccountsSettingsPaneInterop COM 介面 (在 accountssettingspaneinterop.h 中)。
CoreDragDropManager 請改用 IDragDropManagerInterop COM 介面 (在 dragdropinterop.h 中)。
CoreTextServicesManager 此類別目前僅在 Windows Insider Preview 組建中支援桌面應用程式。
DataTransferManager 請改用 IDataTransferManagerInterop COM 介面 (在 shobjidl_core.h 中)。
DisplayInformation 若要擷取 DisplayInformation 的執行個體,請使用 IDisplayInformationStaticsInterop 介面。

或者,你可以不使用 LogicalDpi 屬性,而是使用 XamlRoot.RasterizationScale 屬性,並透過 XamlRoot.Changed 事件監聽變更( XamlRoot.RasterizationScale 屬性在 WinUI 中提供)。

而且,除了 RawPixelsPerViewPixel 屬性外,你也可以選擇使用 WinUI 提供的 XamlRoot.RasterizationScale 屬性。
InputPane 請改用 IInputPaneInterop COM 介面 (在 inputpaneinterop.h 中)。
PlayToManager 請改用 IPlayToManagerInterop COM 介面 (在 playtomanagerinterop.h 中)。
Print3DManager 請改用 IPrinting3DManagerInterop COM 介面 (在 print3dmanagerinterop.h 中)。
PrintManager 請改用 IPrintManagerInterop COM 介面 (在 printmanagerinterop.h 中)。
RadialController 請改用 IRadialControllerInterop COM 介面 (在 radialcontrollerinterop.h 中)。
RadialControllerConfiguration 請改用 IRadialControllerConfigurationInterop COM 介面 (在 radialcontrollerinterop.h 中)。
ResourceContext 請參見 MRT 到 MRT Core 遷移
ResourceLoader 請參見 MRT 到 MRT Core 遷移
SpatialInteractionManager 請改用 ISpatialInteractionManagerInterop COM 介面 (在 spatialinteractionmanagerinterop.h 中)。
SystemMediaTransportControls 請改用 ISystemMediaTransportControlsInterop COM 介面 (在 systemmediatransportcontrolsinterop.h 中)。
UserActivityRequestManager 請改用 IUserActivityRequestManagerInterop COM 介面(在 useractivityinterop.h 中)。
UIViewSettings 請改用 IUIViewSettingsInterop COM 介面 (在 uiviewsettingsinterop.h 中)。

傳統型應用程式中不支援下列類別,因為 API 不提供其 GetForCurrentViewCreateForCurrentView 方法的替代方案。 該清單可能不完整。

Class 替代 API
AppCapture None
BrightnessOverride None
ConnectedAnimationService None
CoreInputView None
CoreWindowResizeManager None
DisplayEnhancementOverride None
EdgeGesture None
GazeInputSourcePreview None
HdmiDisplayInformation None
全息鍵盤位置覆蓋預覽 None
KeyboardDeliveryInterceptor None
LockApplicationHost None
MouseDevice None
指標視覺化設定 None
ProtectionPolicyManager None
SearchPane None
SettingsPane None
SystemNavigationManager None
SystemNavigationManagerPreview None
WebAuthenticationBroker 沒有。 更多細節請參閱 WebAuthenticationBroker.AuthenticateAsync 拋出 COMException GitHub 問題。

實作 IInitializeWithWindow 的類別

某些選擇器、彈出視窗、對話框及其他Windows 執行階段(WinRT)物件依賴於CoreWindow;通常用於顯示使用者介面。 即使傳統型應用程式不支援 CoreWindow (請參閱上述核心不支援的類別),您仍然可以透過新增少量互通程式碼,在傳統型應用程式中使用許多 WinRT 類別。

如需詳細資訊 (包括受影響的類型清單),以及程式碼範例,請參閱顯示相依於 CoreWindow 的 WinRT UI 物件

不支援的成員

本節列出 WinRT 類別中的特定成員,這些成員不支援在桌面應用程式中使用。若無法提供完整清單,則以描述形式提供資訊。 除非另有說明,否則桌面應用程式中支援的類別除了這些成員外,還包括其餘類別。

Events

除了指定的事件,傳統型應用程式中支援下列類別。

Class 不支援的事件
UISettings ColorValuesChanged
無障礙設定 HighContrastChanged

方法

除了指定的方法,傳統型應用程式中支援下列類別。

Class 不支援的方法
DeviceInformationPairing PairAsync

使用 Request 命名模式的方法

傳統型應用程式不支援遵循請求 命名模式的大多數方法 (例如 AppCapability.RequestAccessAsyncStoreContext.RequestPurchaseAsync)。 在內部,這些方法會使用 Windows.UI.Popups 類別。 該類別要求執行緒具有 CoreWindow 物件,這在傳統型應用程式中不受支援。

遵循要求命名模式的方法完整清單很長,本文不提供這些方法的完整清單。

需要套件識別的 API

下列 WinRT 類別需要套件識別資料 (請參閱需要套件識別資料的功能)。 這些 API 僅支援封裝的傳統型應用程式 (也就是在執行階段具有套件識別資料)。 該清單可能不完整。

Windows。ApplicationModel...

Windows.Data...

Windows。裝置......

Windows。基金會......

Windows。全球化......

Windows.Graphics

Windows。管理層......

Windows.Media

Windows。網路......

Windows。服務。地圖......

Important

Windows 地圖平台 API(Windows。Services.Maps.*)已被棄用,未來版本的 Windows 可能無法使用。 欲了解更多資訊,請參閱 已棄用功能資源

Windows。Services.Store......

Windows。儲存......

Windows。系統......

Windows.UI...

此外,從沒有套件識別的桌面應用程式呼叫時,AdaptiveMediaSource.CreateFromUriAsync 方法不支援 ms-appxms-resource URI 格式。

另請參閱