作為 C# 桌面應用程式開發者,在 .NET 中,你可以利用代表多個互通函式的 C# 互操作類別,以及 Windows Runtime(WinRT)COM 互通介面。 這些包括代表 IWindowNative、 IInitializeWithWindow、 GetWindowIdFromWindow 函式等 C# 類別。
本主題列出可用的 C# Interop 類別,並示範如何使用它們。 主題末尾的Background章節說明了.NET先前版本中互操作介面的使用方式,以及為何會做出這項變更。
設定一個.NET桌面專案以使用 C# 互操作類別
下一節列出的 C# 互通類別(Available C# interop classes)可在 .NET 中取得,或作為 Windows App SDK 的一部分,或透過特定的 Target Framework Moniker,我們將會看到。
在 WinUI 3 C# 桌面專案
當你在 Visual Studio 建立新的 WinUI project(參見 建立你的第一個 WinUI project),你的project已經設定好,你可以立刻開始使用所有 C# 互操作類別。
在其他 C# 桌面專案類型 (例如 WPF 或 WinForms) 中
對於其他 .NET 桌面專案類型,如 Windows Presentation Foundation(WPF) 或 Windows Forms(WinForms),你需要先設定好專案,才能存取 C# 互操作類別。 以下列出的第一組課程,你需要參考Windows App SDK。 第二組則需配置一個
打開你的 C# .NET 桌面專案的檔案。
在
.csproj檔案中,修改 TargetFramework 元素,以鎖定特定.NET及 Windows SDK 版本。 例如,以下元素適用於針對Windows 10的.NET 6 project,版本為 2004。<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 (WinRT.Interop) WindowNative 關於 WPF 和 WinForms 的替代方案,請參見 Retrieve a window handle (HWND)。 |
程式碼範例
這個程式碼範例示範如何在 WinUI 應用程式中使用兩個 C# 互通類別(參見 Create your first WinUI project)。 範例情境是顯示一個 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 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 執行時移除,移至
雖然 ComImport 技術仍適用於基於 IUnknown 的互操作介面,但對於用於與 WinRT 互通的基於 IInspectable 的介面已不再適用。
所以作為替代方案,在 .NET 中,你可以利用本主題中描述的 C# 互通類別。
疑難解答和已知問題
C# Interop 類別目前沒有已知問題。 若要提供回饋或回報其他問題,請將回饋加入現有問題,或在 WindowsAppSDK GitHub repo 提交新議題。
相關主題
- 建立你的第一個 WinUI project
- 管理應用程式視窗
- 取得視窗控制代碼 (HWND)
- 顯示相依於 CoreWindow 的 WinRT UI 物件