Table of Contents

Class ViewModelLocator

Namespace
Loehnert.Lisrt.Framework.Services
Assembly
Loehnert.Lisrt.dll

Represents a service for finding a view model for a model. To find a view model, a view model factory for the type must be exported. If a view model factory is not found, a recursive search for a view model factory of the base types is performed.

[Export(typeof(IViewModelLocator))]
public class ViewModelLocator : IViewModelLocator
Inheritance
ViewModelLocator
Implements
Inherited Members

Examples

Export a view model factory for your model type.

[Export(typeof(IViewModelFactory))]
internal class MyViewModelFactory : IViewModelFactory
{
    public Type ModelType => typeof(MyModel);

    public string Key => string.Empty;

    public object GetViewModel(object model, params object[] parameters)
    {
        return new MyViewModel((MyAdjustment)model);
    }
}

Locate the view model with the ViewModelLocator:

var model = new MyModel();
IoC.Get<IViewModelLocator>().GetViewModel(model);

Constructors

ViewModelLocator(IEnumerable<IViewModelFactory>)

Initializes a new instance of the ViewModelLocator class.

[ImportingConstructor]
public ViewModelLocator(IEnumerable<IViewModelFactory> viewModelFactories)

Parameters

viewModelFactories IEnumerable<IViewModelFactory>

Factories to create view models.

Exceptions

ArgumentNullException

Thrown when an argument is null.

ArgumentException

Thrown when an item of viewModelFactories is null, Key is null or ModelType is null.

Methods

GetViewModel(object, params object[])

Gets the view model for the model and Key is Empty.

public object GetViewModel(object model, params object[] parameters)

Parameters

model object

Model to locate the view model.

parameters object[]

Parameters for view model construction.

Returns

object

The located view model.

Exceptions

ArgumentNullException

Thrown when an argument is null.

GetViewModelByKey(string, object, params object[])

Gets the view model for the model and key.

public object GetViewModelByKey(string key, object model, params object[] parameters)

Parameters

key string

Key of the view model location.

model object

Model to locate the view model.

parameters object[]

Parameters for view model construction.

Returns

object

The located view model.

Exceptions

ArgumentNullException

Thrown when an argument is null.

Register(object, object)

Register an view model for a model.

public void Register(object model, object viewModel)

Parameters

model object

Model whose view model should be registered.

viewModel object

View model to register.

Exceptions

ArgumentNullException

Thrown when an argument is null.

Register(string, object, object)

Register an view model for a model and key.

public void Register(string key, object model, object viewModel)

Parameters

key string

Key of view model to register.

model object

Model whose view model should be registered.

viewModel object

View model to register.

Exceptions

ArgumentNullException

Thrown when an argument is null.