通过


在 C#/WinRT 中 .NET 的 WinRT 类型映射

本文列出了 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 类型进行自定义类型映射有两个原因:

  • 映射到具有不同名称和/或命名空间.NET类型的 WinRT 类型。 这些自定义映射用于将 WinRT 类型映射到现有.NET等效类型。 在某些情况下,映射可能涉及不同的类型(例如,将值类型映射到类类型)。

  • 映射到.NET中具有相同名称和命名空间类型的 WinRT 类型。 这些自定义映射通常出于性能或增强原因,直接在 C# 中实现。 WinRT 和 .NET 中具有相同命名空间名称和类型名称的大多数类型都是结构(或与结构(如枚举)关联的类型)。 在 WinRT 中,结构体除了字段以外没有其他成员,并且需要使用辅助类型,这些辅助类型被 .NET 隐藏。 这些结构的.NET版本具有提供隐藏帮助程序类型的功能的属性和方法(例如,Windows)。Ui。Color)。

注释

有关 UWP 应用上下文中 WinRT 和 .NET 类型之间的映射列表,请参阅 .NET Windows 运行时 类型的映射

Windows SDK 中 WinRT 类型的映射

具有不同名称和/或命名空间的类型

WinRT 类型/命名空间 .NET类型/命名空间
DateTime(Windows.Foundation) DateTimeOffset (系统)
EventHandler<T> (Windows.Foundation) EventHandler<T> (系统)
EventRegistrationToken (Windows.Foundation) EventRegistrationToken (WinRT)
HResult (Windows.Foundation) 异常(系统)
IClosable (Windows.Foundation) IDisposable (系统)
IReference<T> (Windows.Foundation) 可空的<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.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
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 (系统)

具有相同名称和命名空间的类型

类型 Namespace
CornerRadius Microsoft.UI.Xaml
持续时间 Microsoft.UI.Xaml
DurationType(持续时间类型) Microsoft.UI.Xaml
GridLength 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
重复行为类型 Microsoft.UI.Xaml.Media.Animation
Matrix3D Microsoft.UI.Xaml.Media.Media3D