WPF MVVM 导航

WPF MVVM Navigation

我试图在登录后在我的主窗口中实现 MVVM 导航,在此之前,在单击 'Login' 按钮后,我必须调用 MainWindow.xaml 进行显示,之后我用于导航在我的主窗口中基于 menu/ribbon 选择。

以下是我目前所做的实现:

在 'Login' 按钮命令上:

private void Entry(object parameter)
    {
        IMainWindowViewModel viewM = new MainWindowViewModel();
        ViewBinder<IMainWindowView> main = new ViewBinder<IMainWindowView>(viewM);
        var view = main.View;
    }

MainWindowViewModel:

public class MainWindowViewModel:ViewModel<IMainWindowView>, IMainWindowViewModel
{

    public int EmpID
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    public string EmpName
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    public void GetEmployees()
    {
        throw new NotImplementedException();
    }
public object DataContext
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    public MainWindowViewModel(IMainWindowView view)
        : base(view)
    { }
}

IMainWindowViewModel:

public interface IMainWindowViewModel:IMainWindowView
{
    int EmpID { get; set; }
    string EmpName { get; set; }
    void GetEmployees();
}

IMainWindowView:

public interface IMainWindowView:IView
{
}

ViewBinder:

public class ViewBinder<T> where T : IView
{
    private T currentView;
    public IView View
    {
        get
        {
            var viewModel = currentView.GetViewModel();
            return (IView)viewModel.View;
        }
    }

    public ViewBinder(T targetView) 
    {
        this.currentView = targetView;
    }

}

但是 运行 此应用显示如下错误消息: 'System.Waf.Applications.ViewModel' 不包含采用 0 个参数的构造函数 D:\MajorApps\SampleApp\MajorApps.Application\ViewModels\MainWindowViewModel.cs

谁能帮我解决这个问题missed/wrong。

感谢@nag

MainWindowViewModel 的基础 class 不包含无参数构造函数。您必须调用它定义的其中一个,例如:

public class MainWindowViewModel:ViewModel<IMainWindowView>, IMainWindowViewModel
{
    public MainWindowViewModel()
        : base(/* something here */)
    {
    }

    // ....
}