Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prism ViewLocator : How to fix "Your views must implement IView"

Tags:

c#

mvvm

wpf

xaml

prism

I'm starting a WPF application. I use Prism and its ViewLocator.

It implies that when I've a View that has to have a ViewModel bound to it, I've to indicate on it mvvm:ViewModelLocator.AutoWireViewModel="True"and make it implements the IView interface, which I did, in the code behind.

In my small test application, every thing works fine, I get my ViewModel, it is set to the DataContext of my View.

The issue is that everyplace I'm using the mvvm:ViewModelLocator.AutoWireViewModel="True", I get this error in the "Error List":

"Your views must implement IView"

From my understanding, the issue is that the xaml editor doesn't seems to check if my code behind class implement this interface.

So how to avoid this error?

like image 422
J4N Avatar asked Aug 19 '15 14:08

J4N


3 Answers

In fact Prism 6, which has just been released yet, remove the need of having an IView, so you don't have this message anymore :)

like image 162
J4N Avatar answered Nov 14 '22 00:11

J4N


The XAML Editor gives an error message when you locate your View Model in XAML. You can instead do the job in code behind:

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

Then no error message is shown.

like image 2
dytori Avatar answered Nov 14 '22 01:11

dytori


If you don't want to upgrade to Prism 6, here's the solution.

Take the source code of the ViewModelLocator class and in the AutoWireViewModelChanged method remove 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 instead of the one from Prism:

<Page 
   xmlns:my="using:MyNamespace"
   my:ViewModelLocator.AutoWireViewModel="True">
like image 2
Artemious Avatar answered Nov 14 '22 01:11

Artemious