Open echoix opened 1 year ago
I'd like to know if this issue was acknowledged and if there was any missing information for this bug, or any other blockers I should be aware of.
Hi @echoix
It took me a while to understand the problem but finaly I go it.
When the ObservableRecipientAttribute
is used, the Broadcast method is generated in the partial class itself. So it's not possible to override it directly for custom behavior.
The generated method has following comment:
/// <remarks>
/// You should override this method if you wish to customize the channel being
/// used to send the message (eg. if you need to use a specific token for the channel).
/// </remarks>
So, if you want ObservableValidator
and have custom behavior of the Broadcast, just give it an overridable class:
[ObservableRecipient]
public partial class MySecondObservableClassRecipient : ObservableValidator { }
public partial class MySecondObservableClass : MySecondObservableClassRecipient
{
[ObservableProperty]
[Range(1, 21)]
[NotifyPropertyChangedRecipients]
private int mySecondIntProperty;
protected override void Broadcast<T>(T oldValue, T newValue, string? propertyName)
{
// my broadcast customization
base.Broadcast(oldValue, newValue, propertyName);
}
}
best regards
Describe the bug
Documentation of
ObservableRecipient.Broadcast
indicate that Broadcast(...) should be overridden in order to use a custom channel (token). If we need to use the features ofObservableValidator
and need to use[NotifyPropertyChangedRecipients]
, then we need to use[ObservableRecipient]
on the class in order to have the missing pieces. I couldn't read online the documentation of theObservableRecipient.Broadcast
method generated by the attribute since the API docs for this dotnet CommunityToolkit aren't published on Microsoft Learn, only the old Windows Community Toolkit 7.0.0 are available (https://learn.microsoft.com/en-us/dotnet/api/communitytoolkit.mvvm.componentmodel.observablerecipient?view=win-comm-toolkit-dotnet-7.0) (and it didn't exist). So I referred to the XML docs in the SourceLink using F12 in Visual Studio.Regression
No response
Steps to reproduce
I made a new empty WPF project, and created a little sample pointing out what works and also the problem when wanting to add ObservableValidator in the mix.
Expected behavior
Be able to use the token feature with
[NotifyPropertyChangedRecipients]
when using anObservableValidator
class, thus I must be able to override theBroadcast
method.Screenshots
No response
IDE and version
VS 2022
IDE version
VisualStudio.17.Release/17.6.4+33815.320
Nuget packages
Nuget package version(s)
8.2.0
Additional context
In the source generator ObservableRecipientGenerator.cs, there is already some logic to not generate some methods if it already exists. https://github.com/CommunityToolkit/dotnet/blob/e071ed22b379f904820475db827be3ad04d3e96e/src/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservableRecipientGenerator.cs
What I would see as a solution would be to have the same kind of checks, for other methods, like
Broadcast
, and we would probably see this file have a new field in the record: https://github.com/CommunityToolkit/dotnet/blob/e071ed22b379f904820475db827be3ad04d3e96e/src/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/Models/ObservableRecipientInfo.csHelp us help you
No, just wanted to report this