c# - WPF 和 Unity - 在类型上找不到匹配的构造函数

标签 c# wpf unity-container

我想在使用 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/

相关文章:

c# - 如何将 Owin 上下文传递给被注入(inject) Api Controller 的 Repo

c# - Unity - 根据某些条件解决依赖关系

c# - 在没有 HTTP 请求的情况下使用 PerRequestLifetimeManager 解析类型

c# - 查找严格以 $ 开头的单词,正则表达式 C#

c# - ObjectCache 不适用于多个解决方案项目?

javascript - 通过 getJSON 从 .cshtml 调用 Controller

c# - AutoMapper Project().To() 并对子集合进行排序

c# - ListView 在更新时不会改变

wpf - LargeChange 和 SmallChange 属性对 ProgressBar 控件有何作用?

wpf - 如何在 WPF 中绘制一个凹角矩形?