CommunityToolkit / CommunityToolkit/dotnet

.NET Framework (WPF) Source Generators not working (when in same project)

Open
#158 6 comments 2 reactions 0 assignees View on GitHub
external :arrow_heading_up: mvvm-toolkit :toolbox:
Dominant language
C#
Stars
3.8k
Forks
400
PR merge metrics
No merged PRs in 30d

Description

1.) Create a new WPF Framework app and flip on C# 10
2.) Create a simple view model:

```csharp
public partial class MainViewModel : ObservableObject
{
public MainViewModel()
{
}

[ObservableProperty]
[AlsoNotifyChangeFor(nameof(FullName))]
string firstName;

[ObservableProperty]
[AlsoNotifyChangeFor(nameof(FullName))]
string lastName;

public string FullName => $"{FirstName} {LastName}";

[ICommand]
async Task Submit()
{
Debug.WriteLine("DEBUG INFO: Submitted");
this.FirstName = "test";
}

}
```

The code will not compile and says that `FirstName` and `LastName` do not exist, even though there are no red squiggles and also the code is generated. The funny part is if you don't reference `FirstName` or `LastName` and only use `firstName` and `lastName` then the app will run and everything works...

Work around:
Create a .NET Standard 2.0 library, flip on C# 10, and then move the ViewModel over there and it will work great.

See: https://github.com/jamesmontemagno/MVVMSourceGenerators/ for reference

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.