CommunityToolkit / CommunityToolkit/dotnet

Allow ObservableObject/INotifyPropertyChanged generator to work with interface specified

Open
#1,107 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
C#
Stars
3.8k
Forks
400
PR merge metrics
No merged PRs in 30d

Description

Currently, using `[ObservableObject]`/`[INotifyPropertyChanged]` generator in WinUI project causes warning MVVMTK0049/0050, since WinRT source generator can't see the implemented interface in MVVMTK source generator.

A workaround for this can be explicitly specify the interfaces in user written part, then they are visible to WInRT source generator:
```csharp
[INotifyPropertyChanged]
public abstract partial class MyControlBase : UserControl, INotifyPropertyChanged
{
}
```

However, when the MVVMTK generator sees the interface specification, it will think the interface is implemented by user and remove its generation. If we can force it to provide its generation, then the AOT warning should be addressed.

---

I also find a workaround by introducing another level of inheritance:

```csharp
[INotifyPropertyChanged]
public abstract partial class ObservableUserControl : UserControl
{
}

public partial class MyControl : ObservableUserControl, INotifyPropertyChanged
```

Then WinRT source generator will correctly see interfaces on `MyControl`, only `ObservableUserControl` is missing the interface info. Is it safe to ignore the warnings for abstract types?

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.