本文列出了 C#/WinRT 在針對 .NET 6(或更新版本)桌面應用程式中,Windows 執行階段(WinRT)類型與 .NET 類型之間的映射。 在這些應用程式中,Visual Studio IntelliSense 顯示的是 .NET 類型,而非 Windows 執行階段 類型。 例如,若Windows 執行階段方法的參數型別為 IVector<string>,則 IntelliSense 會顯示一個型別為 IList<string>。 同樣地,在使用 C#/WinRT 撰寫的 WinRT 元件中,成員簽章會使用 .NET 類型。 當你使用 C#/WinRT 產生 Windows 執行階段 元件時,.NET 類型會被轉換成對應的 WinRT 類型。
C#/WinRT 的自訂型態映射在 Windows SDK 或 WinUI 3中依型別分類(WinUI 屬於 Windows 應用程式 SDK)。 Windows SDK 映射的 WinRT 類型位於 Windows.* 命名空間,WinUI 映射的 WinRT 類型則位於 Microsoft。UI。Xaml.*命名空間。 C#/WinRT 為 WinRT 類型做自定義類型映射的原因有兩個:
WinRT 類型會映射到 .NET 類型,但名稱和/或命名空間不同。 這些自訂映射是用來將 WinRT 類型映射到現有的 .NET 等效類型。 有些情況下,對應會是對應到不同的型別(例如,值型別會對應至類別型別)。
WinRT 類型會映射到相同名稱和命名空間的 .NET 類型。 這些自定義對應通常是基於效能或增強的原因,而且會直接在 C# 中實作。 大多數在 WinRT 與 .NET 中擁有相同命名空間名稱與類型名稱的類型是結構(或與結構相關聯的類型,如列舉)。 在 WinRT 中,結構體除了欄位外沒有其他成員,且需要輔助型別,而 .NET 會隱藏這些類型。 這些結構的 .NET 版本具有提供隱藏輔助類型功能的方法和屬性(例如 Windows.UI.Color)。
注意
關於 WinRT 與 .NET 類型在 UWP 應用程式中對應的列表,請參見 .NET Windows 執行階段類型映射。
Windows SDK 中 WinRT 類型的映射
具有不同名稱和/或命名空間的類型
| WinRT 類型/命名空間 | .NET 類型/命名空間 |
|---|---|
| DateTime(Windows.Foundation) | DateTimeOffset (系統) |
| EventHandler<T> (Windows 基金會) | EventHandler<T> (System) |
| EventRegistrationToken(Windows.Foundation) | EventRegistrationToken (WinRT) |
| HResult(Windows.Foundation) | 例外狀況 (系統) |
| IClosable(Windows.Foundation) | IDisposable (系統) |
| 參考<T> (Windows.Foundation) | Nullable<T> [系統] |
| TimeSpan(Windows.Foundation) | TimeSpan (系統) |
| Uri(Windows.Foundation) | Uri(系統) |
| 可迭代<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.Foundation.Numerics) | 四元數 (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 |