加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 18:04:01 所属栏目:百科 来源:网络整理
导读:我想在使用VS2012的 WPF应用程序中使用Unity,我将Unity容器定义如下: IUnityContainer unityContainer = new UnityContainer();unityContainer.RegisterTypeIMainViewModel,MainViewModel();var window = unityContainer.ResolveMainWindow();window.Show()
我想在使用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的自定义实例,因此您不应该在App.xaml文件中设置默认的 StartupUri属性,并且WPF拼命地尝试在没有默认构造函数的情况下实例化类型.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读