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?
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 :)
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.
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">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With