[Spec] ViewModel base class
- Dominant language
- C#
- Stars
- 23.3k
- Forks
- 2k
- Avg merge
- 1d 10h
- Merged PRs (30d)
- 297
Description
# Provide concrete implementation of INotifyPropertyChanged
MAUI should provide an out of the box base, concrete implementation of the `INotifyPropertyChanged` pattern with a minimally useful and prescriptive but not overly opinionated set of convenience methods.
This is useful for building view models, and other base models which use the INPC pattern. We currently have nothing out of the box, and pretty much any sample we ship or app we see in the wild has to implement this themselves. Adding something out of the box is very low cost and provides a better experience for new developers.
Xamarin Community Toolkit provides the type [`ObservableObject`](https://github.com/xamarin/XamarinCommunityToolkit/blob/main/src/CommunityToolkit/Xamarin.CommunityToolkit/ObjectModel/ObservableObject.shared.cs) which we could choose to use as is:
# API
```csharp
public abstract class ObservableObject: INotifyPropertyChanged
{
protected virtual bool SetProperty(
ref T backingStore,
T value,
[CallerMemberName] string propertyName = "",
Action? onChanging = null,
Action? onChanged = null,
Func? validateValue = null)
{
// ...
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName]string propertyName)
=> /* ... */
}
```
# Scenarios
## C# Example
```csharp
public class MyViewModel : Microsoft.Maui.Controls.ObservableObject
{
string name;
public string Name
{
get => name;
set => Set(ref name, value);
}
}
```
# Difficulty : low
# Questions?
- Should we include anything else?
Contributor guide
Assessment
This issue has not been assessed yet.