WPF View Model Generator - Prism & MVVM Light Support

WPF Team Blog
05 May 2022

View Models in complex views contain many properties, commands, and repeated interface implementations. Writing code for all these members is not only boring, but may result in typos and bugs. Fortunately, code writing evolves and new techniques allow you to save time, keep your code base compact, clean, and readable. Compile time generators are one kind of such techniques. The generators work seamlessly as you type code, so you don’t even need to build the project or execute any actions to get code generated.

Our MVVM Code Generator automatically creates commands based on methods, properties based on fields and implements such interfaces as INotifyPropertyChanged, INotifyPropertyChanging, IDataErrorInfo, ISupportServices and ISupportParentViewModel (see How to: Use View Models Generated at Compile Time). This is a good addition for users utilizing DevExpress MVVM Framework. Nevertheless, our components are also compatible with other MVVM frameworks and if you are using Prism or MVVM Light, you can still use our Code Generator as well.

Prism Code Generator

Our Prism View Model Code Generator helps you with all the basic view model functionality: commands, properties, and notifications. It uses Prism’s DelegateCommand class to generate commands. Apart from this, the generator can automatically implement Prism’s IActiveAware interface. It’s sufficient to assign GenerateViewModel to your model class and set ImplementIActiveAware to true:

Base View Model

[GenerateViewModel(ImplementIActiveAware = true)] 
partial class ViewModel { 
    // ... 
    void OnIsActiveChanged() {
        // ... 
    } 
} 

Generated View Model

partial class ViewModel : INotifyPropertyChanged, IActiveAware { 
    // ... 
    bool isActive; 
    public bool IsActive { 
        get => isActive; 
        set { 
            isActive = value; 
            OnIsActiveChanged(); 
            IsActiveChanged?.Invoke(this, EventArgs.Empty); 
        } 
    } 
    public event EventHandler? IsActiveChanged; 
    // ... 
} 

You will need to declare the DevExpress.Mvvm.CodeGenerators.Prism namespace to use the Prism code generator.

You can find additional information at Third-party Libraries Support: Prism. A complete example for this topic is available here: View Model Code Generator for PRISM.

MVVM Light Code Generator

In addition to standard view model functionality, our MVVM generator can automatically create a Messenger and implement the ICleanup interface:

Base View Model

[GenerateViewModel(ImplementICleanup = true)] 
partial class ViewModel { 
    // ... 
    void OnCleanup() { 
        // ... 
    } 
} 

Generated View Model

partial class ViewModel : INotifyPropertyChanged, ICleanup { 
    // ... 
    protected IMessenger MessengerInstance { get; set; } = Messenger.Default; 
    public virtual void Cleanup() { 
        MessengerInstance.Unregister(this); 
        OnCleanup(); 
    } 
    // ... 
} 

Declare the DevExpress.Mvvm.CodeGenerators.MvvmLight namespace to use View Model Generator with MVVM Light.

You can find additional information at Third-party Libraries Support: MVVM Light Toolkit. A complete example for this topic is available here: View Model Code Generator for MVVM Light.

To quickstart with code generators, use our MVVM Application Template.

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.
No Comments

Please login or register to post comments.