0

Hello i have a problem with dependency injection in WPF application. Here is my code: App.xaml.cs:

  IUnityContainer container = new UnityContainer();
  container.RegisterType<ICarViewModel, CarViewModel>();
  container.RegisterType<MainWindow>();
  MainWindow mainWindow = container.Resolve<MainWindow>();
  mainWindow.Show();

CarViewModel.cs:

    public ObservableCollection<Car> Cars{ get; } = new ObservableCollection<Car>()
    {
        new Car() {Name = "Audi"},
        new Car() {Name = "Peugeot"},
        new Car() {Name = "Renault"},
    };

CarView.cs:

public partial class CarView : UserControl, ICarView
{
    public CarView()
    {
        InitializeComponent();
    }

    public CarView(CarViewModel carViewModel) : this()
    {
        DataContext = carViewModel;
    }
}

CarView.xaml:

        <ItemsControl ItemsSource = "{Binding Path = Cars}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation = "Horizontal">
                        <TextBlock Text = "{Binding Path = Name, Mode = OneWay}"/>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>

MainWindow.xaml:

<view:CarView x:Name = "CarViewControl"></view:CarView>

Unity container still call only constructor without parameters from CarView. Thanks for advice

13
  • Take out the parameter-less constructor? It's probably just defaulting to using that one. Commented Aug 4, 2016 at 11:37
  • What? Please, show me how to do it? Commented Aug 4, 2016 at 11:42
  • You just delete the constructor that doesn't take any parameters, and keep the one that does. Make sure to move the call to InitializeComponent() to the constructor that takes parameters and remove the :this() call and you should be fine. Commented Aug 4, 2016 at 11:51
  • It does not work, must be default constructor? Commented Aug 4, 2016 at 12:09
  • can you expand on "it does not work"? Doesn't work how? Compilation error? Runtime error? No error? Commented Aug 4, 2016 at 12:10

1 Answer 1

1

You can register the type this way:

 container.RegisterType(typeof(MainWindow), typeof(MainWindow), new InjectionConstructor(new InjectionParameter(typeof(CarViewModel), CarViewModelInstance)));

Or you can use [InjectionConstructor] attribute on MainWindow constructor:

 [InjectionConstructor]
 public CarView(CarViewModel carViewModel) : this()

Also, you can pass the [Dependency] attribute on constructor parameter, so Unity will try to resolve value for that parameter from its registered instances:

 [InjectionConstructor]
 public CarView([Dependency] CarViewModel carViewModel) : this()

The best approach for your example could maybe looks somehow like this:

 container.RegisterType<ICarViewModel, CarViewModel>("Car");
 ....
 [InjectionConstructor]
 public CarView([Dependency("Car")] CarViewModel carViewModel) : this()

There is a lot of ways to do what you need to do.. More info: https://msdn.microsoft.com/en-us/library/dn178463(v=pandp.30).aspx

Some test code:

 var container1 = new UnityContainer(); 
 container1.RegisterType(typeof(IBar), typeof(Bar), "Bar");
 container1.RegisterType<Foo>();

 public interface IBar
 {
 }

 public class Bar : IBar
 {
 }

 public class Foo
 {
      public IBar Bar
      {
      get;
      private set;
      }

      public Foo()
      {
      }

      [InjectionConstructor]
      public Foo([Dependency("Bar")] Bar bar)
      {
      Bar = bar;
      }
 }
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks I use last example but still call first (parameter-less) constructor.
It's strange. The [InjectionConstructor] attribute tells Unity, that this is the one constructor, which have to be called.. I'll take a look.
Are you sure, that the second constructor is not called at all? I did some tests and code is right. Notice: your first (parameter-less) constructor will be called too (because of ": this()" at the second constructor).
I tried add two constructors with parameter to MainWindow and add InjectionConstructor attribute and it works corectly. But in CarView dont worked
Hmm, really strange.. I produced some test code and it works well. I think, that problem is somewhere else (not in the dependency injection construct)..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.