Question about differences in the behavior of [ObservableCollection] and [ObservableProperty] I have worked with WPF some times and now I´m moving into Avalonia. A lot of things will work as expected and very well, but I´m stucked with the CodeGenerator [ObservableProperty] Either I'm completely wrong or blind or something isn't as I expected in the Avalonia Framework. Following the great explanation from Julians text , especially under the hood.
quote: ...Once the backing field was updated, the PropertyChanged event is raised,..... ...there are not just OnPropertyChanging() and OnPropertyChanged() method calls.....
As far I understand, the generated code calls automatically a method called OnPropertyChanged and raise the Event PropertyChanged. But both of them never occurs. Am I on the wrong way? It's not the problem to wrote more lines by myself......but Source Generator is supposed to help..
The TextBox in my View
<TextBox Width="200" Height="200" Margin="50" Background="Beige" Text="{Binding TestTextBox}">
The ViewModel partial part1
public partial class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
}
the ViewModel part2
public partial class MainViewModel : ObservableObject
{
[ObservableProperty]
private string _testTextBox;
/* This will work fine
partial void OnTestTextBoxChanged(string testTextBox)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("TestTextBox"));
}
*/
// these method isn't call
public void OnPropertyChanged([CallerMemberName] string name = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
I have read all about ObservableProperty I have found so far...especially Julians Blog Entry. Is this an Avalonia issue?
You don't need to invoke the PropertyChanged event yourself, this is already done as part of the generated code. (You can see this generated code if using visual studio, open the Anaylzers from the solution explorer, open CommunityToolkit.Mvvm.SourceGenerators, and then open the CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator and find your class).
By implementing CommunityToolkit you are saving yourself the trouble of doing all this boiler plate code. If you need to respond to your own PropertyChanged event, it is protected virtual and you can override it.
However, in this case you still DO NOT need to invoke PropertyChanged as this is done by the base implementation.
If you just need to react to a specific property changed, you already figured out you can do that:
However in this case, once again, you DO NOT need to invoke PropertyChanged. This is all done for you and is the real point of using CommunityToolkit.MVVM.
In fact if you don't need to react to or override the events at all...
... and you're done. I know that seems too easy, but that's the beauty of the CommunityToolkit.MVVM package.
Oh also, the following code...
You don't need that either. ObservableObject already implements INotifyPropertyChanged, and anything that a partial class implements is also implemented by ALL partial declarations automatically.
If you are still struggling with the concept, I cannot reccomend James Montemagno's videos enough. He takes an old MVVM setup and converts it to the generators making it very clear what is happening. He does it in WPF, but it's the same in Avalonia.
MVVM Source Generators
More MVVM Source Generators