在主窗口中添加没有默认构造函数的用户控件将引发异常. [英] Adding user control that has no default constrctor in mainwindow throws exception.

查看:86
本文介绍了在主窗口中添加没有默认构造函数的用户控件将引发异常.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 我在我的项目中遵循MVVM模式.我有一个用户控件,其构造函数带有一个参数,如下所示.

    

公共局部类UserMessagesInformation:UserControl
    {
        公共UserMessagesInformation(IUnityContainer容器)
        {
            InitializeComponent();
            DataContext = container.Resolve< IUserMessagesViewModel>("UMVM");
        }
    } 

我需要在我的主window.xaml中添加此用户控件.当我这样做时,我会收到编译错误.

< lcl:UserMessagesInformation Horizo​​ntalAlignment ="Stretch" VerticalAlignment =拉伸"/> 

我得到的错误是类型userMessagesInformation不包括任何可访问的构造函数".  请让我知道如何解决它.

谢谢

桑杰.

  

解决方案

不,您不能通过XAML添加带有参数的用户控件.您将不得不通过后面的代码添加代码,或者按照以下链接中的建议尝试使用代码

http://stackoverflow.com/a/21718246


Hi,

   I follow MVVM pattern in my project. I have a usercontrol whose constructor takes an argument, as shown below.

    

  public partial class UserMessagesInformation : UserControl
    {
        public UserMessagesInformation(IUnityContainer container)
        {
            InitializeComponent();
            DataContext = container.Resolve<IUserMessagesViewModel>("UMVM");
        }
    }

I need to add this usercontrol in my main window.xaml. When I do so I get compilation error.

<lcl:UserMessagesInformation  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>

The error which I get is "The type userMessagesInformation does not include any accessible constructor".  Kindly let me know how to fix it. 

Thanks,

sanjay.

  

解决方案

No, you can't add User Control with parameter via XAML. You would have to either add via code behind or try code as suggested in following link

http://stackoverflow.com/a/21718246


这篇关于在主窗口中添加没有默认构造函数的用户控件将引发异常.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆