2015-03-31 92 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 */) 
    { 
    } 

    // .... 
}