Skip to content
Philip Mørch edited this page Aug 9, 2022 · 1 revision

MvvmElegance is a simple, elegant and powerful ViewModel-First MVVM framework for Avalonia, inspired by Stylet. The relatively low lines of code count and modularity makes it an attractive option for any desktop application. The framework was built with Tasks and async/await in mind, which means most of the methods return a Task that can be awaited. It is split into two projects: a project you reference in your ViewModel-project and another you reference in your Avalonia-project. This way no view-types will ever hit your view models!

To get started using MvvmElegance check out the Quick Start guide on the right.

A brief outline of the features of MvvmElegance is shown below, with links that go to the respective pages.

The Screen and conductor classes provide features that make them suitable base classes: PropertyChanged notifications, automatic validation, notifications when shown/hidden/closed and controlling when and whether to close. They both inherit from ValidatingModelBase, which makes it easy to provide user input validation. Simply create a validation adapter for your favorite validation framework (e.g. FluentValidation) with the IModelValidator<T> interface.

Similar to those of Stylet, Actions allow you to call methods on your view models directly - without ICommand properties. This is to avoid representing button clicks or similar with properties instead of methods, which doesn't feel right. Instead it's a simple <Button Command="{e:Action DoSomething}"> that will call DoSomething() or DoSomething(param) on your view model. In addition, CanExecute and CanExecuteChanged have been replaced with a property named Can prefixed to a method name: for example CanDoSomething enables/disables the button that calls DoSomething(...). If your Action method returns a Task, the button, menu item, etc. will be disabled until the Task is marked as completed.

Tired of referencing windows or other view-types in your view models? With MvvmElegance's ViewModel-First approach, windows and dialog windows can be displayed by providing the view model. ViewService then finds out which Window type is associated with your view model using the IViewTypeLocator, constructs it, sets its DataContext and displays it. MvvmElegance also allows you to display file dialogs and a built-in message box in an MVVM-compatible way.

To provide user input validation, simply implement IModelValidator<T> as an adapter over your favorite validation framework (e.g. FluentValidation). Then, inherit from the base class ValidatingModelBase and it will take care of running your validations and reporting the results to the view. Both the Screen class and all conductors inherit from ValidatingModelBase.

With the IEventAggregator you can send messages between your view models. Simply subscribe to an event to receive notifications in a callback of your choice, and unsubscribe using the returned IDisposable. The EventAggregator is thread-safe, which means you can safely publish events from the thread pool or a dedicated thread. It also allows you to always run a callback on the UI thread, regardless of which thread the event was raised on.

Threading can be hard: sometimes you get events from the thread pool, sometimes it's in an asynchronous context and other times it's not. With MvvmElegance's Dispatch class it's easy to send work to be executed on the UI thread. It provides synchronous and asynchronous dispatcher methods, in addition to one that executes in-place or on-post depending on whether it's already on the UI thread. Dispatch also provides a TaskScheduler for easy UI-thread execution of ContinueWith callbacks on Tasks.

Clone this wiki locally