dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.64k stars 1.62k forks source link

XAML Compile Causes Failure at Run Time #22110

Open david-maw opened 2 weeks ago

david-maw commented 2 weeks ago

Description

The symptom was that some code stopped working but only in a Release build. The problem was obscured by DataTempltes, SwipeViews and general cruft, but it came down to XAML like this:

ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:XamlCompile_Binding"
             BindingContext="{local:MainViewModel}"
             x:DataType="local:MainViewModel"
             x:Class="XamlCompile_Binding.MainPage">

    <CollectionView ItemsSource="{Binding Path=People}">
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <VerticalStackLayout>
                    <Button Text="Press Me" HorizontalOptions="Start"
                    Command="{Binding Source={RelativeSource AncestorType={x:Type local:MainViewModel}}, Path= DoSomething}" 
                    CommandParameter="{Binding}"/>
                </VerticalStackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</ContentPage>

The symptom is that in a Release build the CommandParameter is null whereas in a Debug build the current Person object is passed.

I'm not convinced this is a bug at all, but if not it certainly can make for a surprising feature.

Steps to Reproduce

  1. Load up the project
  2. Build and run a Debug Build
  3. Click on a button, it will display a person's name.
  4. Build and run a Release build.
  5. Click on a button, it will display a message saying the command parameter was null.

Link to public reproduction project repository

https://github.com/david-maw/XamlCompile-Binding.git

Version with bug

8.0.10 SR3

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

Unknown/Other

Affected platforms

Android, Windows, I was not able test on other platforms

Affected platform versions

Windows 11, Android 14

Did you find any workaround?

Yes specifying the correct x:DataType for the DataTemplate (local:Person in this example).

Relevant log output

No response

RoiChen001 commented 2 weeks ago

Can repro this issue at Windows platform on the latest 17.10 Preview 5(8.0.7&8.0.21). 22110