從 .NET 應用程式呼叫 Interop API

身為 C# 傳統型應用程式開發人員,在 .NET 中,您可以使用 C# Interop 類別,其代表數個互通性函式和 Windows 執行階段 (WinRT) COM 互通性介面。 其中包括代表 IWindowNativeIInitializeWithWindow 的 C# 類別、GetWindowIdFromWindow 函式和其他許多項目。

本主題列出可用的 C# Interop 類別,並示範如何使用。 本主題結尾的背景一節說明 Interop 介面在舊版 .NET 中的使用方式,以及變更的原因。

設定 .NET 傳統型專案以使用 C# Interop 類別

下一節中所列的 C# Interop 類別 (可用的 C# Interop 類別) 可在 .NET 中取得,無論是作為 Windows 應用程式 SDK 的一部分,或是使用特定目標 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 應用程式 SDK。 針對第二組,您必須設定以 Windows 10 版本 1809 或更新版本為目標的目標 Framework Moniker,如下所示:

  1. 開啟 C# .NET 傳統型專案的專案檔。

  2. .csproj 檔案中,將 TargetFramework 元素修改為以特定 .NET 和 Windows SDK 版本為目標。 例如,下列元素適合以 Windows 10 版本 2004 作為目標的 .NET 6 專案。

    <PropertyGroup>
      <TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
    </PropertyGroup>
    

如需詳細資訊 (包括其他支援值的清單),請參閱使用目標 Framework Moniker 選項

可用 C# Interop 類別

注意

以下類別需要 .NET 6 SDK 或更新版本。

以下是從其基礎 Interop 函式或 WinRT COM Interop 介面對應的可用 C# Interop 類別。 列出的每個類別都會實作其基礎 Interop API 的函式/方法,並提供參數和傳回值的類型安全包裝函式。 例如,Windows.ApplicationModel.DataTransfer.DragDrop.Core.DragDropManagerInterop.GetForWindow 需要 IntPtr 視窗控制代碼 (HWND) 參數,並且傳回 CoreDragDropManager 物件。 以下的所有 C# Interop 類別和相關聯方法都是靜態的。

作為 Windows 應用程式 SDK 的一部分提供使用

Microsoft.UI.Win32Interop 類別會實作下表中的 C# Interop 方法。 如需程式碼範例,請參閱管理應用程式視窗

Interop 函式 C# Interop 方法
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) InitializeWithWindow
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) 進行初始化。

  1. 您可以使用 IWindowNative WinRT COM Interop 介面來取得視窗控制代碼 (HWND)。 以及 (請參閱上一節中的資料表) 介面是以 WinRT.Interop.WindowNative C# Interop 類別表示。 在這裡,this 物件是主視窗程式碼後置檔案之 Microsoft.UI.Xaml.Window 物件的參考。
  2. 若要使用擁有者視窗初始化 UI 片段,您可以使用 IInitializeWithWindow WinRT COM Interop 介面。 而該介面是以 WinRT.Interop.InitializeWithWindow C# Interop 類別表示。
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 物件

背景

舊版 .NET Framework 和 .NET Core 已內建 WinRT 的知識。 使用這些舊版,您可以使用 ComImport 屬性,直接在 C# 中定義 Interop 介面,然後將投影類別直接轉換成該 Interop 介面。

因為 WinRT 是 Windows 專屬技術,為了支援 .NET 的可攜性和效率目標,我們已從 C# 編譯器和 .NET 執行階段提取 WinRT 投影支援,並將其移至 C#/WinRT 工具組。(請參閱 WinRT 內建支援已從 .NET中移除)。

雖然 ComImport 技術仍然適用於 IUnknown 型 Interop 介面,但是不再適用於可用於與 WinRT 交互操作的 IInspectable 型介面。

因此作為取代項目,在 .NET 中,您可以使用本主題中所述的 C# Interop 類別。

疑難排解和已知問題

C# Interop 類別目前沒有已知問題。 若要提供意見反應或回報其他問題,請將您的意見反應新增至現有問題,或在 WindowsAppSDK GitHub 存放庫上提出新問題。