Prism ViewLocator: How to Fix "Your Views Must Implement IView"

I am running a WPF application. I am using Prism and its ViewLocator .

This means that when I have a view for which a ViewModel is attached, I have to point mvvm:ViewModelLocator.AutoWireViewModel="True" and make it implement the IView interface that I made into the code behind.

In my small test application everything works fine, I get my ViewModel, it is installed in the DataContext of my view.

The problem is that I am using mvvm:ViewModelLocator.AutoWireViewModel="True" , I get this error in the "error list":

"Your Views Must Implement IView"

As far as I understand, the problem is that the xaml editor does not seem to check if my code implements this interface behind the class.

So how to avoid this error?

+7
c # wpf mvvm xaml prism
source share
3 answers

In fact, Prism 6, which has just been released, eliminates the need to have IView , so you no longer have this message :)

+3
source share

The XAML editor gives an error message when searching for your View model in XAML. Instead, you can do the job in code:

 public MainWindow() { InitializeComponent(); ViewModelLocationProvider.AutoWireViewModelChanged(this); } 

Then the error message is not displayed.

+1
source share

If you do not want to upgrade to Prism 6, here is the solution.

Take the source code of the ViewModelLocator class and in the AutoWireViewModelChanged method AutoWireViewModelChanged delete this line:

  // throw new Exception("Your views must implement IView"); 

Change the namespace of this class to your namespace:

 // namespace Microsoft.Practices.Prism.Mvvm namespace MyNamespace 

And in XAML use this property, not one from Prism:

 <Page xmlns:my="using:MyNamespace" my:ViewModelLocator.AutoWireViewModel="True"> 
+1
source share

All Articles