Improve information/experience around x:DataType behaviour in Debug vs Release
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:
- #20002
- #16414
- #11956
- #16021
- #19357