SimonAx
SimonAx

Reputation: 1378

How to use MvvmLight SimpleIoc in different project

Currently, I'm building a C# library, which will be used in different WPF applications. Such a library should be tested well so I'm using MVVM and Mvvm Light in particular. MvvM Light has a simple IOC that is initialized in static cstor of the class ViewModelLocator (at least when used out of the box). When I run my library, this static cstor is called. However, when referencing the library from a different WPF project called MyProject, the initialization is not carried out. The only work-around is to have a ViewModelLocator in MyProject, but that poses an unacceptable restriction on MyProject. Is there any way to have the static cstor of ViewModelLocator in a non-startup project be fired?

This question is not new, I've found similar formulations on SO, e.g. MVVMLight within another MVVMLight project and Register viewmodel in different project from main project viewmodellocatoe in mvvmlight. However, in both cases no answer has been suggested and now I'm wondering if someone with new insight can help a hand.

Thnx

Upvotes: 1

Views: 1123

Answers (1)

simon at rcl
simon at rcl

Reputation: 7344

This is a bit of a guess, but here goes....

The link between the ViewModelLocator and the View is in the XAML file, usually via a Static resource. Here's the definition of that resource e from one of my projects:

 <Application.Resources>
    <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" xmlns:vm="clr-namespace:Rcl.Reports.DataModelManager.ViewModel" />
  </Application.Resources>

Nore the full namespace path to the ViewModelLocator is required. When MVVM installs itself it generates this, and assumes that the ViewModelLocator will be in the main WPF project you've installed it in (and in which you need to install it, as the project will be directly be using MVVMLight).

If you have the wrong namespace, not the one of your library, then WPF can't find it and will silently do nothing.

Check that the namespace is defined correctly and see what happens.

Upvotes: 1

Related Questions