Skip to content

{Binding .} doesn't work in release mode #22945

@cat0363

Description

@cat0363

Description

If you use {Binding .} when a child references a view model that is bound to a parent, it will bind as intended in debug mode, but will not bind correctly in release mode. The reproduction code is shown below.

Below is the screen layout.

[MainPage.xaml]

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Maui_IssueReleaseModeNotRendering"
             x:Class="Maui_IssueReleaseModeNotRendering.MainPage" x:DataType="local:ViewModelMainPage">

    <Grid BindingContext="{Binding EmployeeList}">
        <StackLayout Orientation="Vertical" Spacing="0" VerticalOptions="Start" BindableLayout.ItemsSource="{Binding .}">
            <BindableLayout.ItemTemplate>
                <DataTemplate x:DataType="local:ViewModelEmployee">
                    <Grid ColumnDefinitions="Auto,Auto,Auto" ColumnSpacing="5">
                        <Label Grid.Column="0" FontSize="14" TextColor="Black" VerticalOptions="Center" HorizontalOptions="Start" Text="{Binding EmployeeName}" />
                        <Label Grid.Column="1" FontSize="14" TextColor="Black" VerticalOptions="Center" HorizontalOptions="Start" Text="{Binding EmployeeAge}" />
                        <Label Grid.Column="2" FontSize="14" TextColor="Black" VerticalOptions="Center" HorizontalOptions="Start" Text="{Binding EmployeeSex}" />
                    </Grid>
                </DataTemplate>
            </BindableLayout.ItemTemplate>
        </StackLayout>
    </Grid>

</ContentPage>

Below is the view model used in the reproduction code.

[ViewModelMainPage.cs]

    public class ViewModelMainPage
    {
        public ObservableCollection<ViewModelEmployee> EmployeeList
        {
            get; set;
        }

        public ViewModelMainPage()
        {
            EmployeeList = new ObservableCollection<ViewModelEmployee>();
            EmployeeList.Add(new ViewModelEmployee() { EmployeeName = "Test1", EmployeeAge = 30, EmployeeSex = "Male" });
            EmployeeList.Add(new ViewModelEmployee() { EmployeeName = "Test2", EmployeeAge = 31, EmployeeSex = "Female" });
            EmployeeList.Add(new ViewModelEmployee() { EmployeeName = "Test3", EmployeeAge = 32, EmployeeSex = "Female" });
            EmployeeList.Add(new ViewModelEmployee() { EmployeeName = "Test4", EmployeeAge = 33, EmployeeSex = "Male" });
        }
    }

[ViewModelEmployee.cs]

    public class ViewModelEmployee
    {
        public string EmployeeName { get; set; }

        public int EmployeeAge { get; set; }

        public string EmployeeSex { get; set; }
    }

[MainPage.xaml.cs]

    public partial class MainPage : ContentPage
    {
        public ViewModelMainPage VmMainPage = new ViewModelMainPage();

        public MainPage()
        {
            InitializeComponent();

            BindingContext = VmMainPage;
        }
    }

Below is the result when running in debug mode.

[Debug mode]

Below is the result when running in release mode.

[Release mode]

If you build in release mode, {Binding .} is not working.

Is this way of writing not supported in release mode?

Steps to Reproduce

  1. Build the app uploaded to GitHub in release mode and launch it on Android.

In step 1, nothing is displayed in the BindableLayout.

Link to public reproduction project repository

https://github.com/cat0363/Maui-IssueReleaseModeNotRendering.git

Version with bug

8.0.40 SR5

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 11.0, 12.0, 13.0, 14.0

Did you find any workaround?

You can avoid this problem by explicitly specifying the view model without using {Binding .}.
For example, you can avoid this by doing the following:

    <Grid>
        <StackLayout Orientation="Vertical" Spacing="0" VerticalOptions="Start" BindableLayout.ItemsSource="{Binding EmployeeList}">
            <BindableLayout.ItemTemplate>
                <DataTemplate x:DataType="local:ViewModelEmployee">
                    <Grid ColumnDefinitions="Auto,Auto,Auto" ColumnSpacing="5">
                        <Label Grid.Column="0" FontSize="14" TextColor="Black" VerticalOptions="Center" HorizontalOptions="Start" Text="{Binding EmployeeName}" />
                        <Label Grid.Column="1" FontSize="14" TextColor="Black" VerticalOptions="Center" HorizontalOptions="Start" Text="{Binding EmployeeAge}" />
                        <Label Grid.Column="2" FontSize="14" TextColor="Black" VerticalOptions="Center" HorizontalOptions="Start" Text="{Binding EmployeeSex}" />
                    </Grid>
                </DataTemplate>
            </BindableLayout.ItemTemplate>
        </StackLayout>
    </Grid>

Relevant log output

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-xamlXAML, CSS, Triggers, Behaviorsplatform/androids/triagedIssue has been revieweds/verifiedVerified / Reproducible Issue ready for Engineering Triaget/bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions