I am using PRISM 5 in my WPF application. And the Shell view in my application has two regions, consider it as A and B.The region A contains a POPUP (PRISM 5 interactivity feature is used to show popup).
The application is working when i create an instance of the popup view model inside the constructor of the view .
Working code
public PopupView()
{
InitializeComponent();
this.DataContext = new PopupViewModel(); // Working code
}
But when i try to create view model instance using the dependency injection.The application fails on the InitializeComponent();
of the parent view (View A).
DI Not working code
public PopupView(PopupViewModel viewModel)
{
InitializeComponent(); // Failing in AView initialze
// before reaching here
this.DataContext = viewModel;
}
View model registration in module/bootstrapper
container.RegisterType<AViewModel>();
Error occured
NULLReference Exception occured
Stacktrace(Edited for the question)
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(Edited one to avoid project specific information)
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)
{
}
}
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.