-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Compiling fails when trying to use to ObservableProperty source generator property #4174
Comments
Hello Goz3rr, thank you for opening an issue with us! I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible. Other community members may also look into the issue and provide feedback 🙌 |
@Sergio0694 is the field allowed to be set a default value like this: [ObservableProperty]
private string foo = "bar"; I don't remember any of your examples with this. Was this a generation scenario you didn't think of? |
Note that I just did that for testing. Leaving the default value out has the same compiler error, just no way to easily see that it binds correctly at runtime |
@michael-hawker That is supported - source generators can only generate new code, so annotated fields will just remain exactly how they're defined, and using a field initializer like that is just standard C# syntax and therefore perfectly valid as well 🙂 This issue kinda strikes me as potentially being more about tooling in some way rather than about a bug in the source generator itself. The fact that not referencing the property will let the project build and run fine (as well as @Goz3rr having said that the generated code is fine and visible in solution explorer) makes me think it might just be some issue with the way the source generator are run by Roslyn when inside a WPF compilation. I'll see if I can repro this on my end 👍 EDIT: I can repro, and the generated code looks fine. I'm not convinced this is a bug in the MVVM Toolkit. |
So turns out the temporary fix is to just this to the .csproj file: <IncludePackageReferencesDuringMarkupCompilation>true</IncludePackageReferencesDuringMarkupCompilation> This is tracked in dotnet/wpf#3404, with the fix also being bundled with .NET 6 and the new .NET 5 SDK. Closing this as it's an external issue not related to the MVVM Toolkit itself. Thanks @Goz3rr for the report though! 😄 |
Thank you for the temporary fix 👍 |
Describe the bug
When I try to do anything with the property generated from the ObservableProperty source generator the compiler says it doesn't exist. Intellisense however does think it exists, and I can press F12 to inspect the generated code with the property. Binding to the generated property at runtime also works without any issues. No errors show up in the error list in VS either, only in build output.
Steps to Reproduce
Steps to reproduce the behavior:
Create a new WPF Application targeting .NET5.0 and add the Microsoft.Toolkit.Mvvm version 7.1.0-preview1 package from nuget.
Replace MainWindow.xaml with:
Create MainViewModel.cs:
Expected behavior
App compiles and shows "Hello World". Instead, VS reports build errors while none show up.
Going to Output -> Build reveals:
Commenting out the
Foo = "Hello World";
line makes the app compile fine and shows "bar", which suggests it successfully bound to the generatedFoo
property at runtime.Screenshots
Environment
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: