我想在使用 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.dll
Additional 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
的自定义实例,因此您不应该保留默认值 StartupUri
在 App.xaml 文件中设置的属性和 WPF 拼命尝试实例化没有默认构造函数的类型。
关于c# - WPF 和 Unity - 在类型上找不到匹配的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24251244/