我想在使用VS2012的
WPF应用程序中使用Unity,我将Unity容器定义如下:
IUnityContainer unityContainer = new UnityContainer(); unityContainer.RegisterType<IMainviewmodel,Mainviewmodel>(); var window = unityContainer.Resolve<MainWindow>(); window.Show();
我的窗口构造函数如下所示:
public MainWindow(IMainviewmodel mainviewmodel)
{
InitializeComponent();
this.DataContext = mainviewmodel;
}
当我运行该应用程序时,我收到以下错误:
An unhandled exception of type
‘System.Windows.Markup.XamlParseException’ occurred in
PresentationFramework.dllAdditional information: ‘No matching constructor found on type
‘WPFClient.MainWindow’. You can use the Arguments or FactoryMethod
directives to construct this type.’ Line number ‘3’ and line position
‘9’.
我究竟做错了什么?
解决方法
在App.xaml中,确保已经摆脱了正在设置的StartupUri =“MainWindow.xaml”属性.由于您已经覆盖应用程序的OnStartup并提供了MainWindow的自定义实例,因此您不应该在App.xaml文件中设置默认的
StartupUri属性,并且WPF拼命地尝试在没有默认构造函数的情况下实例化类型.