Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve information/experience around x:DataType behaviour in Debug vs Release #21735

Closed
Redth opened this issue Apr 9, 2024 · 0 comments · Fixed by #22056
Closed

Improve information/experience around x:DataType behaviour in Debug vs Release #21735

Redth opened this issue Apr 9, 2024 · 0 comments · Fixed by #22056
Assignees
Labels
area-xaml XAML, CSS, Triggers, Behaviors fixed-in-8.0.80 fixed-in-9.0.0-preview.7.24407.4 t/bug Something isn't working
Milestone

Comments

@Redth
Copy link
Member

Redth commented Apr 9, 2024

Description

There can be differences in behaviour at runtime in Debug vs Release builds and using x:DataType.

For example, take the following view models (Note that they both have a Text property):

public partial class CorrectViewModel
{
    public string Text { get; set; } = "Correct Value";
}

public partial class ActuallyUsedViewModel
{
    public string Text { get; set; } = "Wrong Value";
}

Now with the following XAML:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:MauiApp1"
    x:DataType="local:CorrectViewModel"
    x:Class="MauiApp1.MainPage">

    <Label
        Text="{Binding TextProperty}" />

</ContentPage>

If we set the binding context in the page's code behind:

this.BindingContext = new ActuallyUsedViewModel();

Everything compiles fine. In a Debug build, the label shows the expected text Wrong Value despite the incorrect type on the instance assigned to the Binding context.

If you run a Release build however, the label has no text. Presumably the type cast fails at runtime as the instance assigned to the binding context does not match the x:DataType specified for this compiled binding, and this does not happen in Debug because there's no XAMLC step and so all the bindings are dynamic.

How do we improve the experience here?

Possibly related / duplicate:

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-xaml XAML, CSS, Triggers, Behaviors fixed-in-8.0.80 fixed-in-9.0.0-preview.7.24407.4 t/bug Something isn't working
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants