WPF MVVM导航

时间:2015-03-31 06:41:01

标签: c# wpf mvvm

我正在尝试在登录后在我的MainWindow中实现MVVM导航,之后点击“登录”按钮我将调用MainWindow.xaml进行显示,之后我习惯在Mainwindow中根据菜单进行导航/功能区选择。

以下是我到目前为止所做的实施:

点击“登录”按钮命令:

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

任何人都可以帮我解决我错过/错误的事情。

谢谢@nag

1 个答案:

答案 0 :(得分:0)

MainWindowViewModel的基类不包含无参数构造函数。您必须调用它已定义的那个之一,例如:

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

    // ....
}