View Model Base
Overview
ViewModelBase is the fundamental abstract class for all view models in the Asv.Avalonia framework. It implements the IViewModel interface and provides core features for property notification, resource management, and logging.
Key Features:
Property Change Notifications: implements
INotifyPropertyChanged.Thread-Safe Disposal: a robust disposal pattern ensuring resources are released exactly once.
Navigation Support: implements
ISupportId<NavigationId>to support argument initialization and routing.
Core Components
NavigationId
The IViewModel interface derives from the ISupportId<NavigationId> interface, which contains the Id property (of type NavigationId). It serves as the unique identifier for the view model instance. It's essential for:
Identifying: Distinguishing ViewModels within the application.
Navigation: Providing the path and parameters for routing (especially in
RoutableViewModel).
You can initialize it with custom arguments using the InitArgs method.
Property Notification
IViewModel inherits from INotifyPropertyChanged.
To update properties, use the SetField method. It updates the backing field and automatically raises the PropertyChanged event only when the value has actually changed, preventing unnecessary UI updates.
Disposal Pattern
ViewModelBase implements a thread-safe disposal mechanism. While the base class itself holds no heavy resources, it establishes the pattern for all derived classes:
The public
Dispose()method handles thread safety and prevents multiple disposal calls.Derived classes should override the
Dispose(bool disposing)method to perform actual cleanup (e.g., unsubscribing from events or stopping timers).
API
IViewModel
Defines a base contract for all view models in the application. This interface provides a unique identifier, supports property change notifications, and ensures proper disposal of resources.
Property | Type | Description |
|---|---|---|
|
| Gets a value indicating whether the view model has been disposed. |
Method | Return Type | Description |
|---|---|---|
|
| Initializes a navigation ID with the arguments. |
ViewModelBase: IViewModel
Represents the base implementation of a view model that provides property change notifications and a proper disposal mechanism. This class is designed to be inherited by other view models.
Property | Type | Description |
|---|---|---|
|
| A unique ViewModel ID. |
|
| A logger. |
Event | Type | Description |
|---|---|---|
|
| Occurs when a property value changes. Implements |
Method | Return Type | Description |
|---|---|---|
|
| Releases resources used by the view model. Ensures that the disposal operation is only performed once. |
|
| Initializes Navigation ID with args. |
|
| Releases managed resources when disposing. This method must be implemented by derived classes to handle resource cleanup. |
|
| Throws an |
|
| Sets the field to the specified value and raises the |
ViewModelBase.Dispose(bool disposing)
Parameter | Type | Description |
|---|---|---|
|
|
|
ViewModelBase.SetField<T>
Parameter | Type | Description |
|---|---|---|
|
| The backing field reference. |
|
| The new value to set. |
|
| The name of the property that changed. Automatically set by the caller if not provided. |