本文列出 C#/WinRT 在目標為 .NET 6(或更新版本)的桌面應用程式中 Windows 運行時 (WinRT) 類型和 .NET 類型之間的對應。 在這些應用程式中,Visual Studio IntelliSense 會顯示 .NET 類型,而不是 Windows 運行時間類型。 例如,如果 Windows 執行時間方法接受類型為 IVector<字串>的參數,則 IntelliSense 會顯示類型為 IList<字串>的參數。 同樣地,在使用 C#/WinRT 撰寫的 WinRT 元件中,您會在成員簽章中使用 .NET 類型。 當您使用 C#/WinRT 來產生 Windows 執行時間元件時,.NET 類型會轉譯成對應的 WinRT 類型。
C#/WinRT 自定義類型對應會依 Windows SDK 中的類型或 winUI 3 中的類型分類(WinUI 3 是 Windows App SDK的一部分)。 適用於 Windows SDK 的 WinRT 類型會存在於 Windows.* 命名空間之下,而 WinUI 3 的 WinRT 類型則存在於 Microsoft.UI.Xaml.* 命名空間之下。 C#/WinRT 為 WinRT 類型做自定義類型映射的原因有兩個:
對應至不同名稱和/或命名空間的 .NET 類型的 WinRT 類型。 這些自訂對應是用來將 WinRT 類型映射到現有的 .NET 等效類型。 有些情況下,對應會是對應到不同的型別(例如,值型別會對應至類別型別)。
對應至相同名稱和命名空間之 .NET 類型的 WinRT 類型。 這些自定義對應通常是基於效能或增強的原因,而且會直接在 C# 中實作。 在 WinRT 和 .NET 中具有相同命名空間名稱和類型名稱的大部分類型都是結構(或與結構相關聯的類型,例如列舉)。 在 WinRT 中,結構除了欄位外沒有其他成員,且需要 .NET 隱藏的輔助類型。 這些結構的 .NET 版本具有屬性和方法,可提供隱藏協助程序類型的功能(例如,Windows.UI.Color)。
注意
如需 UWP app 內容中 WinRT 與 .NET 類型之間的對應清單,請參閱在 UWP 中WinRT 類型的 .NET 對應。
Windows SDK 中 WinRT 類型的對應
具有不同名稱和/或命名空間的類型
| WinRT 類型/命名空間 | .NET 類型/命名空間 |
|---|---|
| DateTime (Windows.Foundation) | DateTimeOffset (系統) |
| EventHandler<T> (Windows.Foundation) | EventHandler<T> (System) |
| EventRegistrationToken (Windows.Foundation) | EventRegistrationToken (WinRT) |
| HResult (Windows.Foundation) | 例外狀況 (系統) |
| IClosable (Windows.Foundation) | IDisposable (系統) |
| IReference<T> (Windows.Foundation) | Nullable<T> [系統] |
| TimeSpan (Windows.Foundation) | TimeSpan (系統) |
| Uri (Windows.Foundation) | Uri(系統) |
| IIterable<T> (Windows.Foundation.Collections) | IEnumerable<T> (System.Collections.Generic) |
| IIterator<T> (Windows.Foundation.Collections) | IEnumerator<T> (System.Collections.Generic) |
| IMap<K,V> (Windows.Foundation.Collections) | IDictionary<TKey、TValue> (System.Collections.Generic) |
| IMapView<K,V> (Windows.Foundation.Collections) | IReadOnlyDictionary<TKey,TValue> (System.Collections.Generic) |
| IKeyValuePair<K,V> (Windows.Foundation.Collections) | KeyValuePair<TKey,TValue> (System.Collections.Generic) |
| IVector<T> (Windows.Foundation.Collections) | IList<T> (System.Collections.Generic) |
| IVectorView<T> (Windows.Foundation.Collections) | IReadOnlyList<T> (System.Collections.Generic) |
| AttributeTargets (Windows.Foundation.Metadata) | AttributeTargets (系統) |
| AttributeUsageAttribute (Windows.Foundation.Metadata) | AttributeUsageAttribute (系統) |
| Matrix3x2 (Windows.Foundation.Numerics) | Matrix3x2 (System.Numerics) |
| Matrix4x4(Windows.Foundation.Numerics) | Matrix4x4 (System.Numerics) |
| 平面(Windows.Foundation.Numerics) | 平面 (System.Numerics) |
| 四元數(Windows 基礎 數值) | 四元數 (System.Numerics) |
| Vector2 (Windows.Foundation.Numerics) | Vector2 (System.Numerics) |
| Vector3 (Windows.Foundation.Numerics) | Vector3 (System.Numerics) |
| Vector4 (Windows.Foundation.Numerics) | Vector4 (System.Numerics) |
| IBindableIterable (Windows.UI.Xaml.Interop) | IEnumerable(System.Collections) |
| IBindableVector (Windows.UI.Xaml.Interop) | IList (System.Collections) |
| TypeName (Windows.UI.Xaml.Interop) | 型態 (系統) |
具有相同名稱和命名空間的類型
| 類型 | Namespace |
|---|---|
| IPropertyValue | Windows.Foundation |
| IReferenceArray<T> | Windows.Foundation |
| 點 | Windows.Foundation |
| 矩形 | Windows.Foundation |
| 大小 | Windows.Foundation |
| 顏色 | Windows.UI |
| TypeKind | Windows.UI.Xaml.Interop |
WinUI 中 WinRT 類型的明細表
具有不同名稱和/或命名空間的類型
| WinRT 類型/命名空間 | .NET 類型/命名空間 |
|---|---|
| INotifyCollectionChanged (Microsoft.UI.Xaml.Data) | INotifyCollectionChanged (System.Collections.Specialized) |
| NotifyCollectionChangedEventHandler (Microsoft.UI.Xaml.Data) | NotifyCollectionChangedEventHandler (System.Collections.Specialized) |
| NotifyCollectionChangedEventArgs(Microsoft.UI.Xaml.Data) | NotifyCollectionChangedEventArgs (System.Collections.Specialized) |
| NotifyCollectionChangedAction (Microsoft.UI.Xaml.Data) | NotifyCollectionChangedAction (System.Collections.Specialized) |
| DataErrorsChangedEventArgs(Microsoft.UI.Xaml.Data) | DataErrorsChangedEventArgs (System.ComponentModel) |
| INotifyDataErrorInfo (Microsoft.UI.Xaml.Data) | INotifyDataErrorInfo (System.ComponentModel) |
| INotifyPropertyChanged (Microsoft.UI.Xaml.Data) | INotifyPropertyChanged(System.ComponentModel) |
| PropertyChangedEventHandler (Microsoft.UI.Xaml.Data) | PropertyChangedEventHandler (System.ComponentModel) |
| PropertyChangedEventArgs (Microsoft.UI.Xaml.Data) | PropertyChangedEventArgs (System.ComponentModel) |
| ICommand (Microsoft.UI.Xaml.Input) | ICommand (System.Windows.Input) |
| IXamlServiceProvider (Microsoft.UI.Xaml) | IServiceProvider (系統) |
具有相同名稱和命名空間的類型
| 類型 | Namespace |
|---|---|
| CornerRadius | Microsoft.UI.Xaml |
| 期間 | Microsoft.UI.Xaml |
| 持續時間類型 | Microsoft.UI.Xaml |
| 網格長度 | Microsoft.UI.Xaml |
| 網格單位類型 (GridUnitType) | Microsoft.UI.Xaml |
| 厚度 | Microsoft.UI.Xaml |
| 發電機位置 | Microsoft.UI.Xaml.Controls.Primitives |
| 矩陣 | Microsoft.UI.Xaml.Media |
| KeyTime | Microsoft.UI.Xaml.Media.Animation |
| 重複行為 | Microsoft.UI.Xaml.Media.Animation |
| RepeatBehaviorType | Microsoft.UI.Xaml.Media.Animation |
| Matrix3D | (Microsoft.UI.Xaml.Media.Media3D) |