[Spec] ViewModel base class
See original GitHub issueProvide 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
which we could choose to use as is:
API
public abstract class ObservableObject: INotifyPropertyChanged
{
protected virtual bool SetProperty<T>(
ref T backingStore,
T value,
[CallerMemberName] string propertyName = "",
Action? onChanging = null,
Action? onChanged = null,
Func<T, T, bool>? validateValue = null)
{
// ...
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName]string propertyName)
=> /* ... */
}
Scenarios
C# Example
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?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:18
- Comments:9 (7 by maintainers)
Top Results From Across the Web
How to write a ViewModelBase in MVVM
So let's go step by step and build your own ViewModelBase class. ViewModelBase is class common for all your viewmodels. Let's move all...
Read more >ViewModelBase Class - Telerik UI for WPF - Documentation
The ViewModelBase abstract class is designed to serve as a base class for all ViewModel classes. It provides support for property change notifications....
Read more >ViewModelBase | WPF Controls
The ViewModelBase class is a BindableBase descendant. It inherits features of the BindableBase class (such as the GetProperty, SetProperty, ...
Read more >ViewModelBase class
It serves as base class for all ViewModel classes of the application and provides an implementation of the INotifyPropertyChanged interface in ...
Read more >ViewModelBase Class (Microsoft.TeamFoundation.MVVM)
Determines whether the specified object is equal to the current object. (Inherited from Object.) Protected method, Finalize, Allows an object to ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
MAUI is not going to be a traditional nuget package, rather an optional workload component for the dotnet SDK which is a bit different. There is a separation of MAUI core things and BindableObject is not considered a part of that, but rather part of MAUI Controls (and yes you will be able to tell your project you only want Core and not Controls referenced). This type of base class would exist in Controls, not core, as INPC is more closely tied to XAML/MVVM/DataBinding concepts that MAUI Controls uses. Things like Comet/MVU don’t necessarily require or make use of INPC so this doesn’t make sense in that layer.
I suggest if something really does need to be done for a better hot reload experience, let’s ensure it’s provided via interface… that way even if there is ultimately some base implementation out of the box, it doesn’t leave anyone out because they’re doing something else 😃