6
Vote

ViewModelLocator.AutoWireViewModel says view must implment IView when it already does.

description

When you set ViewModelLocator.AutoWireViewModel="True" in xaml you get the error "Your views must implement IView" even when your view already has IView set.

This can be seen in the BasicMVVMQuickstart_Desktop project in the MainWindow.xaml

Image

This does not prevent you from building nor does it give a error at runtime.


The source of the issue is from ViewModelLocator.AutoWireViewModelChanged
    private static void AutoWireViewModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var view = d as IView;

        if (view == null)
        {
            // TODO: Improve exception
            throw new Exception("Your views must implement IView");
        }

        ViewModelLocationProvider.AutoWireViewModelChanged(view);
    }
the casting d as IView returns null.

It appears that the code behind is not processed during design time when the attached property is evaluated. So if you have public partial class MyClass: UserControl, IView and you do d.GetType() inside AutoWireViewModelChanged you will not get the type MyClass but UserControl instead and therefor the interface IView will not be found.

comments