Skip to content

Compiled bindings: document that x:DataType="x:Object" silently disables compiled bindings and explain XC0022/XC0025 warnings #3254

@PureWeen

Description

@PureWeen

Summary

The compiled bindings documentation does not call out that using x:DataType="x:Object" is an anti-pattern that silently disables compiled bindings and reverts to slower reflection-based binding resolution. This pattern appears frequently in code migrated from Xamarin.Forms and in XAML written without full understanding of x:DataType scoping rules.

Why it matters

Compiled bindings resolve at build time and are 8–20× faster than reflection-based bindings. When a developer adds x:DataType="x:Object" to escape a compiler warning or to "turn off" compiled bindings for a specific element, they lose all the performance benefits silently — there's no warning that they've done this.

This pattern also defeats the compile-time type safety that x:DataType is meant to provide. Binding errors that would be caught at build time (XC0022) become runtime null-reference exceptions instead.

Additionally, the compiler warnings XC0022 and XC0025 — which fire when compiled bindings are misconfigured — are not explained in the docs with actionable guidance. Developers encountering these warnings often silence them with x:DataType="x:Object" rather than fixing the root cause.

What should be documented

1. Call out x:DataType="x:Object" as an anti-pattern

Add to the compiled bindings docs:

Anti-pattern: Adding x:DataType="x:Object" to an element to suppress compiler warnings or escape type checking disables compiled bindings for that element and all descendants, silently reverting to reflection-based binding resolution (8–20× slower). Fix the underlying type mismatch instead of using x:Object as an escape hatch.

<!-- Correct: x:DataType only where BindingContext changes -->
<ContentPage x:DataType="vm:MainViewModel">
    <StackLayout>
        <Label Text="{Binding Title}" />
        <Slider Value="{Binding Progress}" />
    </StackLayout>
</ContentPage>

<!-- Anti-pattern: x:DataType="x:Object" disables compiled bindings -->
<ContentPage x:DataType="vm:MainViewModel">
    <StackLayout>
        <Label Text="{Binding Title}" />
        <Slider x:DataType="x:Object" Value="{Binding Progress}" />  <!-- reverts to reflection -->
    </StackLayout>
</ContentPage>

2. Explain the XC0022 and XC0025 compiler warnings with fix guidance

Warning Meaning Fix
XC0022 Binding path not found on the declared x:DataType Check property name spelling; verify the property is public on the declared type
XC0025 Binding used without x:DataType (non-compiled fallback active) Add x:DataType to the nearest ancestor where BindingContext is set

3. Recommend treating XC0022/XC0025 as errors in CI

<!-- In your .csproj -->
<PropertyGroup>
    <WarningsAsErrors>XC0022;XC0025</WarningsAsErrors>
</PropertyGroup>

Suggested location

docs/fundamentals/data-binding/compiled-bindings.md — add a "Common mistakes" or "Anti-patterns" section, and expand the compiler warnings table with actionable fix guidance

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions