maui icon indicating copy to clipboard operation
maui copied to clipboard

XamlC Compilation errors when upgrading to Microsoft.Maui.Controls 8.0.20

Open konradzuse opened this issue 10 months ago • 8 comments

Description

Update the Nuget packages and compile the app, various compilation errors are reported for things like this...

<Style x:Key="Separator" TargetType="{x:Type BoxView}">
        <Setter Property="Color" Value="{StaticResource Gray-200}"/>
        <Setter Property="HeightRequest" Value="1"/>
        <Setter Property="Margin" Value="0,6"/>
        <Setter Property="HorizontalOptions" Value="FillAndExpand"/>
    </Style>

Error XFC0040 XamlC: Cannot convert value "Color" to "Microsoft.Maui.Controls.BindableProperty"

Other xaml files get comilation error..

Error XamlC: Input string was not in a correct format. Failure to parse near offset 69. Unexpected closing brace without a corresponding opening brace.

Despite them being perfectly valid Xaml and compiling correctly with 8.0.14

Steps to Reproduce

  1. Update Nuget packages to 8.0.20
  2. Compile app (Android/iOS)

Link to public reproduction project repository

No response

Version with bug

8.0.20 SR4

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

8.0.14 SR3.1

Affected platforms

iOS, Android

Affected platform versions

No response

Did you find any workaround?

No, removing the code presented as errors results in a new error reported in another file that was previously not reported.

Relevant log output

No response

konradzuse avatar Apr 10 '24 17:04 konradzuse

@konradzuse what is the code at offset 69? Is it complaining about TargetType="{x:Type BoxView}" or {StaticResource Gray-200}?

The diff has changes to {x:Type}, but not really any parsing logic I can tell:

  • https://github.com/dotnet/maui/compare/8.0.14...8.0.20

jonathanpeppers avatar Apr 10 '24 18:04 jonathanpeppers

@konradzuse what is the code at offset 69? Is it complaining about TargetType="{x:Type BoxView}" or {StaticResource Gray-200}?

There are 2 errors, one about my style binding to the BoxView Color property, and multiple others about closing braces at offset 69. Oddly every file it complains about is offset 69, and there's nothing to note at that offset.

konradzuse avatar Apr 10 '24 19:04 konradzuse

We think something went wrong here:

  • https://github.com/dotnet/maui/commit/4306566998747c0b2527b0258807942f0ea3138e

So, a workaround should be to use TargetType="BoxView" -- just removing the {x:Type} markup extension.

jonathanpeppers avatar Apr 10 '24 19:04 jonathanpeppers

Verified this issue with Visual Studio 17.10.0 Preview 3(8.0.20). Can repro it but not repro in Maui control version:8.0.14

ninachen03 avatar Apr 11 '24 03:04 ninachen03

I can confirm removing {x:Type} resolves the issues in both styles and xaml Content pages.

konradzuse avatar Apr 11 '24 08:04 konradzuse

Facing this issue in .NET 9 Preview 3 as well while using {x:Type} with DataTemplate.

Replacing {x:Type} with just its value resolves it for the time being.

egvijayanand avatar Apr 12 '24 00:04 egvijayanand

I get the same "Failure to parse near offset 69" error for DataTemplates that are inside a ContentPage ResourceDictionary that use x:Type

Something like:

<ContentPage.Resources>
        <ResourceDictionary>
            <DataTemplate x:Key="myTemplate" x:DataType="{x:Type myNamespace:someModel}">

The proposed workarounds don't resolve the issues in this case

TomSoPolaris avatar Apr 12 '24 14:04 TomSoPolaris

I take it back! The workaround does work. I had multiple DataTemplates on the same page.

Updated code:

<ContentPage.Resources>
        <ResourceDictionary>
            <DataTemplate x:Key="myTemplate" x:DataType="myNamespace:someModel">

TomSoPolaris avatar Apr 12 '24 14:04 TomSoPolaris

This fix should be available on our nightly feed if you use

8.0.21-preview.10505

https://github.com/dotnet/maui/wiki/Nightly-Builds

PureWeen avatar Apr 17 '24 18:04 PureWeen

This fix should be available on our nightly feed if you use

8.0.21-preview.10505

https://github.com/dotnet/maui/wiki/Nightly-Builds

Unfortunately this issue still persists in the current 8.0.21 see here.

In our case it even occurs in a simple case like:

<Setter Property="TextDecorations" Value="Strikethrough" />

Error XFC0040 Cannot convert value "TextDecorations" to "Microsoft.Maui.Controls.BindableProperty".

Delphinidae84 avatar May 08 '24 13:05 Delphinidae84

As of 8.0.40 I'm still getting the problem. 8.0.14 works fine.

RobTF avatar May 20 '24 19:05 RobTF

8.0.40 here, works with regular types ({x:Type someNamespace:SomeType}), at least with DataTemplate x:DataType (I'm unsure of Style TargetType and DataTrigger TargetType),

but not with nested types ({x:Type someNamespace:IAnInterface+ReturnType}).

Haukinger avatar May 27 '24 08:05 Haukinger

I'm also having this issue with version 8.0.40.

cat0363 avatar Jun 07 '24 02:06 cat0363

@RobTF @cat0363 could you please share an example of your failing XAML? I can't reproduce this problem with 8.0.40 anymore.

@StephaneDelcroix does x:Type support nested types? (cc @Haukinger)

simonrozsival avatar Jun 07 '24 13:06 simonrozsival

@simonrozsival , Below is an example where the problem occurred.

<Grid IsVisible="True">
    <DataTrigger TargetType="{x:Type Grid}" Binding="{Binding TestValue}" Value="1">
        <Setter Property="IsVisible" Value="False" />
    </DataTrigger>
</Grid>

When I changed the way to write TargetType of DataTrigger as shown below, the build error was resolved.

<Grid IsVisible="True">
    <DataTrigger TargetType="Grid" Binding="{Binding TestValue}" Value="1">
        <Setter Property="IsVisible" Value="False" />
    </DataTrigger>
</Grid>

cat0363 avatar Jun 10 '24 01:06 cat0363

@cat0363 I'm only getting MainPage.xaml(36,18): XamlC error XFC0009: No property, BindableProperty, or event found for "Children", or mismatching type between value and property. which is caused by missing <Grid.DataTriggers>, this fixed snippet works correctly:

<Grid IsVisible="True">
    <Grid.Triggers>
        <DataTrigger TargetType="{x:Type Grid}" Binding="{Binding TestValue}" Value="1">
            <Setter Property="IsVisible" Value="False"/>
        </DataTrigger>
    </Grid.Triggers>
</Grid>

simonrozsival avatar Jun 10 '24 14:06 simonrozsival

@simonrozsival , Sorry, I made a typo. <Grid.Triggers></Grid.Triggers> was missing when posting.

    <Grid IsVisible="True">
        <Grid.Triggers>
            <DataTrigger TargetType="{x:Type Grid}" Binding="{Binding TestValue}" Value="1">
                <Setter Property="IsVisible" Value="False" />
            </DataTrigger>
         </Grid.Triggers>
    </Grid>

It might be because I didn't clean the app when it was built. Maybe there was some garbage left over from the previous version. The build now works. Thank you for your comment.

cat0363 avatar Jun 11 '24 00:06 cat0363

@simonrozsival , I found a condition that causes a build error. An error occurs with the TargetType of DataTrigger, but it was a case where the DataTrigger existed inside the DataTemplate.

[MainPage.xaml]

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

    <StackLayout Orientation="Vertical" BindableLayout.ItemsSource="{Binding TestList}" Spacing="5">
        <BindableLayout.ItemTemplate>
            <DataTemplate x:DataType="{x:Type local:ViewModelTest}">
                <Grid BackgroundColor="Blue" HeightRequest="30">
                    <Grid.Triggers>
                        <DataTrigger TargetType="{x:Type Grid}" Binding="{Binding TestValue}" Value="0">
                            <Setter Property="BackgroundColor" Value="Red" />
                        </DataTrigger>
                    </Grid.Triggers>
                </Grid>
            </DataTemplate>
        </BindableLayout.ItemTemplate>
    </StackLayout>

</ContentPage>

By changing the TargetType of the above DataTrigger from {x:Type Grid} to Grid, the build will pass.

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

    <StackLayout Orientation="Vertical" BindableLayout.ItemsSource="{Binding TestList}" Spacing="5">
        <BindableLayout.ItemTemplate>
            <DataTemplate x:DataType="{x:Type local:ViewModelTest}">
                <Grid BackgroundColor="Blue" HeightRequest="30">
                    <Grid.Triggers>
                        <DataTrigger TargetType="Grid" Binding="{Binding TestValue}" Value="0">
                            <Setter Property="BackgroundColor" Value="Red" />
                        </DataTrigger>
                    </Grid.Triggers>
                </Grid>
            </DataTemplate>
        </BindableLayout.ItemTemplate>
    </StackLayout>

</ContentPage>

The code for MainPage.xaml.cs is shown below.

[MainPage.xaml.cs]

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

        public MainPage()        
        {
            InitializeComponent();
            this.BindingContext = pVmTest;
        }
    }

    public class ViewModelMainPage
    {
        public List<ViewModelTest> TestList { get; set; }

        public ViewModelMainPage()
        {
            TestList = new List<ViewModelTest>()
            {
                new ViewModelTest() { TestValue = 0 },
                new ViewModelTest() { TestValue = 1 },
                new ViewModelTest() { TestValue = 2 },
                new ViewModelTest() { TestValue = 3 }
            };
        }
    }

    public class ViewModelTest
    {
        public int TestValue { get; set; }
    }

When I investigated the location where the build error occurred, I found that it was when there was a DataTrigger in the DataTemplate as shown above.

cat0363 avatar Jun 11 '24 00:06 cat0363

@cat0363 can you also please share the error message you're getting?

simonrozsival avatar Jun 11 '24 06:06 simonrozsival

@simonrozsival , Thank you for your reply. Below is the error message I got.

MainPage.xaml(13,37): XamlC error XFC0040: Cannot convert value "BackgroundColor" to "Microsoft.Maui.Controls.BindableProperty".

cat0363 avatar Jun 11 '24 23:06 cat0363

@cat0363 thanks for the repro, I opened a bugfix PR: https://github.com/dotnet/maui/pull/23043

simonrozsival avatar Jun 13 '24 16:06 simonrozsival

@simonrozsival , thank you for opening a PR for the bug fix.

cat0363 avatar Jun 13 '24 22:06 cat0363

I take it back! The workaround does work. I had multiple DataTemplates on the same page.

Updated code:

<ContentPage.Resources>
        <ResourceDictionary>
            <DataTemplate x:Key="myTemplate" x:DataType="myNamespace:someModel">

This answer helped me.

I changed this code... <DataTemplate x:DataType="{x:Type dataTransferObjectsAddress:DistrictGetDTO}">

...for this: <DataTemplate x:DataType="dataTransferObjectsAddress:DistrictGetDTO">

Thank you very much @TomSoPolaris !

rodrigobarbosa1406 avatar Jul 07 '24 15:07 rodrigobarbosa1406