Mapeamentos .NET de tipos do WinRT em C#/WinRT

Este artigo lista os mapeamentos que o C#/WinRT faz entre tipos do Windows Runtime (WinRT) e tipos .NET em aplicativos da área de trabalho direcionados ao .NET 6 (ou posterior). Nesses aplicativos, o IntelliSense do Visual Studio mostra o tipo .NET em vez do tipo de Windows Runtime. Por exemplo, caso um método do Windows Runtime utilize um parâmetro do tipo IVector<string>, o IntelliSense mostra um parâmetro do tipo IList<string>. Da mesma forma, em um componente do WinRT criado usando C#/WinRT, você usa o tipo .NET em assinaturas de membros. Quando você usa o C#/WinRT para gerar um componente do Windows Runtime, o tipo .NET é convertido no tipo WinRT correspondente.

Os mapeamentos de tipo personalizado do C#/WinRT são categorizados por tipos na SDK do Windows ou no WinUI 3 (WinUI 3 faz parte do SDK do aplicativo Windows). Os tipos de WinRT para mapeamentos de SDK do Windows residem nos namespaces Windows. * e os tipos de WinRT para os mapeamentos do WinUI 3 residem nos namespaces Microsoft. UI. Xaml. *. Há dois motivos para mapeamentos de tipo personalizado que o C#/WinRT faz para tipos de WinRT:

  • Tipos WinRT que são mapeados para tipos do .NET com um nome e/ou namespace diferente. Esses mapeamentos personalizados são para mapear tipos de WinRT para tipos equivalentes .NET existentes. Também há casos em que o mapeamento é para um tipo diferente (por exemplo, um tipo de valor é mapeado para um tipo de classe).

  • Tipos WinRT que são mapeados para tipos do .NET com o mesmo nome e namespace. Esses mapeamentos personalizados são geralmente para motivos de desempenho ou aprimoramento e são implementados diretamente no C#. A maioria dos tipos com o mesmo nome de namespace e o mesmo nome de tipo no WinRT e no .NET é de estruturas (ou tipos associados a estruturas, como enumerações). No WinRT, as estruturas não têm membros além de campos e exigem tipos de auxiliar, que são ocultados pelo .NET. As versões do .NET dessas estruturas têm propriedades e métodos que oferecem a funcionalidade dos tipos de elemento oculto (por exemplo, Windows.UI.Color).

Observação

Para obter uma lista de mapeamentos entre os tipos do WinRT e do .NET no contexto de aplicativos UWP, confira Mapeamentos do .NET dos tipos de WinRT no UWP.

Mapeamentos para tipos de WinRT no SDK do Windows

Tipos com nome e/ou namespace diferentes

Tipo/namespace do WinRT Tipo/namespace do .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)

Tipos com o mesmo nome e namespace

Tipo Namespace
IPropertyValue Windows.Foundation
IReferenceArray<T> Windows.Foundation
Point Windows.Foundation
Rect Windows.Foundation
Tamanho Windows.Foundation
Cor Windows.UI
TypeKind Windows.UI.Xaml.Interop

Mapeamentos para tipos de WinRT em WinUI

Tipos com nome e/ou namespace diferentes

Tipo/namespace do WinRT Tipo/namespace do .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)

Tipos com o mesmo nome e namespace

Tipo Namespace
CornerRadius Microsoft.UI.Xaml
Duration Microsoft.UI.Xaml
DurationType Microsoft.UI.Xaml
GridLength Microsoft.UI.Xaml
GridUnitType Microsoft.UI.Xaml
Espessura Microsoft.UI.Xaml
GeneratorPosition Microsoft.UI.Xaml.Controls.Primitives
Matriz 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