Using compiled bindings
Default bindings let you get values from an object even if its type isn’t known beforehand. They just need to know the name of the property to bind to, and this is done using reflection, which pulls public properties from the object at runtime. As you may know, reflection isn’t the fastest process.
However, in most cases, we already know what object type will be used in BindingContext
. When we provide this type to .NET MAUI, it can get rid of reflection by creating a binding at compile time. For this, it’s sufficient to specify x:DataType
in your view or DataTemplate
:
<DataTemplate x:DataType="local:Item"> … </DataTemplate>
We’ve used this technique in previous recipes, so why revisit it in this one? Sometimes, you might not be able to use compiled bindings because the object in BindingContext
is unknown in advance or contains nested properties with unknown types. Understanding...