PopupWindowAction breaking MEF import?

If you use Prism InteractionRequest.PopupWindowAction feature, you might have found the MEF Import attribute does not work in the popup window.

This is because PopupWindowAction.WindowContent property creates a new instance of your xaml usercontrol which caused the conflit to MEF principal and consequently the Import chain is broken here.

My workaround is not to use xaml code for the interaction which may look like this:

<i:Interaction.Triggers>

        <prism:InteractionRequestTrigger SourceObject="{Binding BizSelectionRequest, Mode=OneWay}">

            <PopupWindowAction CenterOverAssociatedObject="True"

                                              IsModal="True">

                <PopupWindowAction.WindowContent>

                    <views:BizSearch />

                </PopupWindowAction.WindowContent>

            </PopupWindowAction>

        </prism:InteractionRequestTrigger>

    </i:Interaction.Triggers>

Instead, you can comment the above xaml code and create the expected interaction in your code behind:

                var triggerCollection = System.Windows.Interactivity.Interaction.GetTriggers(this);

                InteractionRequestTrigger trigger = new InteractionRequestTrigger();

                var binding = new Binding(“BizSearchRequest");

                binding.Source = this.DataContext;

                BindingOperations.SetBinding(trigger, InteractionRequestTrigger.SourceObjectProperty, binding);

                PopupWindowAction pwa = new PopupWindowAction();

                pwa.WindowContent = v;

                pwa.WindowContent.DataContext = vm;

                pa.CenterOverAssociatedObject = true;

                pa.IsModal = true;

                trigger.Actions.Add(pa);

                triggerCollection.Add(trigger);

The "v" and "vm" are imported by MEF

[Import]

BizSearchPopup bizSearchPopup;



[Import]

BizSearchPopupViewModel bizSearchPopupViewModel;

By this trick, a little dirty though, whatever you import within your BizSearchPopup or BizSearchPopupViewModel should work.

 

你可能感兴趣的:(PopupWindow)