You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cannot compile when using a Source Generated View-Model property with a TwoWay x:Bind #12073
area/code-generationCategorizes an issue or PR as relevant to code generationarea/xBind 🪢Categorizes an issue or PR as relevant to x:Binddifficulty/medium 🤔Categorizes an issue for which the difficulty level is reachable with a good understanding of WinUIhas-repro-branchkind/bugSomething isn't workingproject/binding 🪢Categorizes an issue or PR as relevant to the binding engine
OK not to question your response but if i create a standard WinUI3 project and add say a TextBox with the x:Bind mode=twoway it seems to work correctly.
Is there something changed in the xaml compiler for this that you guys have yet to implement.
The Microsoft WinUI toolchain does not use C# generators. It is able to introspect generated code because an intermediate assembly is generated then analyzed in a second pass.
Uno is not built using this generation sequence, and it's not something we can change easily.
There are multiple choices at this time:
Use {Binding }
Generate your partial classes based on T4 templates.
Use a generic base class for all your views, which implements your InitializeData methods and provides a ViewModel property.
Wait for Roslyn to implement generator dependencies, but it is not likely to happen soon.
A generic base view doesn't work. WPF had that limitation as well.
Would be cool if Uno could provide a hook for us to use our own generators that run first. Some specialized attribute that Uno could provide the context when its generator runs to call ours.
Indeed, that's correct, the generic base class does not work.
About having an extension, attributes probably would not cut it, though we've added internal support for MVVM Toolkit, and we've wondered if it would be useful for others to allow for extensibility points there.
@Youssef1313, that's the kind plug-in extensibility that may be useful, where a msbuild-provided registration of an additional externally-built assembly for ITypeProvider. I would guess that we'd return the generic parameter of the ViewAttribute type when TryGetType is called with the ViewModel property.
If an external assembly is provided via MSBuild, I think we will have to do some kind of assembly loading. At this point, this breaks the compiler, especially when UseSharedCompilation is true.
@jaredpar Sorry for the ping, but is there a way we can do assembly loading properly without breaking the compiler? The scenario here is we try to workaround dotnet/roslyn#57239. For our generator, all we need is the type of a generated symbol. For CommunityToolkit.Mvvm generators, we added this hack where we anticipate what will be generated. What we would like to do here is to let external consumers being able to plug in a similar implementation. I think this is not possible without loading external assemblies in the generator. So is there a way we can safely load such assemblies?
@jeromelaban could this source generator issue be added to the pinned Epic list? It is getting multiple opened issues like this one and adding this could simplify and centralize linking and lookup to it.
The tests install a small custom ISourceGenerator that emits a partial MainPage with a ViewModel property (simulating the user's custom source generator in the original repro project).
Observed behavior on current master, mirroring the bug report:
OneWay compiles cleanly — test passes.
TwoWay fails to compile with:
UXAML0001: Unable to find member 'ViewModel' on type 'TestRepro.MainPage'
Cascade CS1501 / CS1003 errors on __PreserveProperties in the generated MainPage_*.cs.
The XAML code generator's TwoWay setter path does not appear to resolve a property that is introduced by an unrelated source generator in the same compilation, even though the OneWay getter path works.
area/code-generationCategorizes an issue or PR as relevant to code generationarea/xBind 🪢Categorizes an issue or PR as relevant to x:Binddifficulty/medium 🤔Categorizes an issue for which the difficulty level is reachable with a good understanding of WinUIhas-repro-branchkind/bugSomething isn't workingproject/binding 🪢Categorizes an issue or PR as relevant to the binding engine
Activity
cconner100 commentedon May 3, 2023
any update on this item. It has me stuck now, i will have to re-write a lot of code if I cant use the generators. Really a high priority for me
jeromelaban commentedon May 3, 2023
This is a limitation of the C# generators (dotnet/roslyn#57239), unfortunately, as generators cannot depend on each other's output.
The only way around it is to explicitly provide properties in C# files before the compilation can happen, using T4 templates for instance.
Otherwise, you'll need to use standard
{Binding}expressions, for which the evaluation happens at runtime.alove-kahua commentedon May 3, 2023
I had hope considering it works in a OneWay bind. What makes it not work in the TwoWay scenario?
jeromelaban commentedon May 3, 2023
OneWaymostly works because the bound property does not need to be analyzed, while theTwoWayneeds type conversions for the code to work properly.cconner100 commentedon May 4, 2023
OK not to question your response but if i create a standard WinUI3 project and add say a TextBox with the x:Bind mode=twoway it seems to work correctly.
Is there something changed in the xaml compiler for this that you guys have yet to implement.
Take a look at https://github.com/cconner100/CommunityToolkitTest
I added the code generation to the SettingsViewModel and edited the SettingsPage
Yeah its not pretty but a quick test was all i was looking for
jeromelaban commentedon May 4, 2023
The Microsoft WinUI toolchain does not use C# generators. It is able to introspect generated code because an intermediate assembly is generated then analyzed in a second pass.
Uno is not built using this generation sequence, and it's not something we can change easily.
There are multiple choices at this time:
{Binding }InitializeDatamethods and provides aViewModelproperty.alove-kahua commentedon May 4, 2023
A generic base view doesn't work. WPF had that limitation as well.
Would be cool if Uno could provide a hook for us to use our own generators that run first. Some specialized attribute that Uno could provide the context when its generator runs to call ours.
jeromelaban commentedon May 5, 2023
Indeed, that's correct, the generic base class does not work.
About having an extension, attributes probably would not cut it, though we've added internal support for MVVM Toolkit, and we've wondered if it would be useful for others to allow for extensibility points there.
@Youssef1313, that's the kind plug-in extensibility that may be useful, where a msbuild-provided registration of an additional externally-built assembly for
ITypeProvider. I would guess that we'd return the generic parameter of theViewAttributetype whenTryGetTypeis called with theViewModelproperty.Youssef1313 commentedon May 5, 2023
If an external assembly is provided via MSBuild, I think we will have to do some kind of assembly loading. At this point, this breaks the compiler, especially when
UseSharedCompilationis true.@jaredpar Sorry for the ping, but is there a way we can do assembly loading properly without breaking the compiler? The scenario here is we try to workaround dotnet/roslyn#57239. For our generator, all we need is the type of a generated symbol. For CommunityToolkit.Mvvm generators, we added this hack where we anticipate what will be generated. What we would like to do here is to let external consumers being able to plug in a similar implementation. I think this is not possible without loading external assemblies in the generator. So is there a way we can safely load such assemblies?
Youssef1313 commentedon May 5, 2023
@alove-kahua For your specific scenario, would this hack work out?
and add
private MainViewModel? _viewModel => ViewModel;in page's code behind.DevTKSS commentedon Apr 20, 2025
@jeromelaban could this source generator issue be added to the pinned Epic list? It is getting multiple opened issues like this one and adding this could simplify and centralize linking and lookup to it.
test: Reproduce x:Bind TwoWay compile failure with source-generated V…
MartinZikmund commentedon Apr 13, 2026
Repro generator
Runtime test(s) added in branch
dev/mazi/repro/12073:src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Given_MvvmGeneratedMembers.cs→When_Page_Has_External_Source_Generated_ViewModel_Property_TwoWay_12073src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Given_MvvmGeneratedMembers.cs→When_Page_Has_External_Source_Generated_ViewModel_Property_OneWay_12073The tests install a small custom
ISourceGeneratorthat emits a partialMainPagewith aViewModelproperty (simulating the user's custom source generator in the original repro project).Observed behavior on current master, mirroring the bug report:
UXAML0001: Unable to find member 'ViewModel' on type 'TestRepro.MainPage'CS1501/CS1003errors on__PreservePropertiesin the generatedMainPage_*.cs.The XAML code generator's TwoWay setter path does not appear to resolve a property that is introduced by an unrelated source generator in the same compilation, even though the OneWay getter path works.