2015-03-31 87 views
0

我试图在登录后在我的MainWindow中实现MVVM导航,在此之前,点击'登录'按钮后我要调用MainWindow.xaml来显示,之后我用来在我的主窗口中进行导航基于菜单/色带选择。WPF MVVM导航

下面是迄今为止我所做的实现:

在 '登录' 按钮命令:

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 \ SampleAp p \ MajorApps.Application \ ViewModels \ MainWindowViewModel.cs

任何人都可以帮助我了解我错过了什么/错。

感谢@nag

+0

您的问题包含似乎相互矛盾的信息:一方面是你有'新MainWindowViewModel()'所以你'MainWindowViewModel '必须有一个带有0个参数的构造函数(你没有显示),另一方面你得到错误 - 你能告诉我们你的约束力吗?你使用'ViewModel'作为'DataContext'或者其他东西(比如'')?如果是的话,那就是你的问题! – Carsten 2015-03-31 07:05:15

+0

您的MainWindowViewModel继承的'ViewModel '类型没有无参数构造函数。你需要在你的MainWindowViewModel中添加一个默认的构造函数来正确调用基类的构造函数。另外,完成你的视图模型的实现或没有任何工作。 – poke 2015-03-31 07:07:09

+0

我已经编辑我的代码,通过添加构造函数到我的viewmodel但基础构造函数我已经混淆了如何给值,它显示错误像''System.Waf.Applications.ViewModel '不包含一个构造函数,其中包含0个参数\t D:\ MajorApps \ SampleApp \ MajorApps.Application \ ViewModels \ MainWindowViewModel.cs' – nag 2015-03-31 07:13:35

回答

0

的基类MainWindowViewModel不含参数的构造函数。你将不得不调用它定义的那些之一,是这样的:

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

    // .... 
}