0

I'm trying to implement MVVM navigation in my MainWindow after login, before that after click on 'Login' button i've to call MainWindow.xaml to display and after that i used to do navigation in my Mainwindow based on menu/ribbon selection.

Below is the implementation i've done so far:

On 'Login' button command:

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;
    }

}

But While run this app it's showing error message like below: 'System.Waf.Applications.ViewModel' does not contain a constructor that takes 0 arguments D:\MajorApps\SampleApp\MajorApps.Application\ViewModels\MainWindowViewModel.cs

Can anyone help me this out what i missed/wrong.

Thanks @nag

nag
  • 920
  • 6
  • 27
  • 51
  • your questions contains information that seems to contradict each other: on the one hand you have `new MainWindowViewModel()` so your `MainWindowViewModel` has to have an constructor with 0 parameters (which you did not show) and on the other hand you get the error - can you please tell us what you are binding? Do you use a `ViewModel` as a `DataContext` or something (like ``)? If so there is your problem! – Random Dev Mar 31 '15 at 07:05
  • The type `ViewModel` that your `MainWindowViewModel` is inheriting has no parameterless constructor. You need to add a default constructor to your `MainWindowViewModel` that calls the base’s constructor correctly. Also, finish the implementation of your view model or nothing will work. – poke Mar 31 '15 at 07:07
  • I've edited my code by adding constructor to my viewmodel but for base constructor i've confused how to give value, It's showing error like ''System.Waf.Applications.ViewModel' does not contain a constructor that takes 0 arguments D:\MajorApps\SampleApp\MajorApps.Application\ViewModels\MainWindowViewModel.cs' – nag Mar 31 '15 at 07:13
  • @nag you can [edit](http://stackoverflow.com/posts/29362189/edit) your question and include that information. You are still not showing the constructor for MainWindowViewModel. – default Mar 31 '15 at 08:16

1 Answers1

0

The base class for MainWindowViewModel does not contain a parameterless constructor. You would have to call one of the ones that it has defined, something like:

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

    // ....
}
default
  • 11,485
  • 9
  • 66
  • 102