1

Im pretty new to modern ui framework. I'm adding new page(usercontroller) as ContentSource page. Im using IOC framework (IviewModels and ViewModels). I'm getting error saying no maching constructor found. because usercontroll default constructor injected with Iviewmodel object.

i'm pretty stuck here, it would be great some one can help this matter
thanks

this is my main window code + this is my usercontroll cs file
this is the error

Jayendra
  • 13
  • 4

1 Answers1

1

As you found out, you can't use parameterized constructors because they break the framework. Navigation use just the page URI, no other extra parameters.

So, how do you use IoC without parameterized constructors? You should use a Dependency Injection Container. Something like this:

public partial class MyPage: UserControl
{
  private MyViewModel: IViewModel;
  public MyPage()
  {
     MyViewModel = MyViewModelFactory.Create(IViewModel);
     InitializeComponent();
  }
}

MyVewModelFactory is an object which create other objects. You dont have to code it by yourself. Some common IoC containers are:

  1. Unity
  2. MEF

Using Unity your code would be:

public partial class MyPage: UserControl
{
  private MyViewModel: IViewModel;
  public MyPage()
  {
    MyViewModel = container.Resove<IViewModel>();
    InitializeComponent();
  }
}

Using MEF your code would be:

public partial class MyPage: UserControl
{
  [Import(GetType(IViewModel))]
  private MyViewModel: IViewModel;
  public MyPage()
  {
     InitializeComponent();
  }
}
corradolab
  • 718
  • 3
  • 16