在弹出窗口中获取IOC容器

时间:2014-12-15 13:44:49

标签: c# wpf unity-container prism prism-5

我在WPF应用程序中使用PRISM 5。我的应用程序中的Shell视图有两个区域,将其视为A和B.区域A包含一个POPUP(PRISM 5交互功能用于显示弹出窗口)。

当我在视图的构造函数中创建弹出视图模型的实例时,应用程序正在工作。

工作代码

public PopupView()
{
    InitializeComponent();
    this.DataContext = new PopupViewModel(); // Working code
}

但是当我尝试使用依赖注入创建视图模型实例时。应用程序在父视图的InitializeComponent();上失败(视图A)。

DI无效代码

public PopupView(PopupViewModel viewModel)
{
    InitializeComponent(); // Failing in AView initialze
                           // before reaching here

    this.DataContext = viewModel;
}

在模块/ bootstrapper中查看模型注册

container.RegisterType<AViewModel>();

发生错误

发生NULLReference异常

Stacktrace (编辑问题)

at System.DefaultBinder.BindToMethod(BindingFlags bindingAttr, MethodBase[] match, Object[]& args, ParameterModifier[] modifiers, CultureInfo cultureInfo, String[] names, Object& state)
   at MS.Internal.Xaml.Runtime.DynamicMethodRuntime.CreateInstanceWithCtor(Type type, Object[] args)
   at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateInstance(XamlType xamlType, Object[] args)
   at MS.Internal.Xaml.Runtime.PartialTrustTolerantRuntime.CreateInstance(XamlType xamlType, Object[] args)
   at System.Xaml.XamlObjectWriter.Logic_CreateAndAssignToParentStart(ObjectWriterContext ctx)
   at System.Xaml.XamlObjectWriter.WriteEndObject()
   at System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, Boolean onlyLoadOneNode, Boolean skipJournaledProperties, Boolean shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack`1 stack, IStyleConnector styleConnector)
   at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
   at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   at MyNamespace.AView.InitializeComponent() in e:\xxx\xxxxx\xxx\AView.xaml:line 1
   at MyNamespace.AView..ctor(AViewModel viewModel) in e:\xxx\xxxxx\xxx\AView.xaml.cs:line 18

AViewModel (编辑一个以避免项目特定信息)

 public class ItemSelectionNotification : Confirmation
 { 
      //This class includes properties related to my project
 }

public class AViewModel
 {
        public InteractionRequest<ItemSelectionNotification> ItemSelectionRequest { get; private set; }

        public AViewModel(EventAggregator eventAggregator,IUnityContainer container)
        {
            this.eventAggregator = eventAggregator;
            this.container = container;
            ItemSelectionRequest = new InteractionRequest<ItemSelectionNotification>();
            SettingsCommand = new DelegateCommand(OnClickSetting);    //Command for settings button click      
        }

        //Button click handling
        public void OnClickSetting()
        {                      
                var notification = new ItemSelectionNotification()
                    {
                        Title = "Items"
                    };
                this.ItemSelectionRequest.Raise(notification,OnSaveCallback);
         }  

        private void OnSaveCallback(PropertySelectionNotification returned)
        {
        }   
 }

2 个答案:

答案 0 :(得分:1)

我将假设您在XAML中使用InteractionRequestTrigger PopupWindowActionPopupView绑定到相应的InteractionRequest

您无法将PopupViewModel传递给PopupView的构造函数,因为视图是由PopupWindowAction直接创建的,而不是由DI容器创建的。当PopupWindowAction创建PopupView时,它会将视图的DataContext设置为您传递给INotification的{​​{1}}对象。 InteractionRequest.Raise(…)具有INotification属性,可用于将您想要的任何数据传递给Content。例如,您可以在此处传递PopupView

编辑:我查了PopupViewModel sources,看来我错了。他们在尝试实例化PopupWindowAction时使用ServiceLocator,因此技术上将PopupWindowAction.WindowContentType传递给PopupViewModel的构造函数不应该导致异常,但是因为视图{{ {1}}进一步被传递给PopupView的{​​{1}}对象替换。

示例:

DataContext

答案 1 :(得分:0)

我认为您正在注册 AViewModel ,但IoC容器没有 PopupViewModel 的正确实例或工厂。从我的角度来看,您的View需要 PopupViewModel 作为依赖项,但容器无法解析它,因为此类型未注册。

另外请在这里推送您的XAML文件,因为从InitializeComponent()方法抛出异常,因为标记不一致而发生。因此,我们需要看到标记,以便为您提供更多反馈。