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

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

Closed
cat0363 opened this issue Jun 10, 2024 · 5 comments
Closed

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

cat0363 opened this issue Jun 10, 2024 · 5 comments
Labels
area-xaml XAML, CSS, Triggers, Behaviors platform/android 🤖 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Milestone

Comments

@cat0363
Copy link
Contributor

cat0363 commented Jun 10, 2024

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

@cat0363 cat0363 added the t/bug Something isn't working label Jun 10, 2024
@jsuarezruiz jsuarezruiz added the area-xaml XAML, CSS, Triggers, Behaviors label Jun 10, 2024
Copy link
Contributor

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

@cat0363
Copy link
Contributor Author

cat0363 commented Jun 10, 2024

Additional Information :
{Binding .} and {Binding} do not work.

@Zhanglirong-Winnie Zhanglirong-Winnie added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Jun 11, 2024
@Zhanglirong-Winnie
Copy link

Verified this issue with Visual Studio 17.11.0 Preview 1.1 (8.0.40). Can repro on Android platform with sample project.

@jsuarezruiz jsuarezruiz added this to the Backlog milestone Jun 11, 2024
@simonrozsival
Copy link
Member

@cat0363 there's a bug in your code. There's a mismatch between the x:DataType (ViewModelMainPage) and the binding context that's applied to {Binding .} (ObservableCollection<ViewModelEmployee>). The code in the "workaround" is correct.

Unfortunately, in Debug mode the values of x:DataType are ignored and so the non-compiled bindings will show the expected result. The following PR will make the experience the same across both configurations: #22056

Is this explanation enough? Can I go ahead and close this issue?

@cat0363
Copy link
Contributor Author

cat0363 commented Jun 18, 2024

@simonrozsival , Thank you for your explanation.
I didn't know that x:DataType was ignored in Debug mode.
Thanks for letting me know that PR #22056 makes the experience the same for both configurations.
I agree to close this issue.

As a side note, I got the expected results by doing the following.

[MainPage.xaml]

    <Grid BindingContext="{Binding EmployeeList}">
        <StackLayout Orientation="Vertical" Spacing="0" VerticalOptions="Start" x:DataType="local:EmployeeCollection" 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>

[MainPage.xaml.cs]

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

        public MainPage()
        {
            InitializeComponent();

            BindingContext = VmMainPage;
        }
    }

[EmployeeCollection.cs]

    public class EmployeeCollection : ObservableCollection<ViewModelEmployee>;

[ViewModelMainPage.cs]

    public class ViewModelMainPage
    {
        public EmployeeCollection EmployeeList
        {
            get; set;
        }

        public ViewModelMainPage()
        {
            EmployeeList = new EmployeeCollection();
            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; }
    }

@simonrozsival simonrozsival closed this as not planned Won't fix, can't repro, duplicate, stale Jun 18, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jul 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-xaml XAML, CSS, Triggers, Behaviors platform/android 🤖 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants