Get IOC container in a popup Get IOC container in a popup wpf wpf

Get IOC container in a popup


I will assume that you use InteractionRequestTrigger with PopupWindowAction in your XAML to bind PopupView to corresponding InteractionRequest.

You can't pass PopupViewModel to the constructor of the PopupView because the view is created by the PopupWindowAction directly and not by DI-container. When PopupWindowAction creates PopupView, it will set view's DataContext to the INotification object that you passed to the InteractionRequest.Raise(…). That INotification has a Content property which can be used to pass any data you want to the PopupView. For example, you can pass PopupViewModel here.

EDIT: I've looked up PopupWindowAction sources and it appears that I'm wrong. They use ServiceLocator when they try to instantiate PopupWindowAction.WindowContentType, so technically passing PopupViewModel to PopupView's constructor should not result in an exception, but it is still useless since view's DataContext is further replaced by the INotification object passed to the InteractionRequest.

Example:

// PopupViewModel.csinternal sealed class PopupViewModel{    public PopupViewModel(string message)    {        Message = message;    }    public string Message { get; }}    // PopupView.xaml<UserControl …>    <Grid DataContext="{Binding Content, Mode=OneTime}">        <Label Text="{Binding Message, Mode=OneTime}" />    </Grid></UserControl>// SomeViewModel.csinternal sealed class SomeViewModel{    // Don't use DI-container references to construct objects, inject factories instead.    // Also to keep things simple you can just create your PopupViewModel directly if it has no external dependencies.    private readonly Func<string, PopupViewModel> _popupViewModelFactory;    public SomeViewModel(Func<string, PopupViewModel> popupViewModelFactory)    {        _popupViewModelFactory = popupViewModelFactory;    }    public ICommand ShowPopupCommand { get; } = new DelegateCommand(DoShowPopup);    public InteractionRequest<INotification> PopupRequest { get; } = new InteractionRequest<INotification>();    private void DoShowPopup()    {        PopupRequest.Raise(new Notification        {            Content = _popupViewModelFactory("This is a Popup Message!")        }, _ =>        {            // Callback code.        });    }}// SomeView.xaml<UserControl …>    <i:Interaction.Triggers>        <prism:InteractionRequestTrigger SourceObject="{Binding PopupRequest, Mode=OneTime}">            <prism:PopupWindowAction WindowContentType="views:PopupView" />        </prism:InteractionRequestTrigger>    </i:Interaction.Triggers>    <Button Command="{Binding ShowPopupCommand, Mode=OneTime}" /><UserControl>// SomeModule.cs (or maybe Bootstrapper.cs if you setup your container in Bootstrapper)public sealed class SomeModule : IModule{    private readonly IUnityContainer _container;    public SomeModule(IUnityContainer container)    {        _container = container;    }    public override void Initialize()    {        _container.RegisterType<Func<string, PopupViewModel>>(            new InjectionFactory(c =>                new Func<string, PopupViewModel>(message =>                    c.Resolve<PopupViewModel>(                        new ParameterOverride("message", message))));    }}


I think you are registering the AViewModel, but IoC container is not having the right instance or factory for PopupViewModel. From my point of view, your View needs PopupViewModel as a dependency but the container cant resolve it because this type is not registered.

In addition please push here your XAML file because the exception was thrown from InitializeComponent() method, it happens because inconsistent markup. Thus, we need to see the markup to provide you with more feedback.