unoplatform / uno

Build Mobile, Desktop and WebAssembly apps with C# and XAML. Today. Open source and professionally supported.
https://platform.uno
Apache License 2.0
8.61k stars 694 forks source link

`x:Bind` not support the property generated by source generator #14409

Open SongOfYouth opened 7 months ago

SongOfYouth commented 7 months ago

Current behavior

I have generated the property via IIncrementalGenerator source generator like this: image

and bind it in the xaml file: image

then build, it comes out:

D:\Dysoft\Git\HIS3.0\Busi\Base\Dy.Base.UI\Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator\DictionaryTypePage_08d21259fdf0d2994045384693412e24.cs(111,407,111,605): error CS0029: Cannot implicitly convert type 'Dy.Base.UI.ViewModels.DictionaryTypeItemViewModel' to 'string'

Expected behavior

No response

How to reproduce it (as minimally and precisely as possible)

No response

Workaround

No response

Works on UWP/WinUI

Yes

Environment

No response

NuGet package version(s)

No response

Affected platforms

No response

IDE

Visual Studio 2022

IDE version

No response

Relevant plugins

No response

Anything else we need to know?

What i have bound to the Text of TextBox is property Code, but the error points to the ViewModel type converting fail.

jeromelaban commented 7 months ago

Thanks for the report. It seems that the code you're referencing is coming from a Source Generator. This is currently not supported because of https://github.com/unoplatform/uno/issues/12073, which is ultimately caused by https://github.com/dotnet/roslyn/issues/57239.

If you need this to work, you'll need to place your ViewModel classes in a separate library project from the XAML files.

SongOfYouth commented 7 months ago

Thanks for the report. It seems that the code you're referencing is coming from a Source Generator. This is currently not supported because of #12073, which is ultimately caused by dotnet/roslyn#57239.

If you need this to work, you'll need to place your ViewModel classes in a separate library project from the XAML files.

I think so too.but problem is the same code which from mvvmtoolkit source generator works well...

[ObeservaleProperty]
string _code;
jeromelaban commented 7 months ago

It works with MVVM Toolkit because Uno has a specific workaround for this library. It does not know about the other generators and can't generate code properly.

Youssef1313 commented 3 weeks ago

Best that can be done here is separating out the ViewModels to a separate project, if possible.

That way, the source generator will be run in the context of a project different than the one XAML generator runs in and things will work. I don't think this is currently fixable.

Youssef1313 commented 3 weeks ago

Another possible workaround is also suggested in https://github.com/unoplatform/uno/issues/12073#issuecomment-1536180900