ViewModel Management

This article describes how to retrieve ViewModel instances at runtime. Note that if you utilize the MvvmContext component to build your MVVM-applications at design-time, the component manages ViewModels automatically.

If a ViewModel follows the POCO concept, the MVVM Framework dynamically transforms this ViewModel to a new class containing the necessary infrastructure (to support, for instance, simplified data bindings). The Framework works with dynamically created class instances, which means you cannot initially access these instances at runtime because their types have not been determined.

Use the following options to retrieve a working ViewMode:

The ViewModelSource.Create method

In this approach, you create a ViewModel instance first, then call the SetViewModel method to associate this instance with the specific ViewModel type.

You can inherit ViewModels from the ViewModelBase class that implements MVVM Framework features. In this case, you can create ViewModel instances directly. Note that you also need to call the SetViewModel method to specify that the Framework should use this instance when the ViewModel is required.

In this scenario, you need dynamically generated ViewModels and bind them to interfaces (MVVM framework features are lost when binding interfaces to POCO directly). To obtain the required instances, handle the regular (local) or static (global) ViewModelCreate event as follows: