Search code examples
c#xamlmaui

MAUI compiled binding - whether to use x:Type or not


Could you explain what is the exact difference between x:DataType="{x:Type model:myclass}" and x:DataType="model:myclass"?

I've examined documentation and sources looking for correct code but have found examples of both kinds.


Solution

  • First of all, please check the official document about x:Type Markup Extension. The doc said:

    The x:Type markup extension can be used in object element syntax. In this case, specifying the value of the TypeName property is required to properly initialize the extension.

    The x:Type markup extension can also be used as a verbose attribute; however this use is not typical: <object property="{x:Type TypeName=typeNameValue}" .../>

    So the XAML Object Element Usage: <x:Type TypeName="prefix:typeNameValue"/> is always used. The x:DataType is complex property values that involve nested objects.

    And the official document about Compiled bindings in .NET MAUI also only uses x:DataType="model:myclass". So using x:DataType="model:myclass" is better.