Pemetaan .NET dari jenis WinRT di C#/WinRT

Artikel ini mencantumkan pemetaan yang dilakukan C#/WinRT antara jenis Windows Runtime (WinRT) dan jenis .NET di aplikasi desktop yang menargetkan .NET 6 (atau yang lebih baru). Dalam aplikasi ini, Visual Studio IntelliSense menunjukkan jenis .NET alih-alih jenis Windows Runtime. Misalnya, jika metode Windows Runtime mengambil parameter jenis string> IVector<, maka IntelliSense menunjukkan parameter jenis string> IList<. Demikian pula, dalam komponen WinRT yang ditulis menggunakan C#/WinRT, Anda menggunakan jenis .NET dalam tanda tangan anggota. Saat Anda menggunakan C#/WinRT untuk menghasilkan komponen Windows Runtime, jenis .NET diterjemahkan ke dalam jenis WinRT yang sesuai.

Pemetaan jenis kustom C#/WinRT dikategorikan berdasarkan jenis di Windows SDK atau di WinUI 3 (WinUI 3 adalah bagian dari SDK Aplikasi Windows). Jenis WinRT untuk pemetaan Windows SDK hidup di bawah namespace Windows.* , dan jenis WinRT untuk pemetaan WinUI 3 hidup di bawah namespace Microsoft.UI.Xaml.* . Ada dua alasan untuk pemetaan jenis kustom yang dilakukan C#/WinRT untuk jenis WinRT:

  • Jenis WinRT yang memetakan ke jenis .NET dengan nama dan/atau namespace yang berbeda. Pemetaan kustom ini untuk memetakan jenis WinRT ke jenis setara .NET yang ada. Ada juga kasus di mana pemetaan adalah ke jenis yang berbeda (misalnya, jenis nilai memetakan ke jenis kelas).

  • Jenis WinRT yang memetakan ke jenis .NET dengan nama dan namespace yang sama. Pemetaan kustom ini umumnya untuk alasan performa atau peningkatan, dan diimplementasikan langsung di C#. Sebagian besar jenis yang memiliki nama namespace yang sama dan nama jenis di WinRT dan .NET adalah struktur (atau jenis yang terkait dengan struktur, seperti enumerasi). Di WinRT, struktur tidak memiliki anggota selain bidang, dan memerlukan jenis pembantu, yang disembunyikan .NET. Versi .NET dari struktur ini memiliki properti dan metode yang menyediakan fungsionalitas jenis pembantu tersembunyi (misalnya, Windows.UI.Color).

Catatan

Untuk daftar pemetaan antara jenis WinRT dan .NET dalam konteks aplikasi UWP, lihat pemetaan .NET jenis WinRT di UWP.

Pemetaan untuk jenis WinRT di Windows SDK

Jenis dengan nama dan/atau namespace yang berbeda

Jenis/namespace WinRT Jenis .NET/namespace
DateTime (Windows.Foundation) DateTimeOffset (Sistem)
EventHandler<T> (Windows.Foundation) EventHandler<T> (Sistem)
EventRegistrationToken (Windows.Foundation) EventRegistrationToken (WinRT)
HResult (Windows.Foundation) Pengecualian (Sistem)
IClosable (Windows.Foundation) IDisposable (Sistem)
IReference<T> (Windows.Foundation) Nullable<T> (Sistem)
TimeSpan (Windows.Foundation) TimeSpan (Sistem)
Uri (Windows.Foundation) Uri (Sistem)
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 (Sistem)
AttributeUsageAttribute (Windows.Foundation.Metadata) AttributeUsageAttribute (Sistem)
Matrix3x2 (Windows.Foundation.Numerics) Matriks3x2 (System.Numerics)
Matrix4x4 (Windows.Foundation.Numerics) Matriks4x4 (System.Numerics)
Plane (Windows.Foundation.Numerics) Plane (System.Numerics)
Quaternion (Windows.Foundation.Numerics) Quaternion (System.Numerics)
Vector2 (Windows.Foundation.Numerics) Vektor2 (System.Numerics)
Vector3 (Windows.Foundation.Numerics) Vektor3 (System.Numerics)
Vector4 (Windows.Foundation.Numerics) Vektor4 (System.Numerics)
IBindableIterable (Windows.UI.Xaml.Interop) IEnumerable (System.Collections)
IBindableVector (Windows.UI.Xaml.Interop) IList (System.Collections)
TypeName (Windows.UI.Xaml.Interop) Jenis (Sistem)

Jenis dengan nama dan namespace yang sama

Jenis Ruang nama
IPropertyValue Windows.Foundation
IReferenceArray<T> Windows.Foundation
Titik Windows.Foundation
Rect Windows.Foundation
Ukuran Windows.Foundation
Warna Windows.UI
TypeKind Windows.UI.Xaml.Interop

Pemetaan untuk jenis WinRT di WinUI

Jenis dengan nama dan/atau namespace yang berbeda

Jenis/namespace WinRT Jenis .NET/namespace
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 (Sistem)

Jenis dengan nama dan namespace yang sama

Jenis Ruang nama
CornerRadius Microsoft.UI.Xaml
Durasi Microsoft.UI.Xaml
DurationType Microsoft.UI.Xaml
GridLength Microsoft.UI.Xaml
GridUnitType Microsoft.UI.Xaml
Ketebalan Microsoft.UI.Xaml
GeneratorPosition Microsoft.UI.Xaml.Controls.Primitives
Matrix Microsoft.UI.Xaml.Media
KeyTime Microsoft.UI.Xaml.Media.Animation
RepeatBehavior Microsoft.UI.Xaml.Media.Animation
RepeatBehaviorType Microsoft.UI.Xaml.Media.Animation
Matriks3D (Microsoft.UI.Xaml.Media.Media3D