Share via


C#/WinRT 中 WinRT 類型的 .NET 對應

本文列出 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 應用程式 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 應用程式內容中 WinRT 與 .NET 類型之間的對應清單,請參閱 UWP 中 WinRT 類型的 .NET 對應

Windows SDK 中 WinRT 類型的對應

具有不同名稱和/或命名空間的類型

WinRT 類型/命名空間 .NET 類型/命名空間
DateTime (Windows.Foundation) DateTimeOffset (System)
EventHandler<T> (Windows.Foundation) EventHandler<T> (System)
EventRegistrationToken (Windows.Foundation) EventRegistrationToken (WinRT)
HResult (Windows.Foundation) Exception (System)
IClosable (Windows.Foundation) IDisposable (System)
IReference<T> (Windows.Foundation) Nullable<T> (System)
TimeSpan (Windows.Foundation) TimeSpan (System)
Uri (Windows.Foundation) Uri (System)
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 (System)
AttributeUsageAttribute (Windows.Foundation.Metadata) AttributeUsageAttribute (System)
Matrix3x2 (Windows.Foundation.Numerics) Matrix3x2 (System.Numerics)
Matrix4x4 (Windows.Foundation.Numerics) Matrix4x4 (System.Numerics)
Plane (Windows.Foundation.Numerics) Plane (System.Numerics)
Quaternion (Windows.Foundation.Numerics) Quaternion (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) Type (System)

且有相同名稱和命名空間的類型

類型 Namespace
IPropertyValue Windows.Foundation
IReferenceArray<T> Windows.Foundation
Point 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 (System)

且有相同名稱和命名空間的類型

類型 Namespace
CornerRadius Microsoft.UI.Xaml
持續時間 Microsoft.UI.Xaml
DurationType Microsoft.UI.Xaml
GridLength Microsoft.UI.Xaml
GridUnitType Microsoft.UI.Xaml
Thickness Microsoft.UI.Xaml
GeneratorPosition Microsoft.UI.Xaml.Controls.Primitives
矩陣 Microsoft.UI.Xaml.Media
KeyTime Microsoft.UI.Xaml.Media.Animation
RepeatBehavior Microsoft.UI.Xaml.Media.Animation
RepeatBehaviorType Microsoft.UI.Xaml.Media.Animation
Matrix3D (Microsoft.UI.Xaml.Media.Media3D