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

XamlC XC0022 error : Binding could be compiled if x:DataType is specified. #20568

Closed
pijnappel opened this issue Feb 14, 2024 · 23 comments · Fixed by #20578
Closed

XamlC XC0022 error : Binding could be compiled if x:DataType is specified. #20568

pijnappel opened this issue Feb 14, 2024 · 23 comments · Fixed by #20578
Labels
area-xaml XAML, CSS, Triggers, Behaviors fixed-in-8.0.10 fixed-in-9.0.0-preview.2.10293 i/regression This issue described a confirmed regression on a currently supported version platform/android 🤖 t/bug Something isn't working
Milestone

Comments

@pijnappel
Copy link

Description

MAUI 8.0.7 reports the Errors XC0022 and XC0023 as errors, not as warnings.
I have treat warnings as error active, but <WarningsNotAsErrors>XC0022,XC0023</WarningsNotAsErrors> does not help.

Steps to Reproduce

Create a Binding, leave out x:DataType or set x:DataType to {x:Null}

Link to public reproduction project repository

No response

Version with bug

8.0.7 SR2

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

8.0.6 SR1

Affected platforms

Android, I was not able test on other platforms

Affected platform versions

No response

Did you find any workaround?

Stay with 8.0.6 for the moment

Relevant log output

No response

@pijnappel pijnappel added the t/bug Something isn't working label Feb 14, 2024
@StepKie
Copy link

StepKie commented Feb 14, 2024

We are facing the same issue, and it is breaking our previously successful builds, since we also treat warnings as errors to keep our code base clean.

Lots of warnings with this error message.
For me, no error code is displayed, so I dont know how you know that these are XC0022 and XC0023, @pijnappel.

We have no way to fix or ignore this warning via project file or editorconfig.

We can not fix all instances, as we often bind to ViewModels which use generics like MyViewModel<T>, where binding with x:DataType is not really feasible, or not documented how to do it. If anybody has useful links/hints for this, please share.

Very bold decision to just unleash hundreds of warnings without an error code that makes it configurable.

image

@drasticactions
Copy link
Contributor

If you search the string it comes up here:

public static BuildExceptionCode BindingWithoutDataType = new BuildExceptionCode("XC", 0022, nameof(BindingWithoutDataType), ""); //warning
public static BuildExceptionCode BindingWithNullDataType = new BuildExceptionCode("XC", 0023, nameof(BindingWithNullDataType), ""); //warning

Which is related to:
#19360

Going by it, it does have an error code that should appear, <WarningsNotAsErrors> should have worked. It's also used in the source repo for the compatability examples (

<WarningLevel>4</WarningLevel>
<NoWarn>0114;0108;0109;4014;0649;0169;0472;0414;0168;0219;0429;0618;0612</NoWarn>
<WarningsNotAsErrors>$(WarningsNotAsErrors);XC0022;XC0023</WarningsNotAsErrors>
)

@StephaneDelcroix What do you think?

@StephaneDelcroix
Copy link
Contributor

We use <WarningsNotAsErrors/> extensively in our code. What I see is that you use a comma ',' as separator, where we expect a semicolon ';'

Could you please try and see if it helps ?

Reading the doc again, it seems CSC allows both as separators. Fixing this...

@pijnappel
Copy link
Author

I was going by the documentation and didn't think this could be the problem.
Semicolon ';' worked. These codes are now treated as warnings.

@StephaneDelcroix
Copy link
Contributor

Screenshot 2024-02-14 at 15 13 59 the documentation is kinda funny

StephaneDelcroix added a commit that referenced this issue Feb 14, 2024
@StephaneDelcroix
Copy link
Contributor

@Hottemax we recommend that you don't ignore the warnings, but you can do it by setting <NoWarn>$(NoWarn);XC0022;XC0023</NoWarn>

@StepKie
Copy link

StepKie commented Feb 14, 2024

@Hottemax we recommend that you don't ignore the warnings, but you can do it by setting <NoWarn>$(NoWarn);XC0022;XC0023</NoWarn>

I did that - already after your first link to the Maui.Compatibility repo sample in your initial reply.
Thanks a lot for the quick help!

@UnusualJustin
Copy link

UnusualJustin commented Feb 14, 2024

@Hottemax we recommend that you don't ignore the warnings, but you can do it by setting <NoWarn>$(NoWarn);XC0022;XC0023</NoWarn>

@StephaneDelcroix, I agree that this is a useful finding and would be better not to ignore, but is there a way in Maui xaml to specify a datatype for a generic type?

@samhouts samhouts added potential-regression This issue described a possible regression on a currently supported version., verification pending platform/android 🤖 labels Feb 14, 2024
@daniel-smith-sonepar
Copy link

@Hottemax we recommend that you don't ignore the warnings, but you can do it by setting <NoWarn>$(NoWarn);XC0022;XC0023</NoWarn>

@StephaneDelcroix, I agree that this is a useful finding and would be better not to ignore, but is there a way in Maui xaml to specify a datatype for a generic type?

Yes we have same issue of using generic types or interfaces to ensure specific properties exist on an object that is bound via XAML. Not sure how to handle this situation with this new warning.

@StepKie
Copy link

StepKie commented Feb 14, 2024

@StephaneDelcroix, I agree that this is a useful finding and would be better not to ignore, but is there a way in Maui xaml to specify a datatype for a generic type?

Yes we have same issue of using generic types or interfaces to ensure specific properties exist on an object that is bound via XAML. Not sure how to handle this situation with this new warning.

Yeah, it would be really nice if we could gain some insight on how to achieve this from this issue. I really felt bad not having compiled bindings in these cases, but found no easy way to do it. All the examples we found were always just something along the lines of how to specify the generic type on CollectionView bindings or something similar.

@StephaneDelcroix
Copy link
Contributor

generics in XAML are expressed like this KeyValuePair(x:String, x:String). This works for TypeArguments but as I haven't found any test, I can't guarantee it works for x:DataType. If it does not, report an issue, make sure you ping me, and I'll look into that.

@StepKie
Copy link

StepKie commented Feb 14, 2024

generics in XAML are expressed like this KeyValuePair(x:String, x:String). This works for TypeArguments but as I haven't found any test, I can't guarantee it works for x:DataType. If it does not, report an issue, make sure you ping me, and I'll look into that.

@StephaneDelcroix Thanks for your reply!

I have read https://learn.microsoft.com/en-us/dotnet/maui/xaml/generics?view=net-maui-8.0, and I don't exactly understand what you want me to try out/test. What would be the XAML syntax if I want to bind to lets say a ViewModel of type MyViewModel<MyModel>?

Currently what we do is have a file where we define dozens of "dummy" classes like this:

public class ArticleViewModel : BoViewModel<BoArticle> { }
public class ArticleListViewModel : CardListViewModel<BoArticle> { }

public class PebSampleListViewModel : SamplesListViewModel<BoPebSample> { }
public class EmployeeListPageViewModel : CardListViewModel<BoEmployee> { }

and then we can do

x:DataType="viewmodels:ArticleViewModel", and this works ok

But is there any way to avoid this "dummy class", and just bind to x:DataType="viewmodels:BoViewModel" x:TypeArguments="models:BoArticle" directly?

Because this kind of syntax is not working. Or can you illuminate what you want us to try?

Thanks for your patience!

@StephaneDelcroix
Copy link
Contributor

StephaneDelcroix commented Feb 14, 2024

@Hottemax the syntax (again, I can guarantee it works) would be x:DataType="viewmodels:ArticleViewModel(models:BoArticle)"

@daniel-smith-sonepar
Copy link

daniel-smith-sonepar commented Feb 14, 2024

@StephaneDelcroix - based on example provided by @Hottemax ... wouldn't the syntax be: x:DataType="viewmodels:BoViewModel(models:BoArticle)"

@StepKie
Copy link

StepKie commented Feb 14, 2024

Unfortunately this does not work for me.

Error XFC0000 Cannot resolve type "clr-namespace:MyApp.ViewModels:viewmodels:BoViewModel(models:BoArticle)".

if anyone has a link into some sample repo where I can observe this kind of syntax working, it would be greatly appreciated.

@jeremy-visionaid
Copy link
Contributor

jeremy-visionaid commented Feb 14, 2024

I'm hitting this issue too. In my case I couldn't easily tell what x:Type should be... For Shell ItemTemplate bindings, the documentation merely says

Shell provides the Title and FlyoutIcon properties to the BindingContext of the ItemTemplate.

In 8.0.6 I'd had the DataType set to FlyoutItem, and this appeared to be working (I thought that it was being compiled OK).
In 8.0.7 it compiled but did not work at run time (the text and items were blank). It seems like the secret sauce was actually x:DataType="BaseShellItem".

So, it seems there's a bug here in that if the binding compile then I expect they should work at run time... But also a possible improvement to be made, if xamlc knows that a binding could be compiled, then doesn't it already know what the type should be? It could maybe give us a hint to save us some time in fixing the warnings. Or is this just a blanket warning for all bindings that aren't compiled?

@StepKie
Copy link

StepKie commented Feb 15, 2024

@StephaneDelcroix, according to your offer, should I create an issue for this now, and tag-mention you? Just want to make sure before doing so that this is a real issue and I am not doing something obvious wrong with

x:DataType="viewmodels:BoViewModel(models:BoArticle)"

@StephaneDelcroix
Copy link
Contributor

@Hottemax please do

@durandt
Copy link

durandt commented Mar 5, 2024

@Hottemax we recommend that you don't ignore the warnings, but you can do it by setting <NoWarn>$(NoWarn);XC0022;XC0023</NoWarn>

@StephaneDelcroix Could you point to a piece of documentation or issue/thread describing why XC0022/XC0023 should be avoided? (Or explain here). The warning itself "Binding could be compiled if x:DataType is specified" does not say much.

@durandt
Copy link

durandt commented Mar 5, 2024

(Also, not confirmed yet, but I get a LOT of warnings although i defined x:DataType almost everywhere and I suspect the XAML code inside toolkit:StateContainer.StateViews (xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit") gets flagged with the warning even though x:DataType is declared on the outside)

@durandt
Copy link

durandt commented Mar 6, 2024

Confirmed the reason why I get many of these warnings is that I am using CommunityToolkit's StateContainer.

Not sure if it is an issue to report here or on the CommunityToolkit's repo, or even if it is an issue.

Repro at: https://github.com/durandt/maui-issue6-xaml-datatype-warnings

Example:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             xmlns:vm="clr-namespace:maui_issue6_xaml_datatype_warnings.ViewModels"
             x:Class="maui_issue6_xaml_datatype_warnings.SecondaryPage"
             x:DataType="vm:PageViewModel">

    <VerticalStackLayout
        toolkit:StateContainer.CurrentState="{Binding CurrentState}"
        toolkit:StateContainer.CanStateChange="{Binding CanStateChange}">

        <toolkit:StateContainer.StateViews>
            <VerticalStackLayout toolkit:StateView.StateKey="Init">
                <ActivityIndicator IsRunning="True" HorizontalOptions="Center" />
                <Label Text="Loading Content..." HorizontalOptions="Center" />
                <Button
                    Text="Switch to next state"
                    Command="{Binding SwitchStateCommand}"
                    HorizontalOptions="Fill" />
            </VerticalStackLayout>
            <!-- More state views -->
        </toolkit:StateContainer.StateViews>
    </VerticalStackLayout>
</ContentPage>

I can get rid of the warning if I specify the x:DataType again at the top of the state view:

<VerticalStackLayout toolkit:StateView.StateKey="Init" x:DataType="vm:PageViewModel">

@SunshineSpring666
Copy link

Such issue began to appear again in latest Visual Studio preview with DOTNET 9 preview.

@StepKie
Copy link

StepKie commented May 2, 2024

Hi @StephaneDelcroix,

I can confirm it works in the current MAUI release (I get a compile-time error if the property is not found when x:DataType is specified.

However, it would be nice if the support in the XAML editor can be improved
(I can still not F12 to the bound property in the source code, and properties are not offered with Ctrl+Enter code completion, which is, besides the compile-time safety of ensuring the bound property exists, one of the main reasons to use this feature

image

@samhouts samhouts added this to the .NET 8 SR3 milestone May 15, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jun 15, 2024
@samhouts samhouts added i/regression This issue described a confirmed regression on a currently supported version and removed fixed-in-8.0.14 fixed-in-8.0.40 fixed-in-9.0.0-preview.2.10247 potential-regression This issue described a possible regression on a currently supported version., verification pending labels Jul 1, 2024
@samhouts samhouts added the area-xaml XAML, CSS, Triggers, Behaviors label Aug 28, 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 fixed-in-8.0.10 fixed-in-9.0.0-preview.2.10293 i/regression This issue described a confirmed regression on a currently supported version platform/android 🤖 t/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants