0
votes

I have created a small test WPF .net Framework solution using Prism7 with Unity. In my only Module I have a View and a ViewModel. I'm not using the AutoWireViewModel property on the View. Instead I have a constructor on the View that takes my ViewModel as a parameter:

public partial class ViewA : UserControl
{
    public ViewA(ViewAViewModel viewModel)
    {
        InitializeComponent();
        DataContext = viewModel;
    }
}

When I run the application this works, but I can't understand how. How is the ViewModel resolved without me having added it to the Unity Container? Is this some default Prism magic? If it is, is there a place where it is described?

Would be thankful for any insight.

1
add a debuger and see the call stack of the on constructor - Yogesh
Call Stack of ViewModel constructor looks like this: Module1.dll!Module1.ViewModels.ViewAViewModel.ViewAViewModel() Line 22 C# [External Code] Module1.dll!Module1.Module1Module.OnInitialized(Prism.Ioc.IContainerProvider containerProvider) Line 13 C# [External Code] Prism7Test.exe!Prism7Test.App.InitializeModules() Line 32 C# [External Code] So it's done by Prism it seems. But I don't understand why or if this the way you're meant to do things? Seems weird the VM is constructed without being registered in the Unity container. - Fred
Unity resolves concrete types without registration. How do you create theViewA instance? - Haukinger

1 Answers

1
votes

All Prism containers are configured to resolve Concrete types automatically as transients. This is what allows Prism to resolve any ViewModel regardless of whether you have registered it or not.