身為 C# 傳統型應用程式開發人員,您可以在 .NET 中使用代表數個互操作性函式和 Windows 運行時間 (WinRT) COM 互操作性介面的 C# Interop 類別。 這些包括代表 IWindowNative、 IInitializeWithWindow、 GetWindowIdFromWindow 函式等 C# 類別。
本主題列出可用的 C# Interop 類別,並示範如何使用它們。 主題末尾的 背景 部分說明了 .NET 先前版本中互通介面的使用方式,以及為何會做出這項變更。
設定 .NET 桌面專案以使用 C# Interop 類別
下一節中所列的 C# Interop 類別(可用的 C# interop 類別)可在 .NET 中取得,無論是作為 Windows App SDK的一部分,或是使用特定的 Target Framework Moniker,如我們所見。
在 WinUI 3 C# 桌面專案中
當您在 Visual Studio 中建立新的 WinUI 3 專案時(請參閱 建立您的第一個 WinUI 3 專案),您的項目已設定好,而且您可以立即開始使用所有 C# Interop 類別。
在其他 C# 桌面項目類型中 (WPF 或 WinForms)
對於其他 .NET 桌面項目類型,例如 Windows Presentation Foundation (WPF) 或 Windows Forms (WinForms),您必須設定專案,才能存取 C# Interop 類別。 針對下面所列的第一組類別,您必須參考 Windows App SDK。 針對第二組,您必須設定以 Windows 10 版本 1809 或更新版本為目標的 Target Framework Moniker,如下所示:
開啟 C# .NET 桌面項目的項目檔。
在檔案中
.csproj,修改 TargetFramework 元素以鎖定特定的 .NET 和 Windows SDK 版本。 例如,下列元素適用於以 Windows 10 版本 2004 為目標的 .NET 6 專案。<PropertyGroup> <TargetFramework>net8.0-windows10.0.19041.0</TargetFramework> </PropertyGroup>
如需更多資訊,包括其他支援值的清單,請參閱 目標框架標識符選項。
可用的 C# Interop 類別
Note
下列類別需要 .NET 6 SDK 或更新版本。
以下是從其基礎互操作函式或 WinRT COM 互操作介面對應的可用 C# 互操作類別。 列出的每個類別都會實作其基礎 Interop API 的函式/方法,並為參數和回傳值提供型別安全的包裝器。 例如, Windows.ApplicationModel.DataTransfer.DragDrop.Core.DragDropManagerInterop.GetForWindow 需要一個 IntPtr 視窗 handle (HWND) 參數,並回傳 CoreDragDropManager 物件。 以下的所有 C# Interop 類別和相關聯的方法都是靜態的。
可在 Windows App SDK 中取得
Microsoft.UI.Win32Interop 類別實作了下表中的 C# 互通方法。 如需程式代碼範例,請參閱 管理應用程式視窗。
| 互操作功能 | C# 互操作方法 |
|---|---|
| GetDisplayIdFromMonitor | (Microsoft.UI) DisplayId Win32Interop.GetDisplayIdFromMonitor(IntPtr hmonitor) |
| GetIconFromIconId | (Microsoft.UI) IntPtr Win32Interop.GetIconFromIconId(IconId iconId) |
| GetIconIdFromIcon | (Microsoft.UI) IconId Win32Interop.GetIconIdFromIcon(IntPtr hicon) |
| GetMonitorFromDisplayId | (Microsoft.UI) IntPtr Win32Interop.GetMonitorFromDisplayId(DisplayId displayId) |
| GetWindowFromWindowId | (Microsoft.UI) IntPtr Win32Interop.GetWindowFromWindowId(WindowId windowId) |
| GetWindowIdFromWindow | (Microsoft.UI) WindowId Win32Interop.GetWindowIdFromWindow(IntPtr hwnd) |
可透過目標 Framework Moniker 取得
| WinRT COM Interop 介面 | C# Interop 類別 |
|---|---|
| IAccountsSettingsPaneInterop | (Windows.UI.ApplicationSettings) AccountsSettingsPaneInterop |
| IDisplayInformationStaticsInterop | 引進 TFM net6.0-windows10.0.22621.0 和 .NET 6.0.7。(Windows.Graphics.Display) DisplayInformationInterop |
| IDragDropManagerInterop | (Windows.ApplicationModel.DataTransfer.DragDrop.Core) DragDropManagerInterop |
| IInitializeWithWindow | (WinRT.Interop) 使用視窗初始化 |
| IInputPaneInterop | (Windows.UI.ViewManagement) InputPaneInterop |
| IPlayToManagerInterop | (Windows.Media.PlayTo) PlayToManagerInterop |
| IPrintManagerInterop | (Windows.Graphics.Printing) PrintManagerInterop |
| IRadialControllerConfigurationInterop | (Windows.UI.Input) RadialControllerConfigurationInterop |
| IRadialControllerIndependentInputSourceInterop | (Windows.UI.Input.Core) RadialControllerIndependentInputSourceInterop |
| IRadialControllerInterop | (Windows.UI.Input) RadialControllerInterop |
| ISpatialInteractionManagerInterop | (Windows.UI.Input.Spatial) SpatialInteractionManagerInterop |
| ISystemMediaTransportControlsInterop | (Windows.Media) SystemMediaTransportControlsInterop |
| IUIViewSettingsInterop | (Windows.UI.ViewManagement) UIViewSettingsInterop |
| IUserConsentVerifierInterop | (Windows.Security.Credentials.UI) UserConsentVerifierInterop |
| IWebAuthenticationCoreManagerInterop | (Windows.Security.Authentication.Web.Core) WebAuthenticationCoreManagerInterop |
| IWindowNative | 只限於 WinUI 3 (WinRT.Interop) WindowNative 如需 WPF 和 WinForms 的替代方案,請參閱 擷取視窗控點 (HWND)。 |
程式碼範例
此程式代碼範例示範如何在 WinUI 3 應用程式中使用兩個 C# Interop 類別(請參閱 建立您的第一個 WinUI 3 專案)。 範例情境是要顯示 Windows.Storage.Pickers.FolderPicker。 但在桌面應用程式中顯示選擇器之前,必須使用擁有者視窗的句柄 (HWND) 來初始化它。
- 你可以透過使用 IWindowNative WinRT COM 互操作介面來取得視窗處理常式(HWND)。 而且(參考前一節的表格)這個介面是由 WinRT.Interop.WindowNative C# 互操作類別來表示。 此處物件
this是主視窗程式碼後方檔案中 Microsoft.UI.Xaml.Window 物件的參考。 - 要初始化帶有擁有者視窗的 UI 片段,可以使用 IInitializeWithWindow WinRT COM 互操作介面。 這個介面由 WinRT.Interop.InitializeWithWindow C# 互操作類別來表示。
private async void myButton_Click(object sender, RoutedEventArgs e)
{
// Create a folder picker.
var folderPicker = new Windows.Storage.Pickers.FolderPicker();
// 1. Retrieve the window handle (HWND) of the current WinUI 3 window.
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
// 2. Initialize the folder picker with the window handle (HWND).
WinRT.Interop.InitializeWithWindow.Initialize(folderPicker, hWnd);
// Use the folder picker as usual.
folderPicker.FileTypeFilter.Add("*");
var folder = await folderPicker.PickSingleFolderAsync();
}
另請參閱 擷取視窗句柄 (HWND) 和 顯示相依於 CoreWindow 的 WinRT UI 物件。
Background
舊版 .NET Framework 和 .NET Core 具有 WinRT 的內建知識。 在之前的版本中,你可以直接用 C# 定義一個互通介面,並用 ComImport 屬性,然後直接將投影類別投射到該互操作介面。
由於 WinRT 是 Windows 專用技術,為了支援 .NET 的可攜性與效率目標,我們將 WinRT 投影支援從 C# 編譯器和 .NET 執行時移除,移入 C#/WinRT 工具包(參見 .NET 已移除 WinRT 內建支援)。
雖然 ComImport 技術仍適用於基於 IUnknown 的互操作介面,但對於用於與 WinRT 互通的基於 IInspectable 的介面已不再適用。
因此,在 .NET 中,您可以使用本主題中所述的 C# Interop 類別。
疑難解答和已知問題
C# Interop 類別目前沒有已知問題。 若要提供意見反應或回報其他問題,請將您的意見反應新增至現有問題,或在 WindowsAppSDK GitHub 存放庫上提出新問題,。
相關主題
- 建立您的第一個 WinUI 3 專案
- 管理應用程式視窗
- 取得視窗控制代碼 (HWND)
- 顯示相依於 CoreWindow 的 WinRT UI 物件