簡體   English   中英

WPF MVVM導航[重復]

[英]WPF MVVM Navigation [duplicate]

這個問題已經在這里有了答案:

我試圖在登錄后在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

MainWindowViewModel的基類不包含無參數構造函數。 您必須調用它定義的其中之一,例如:

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

    // ....
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM