“无法创建实例”在WPF XAML错误 [英] 'Cannot create an instance of' error in WPF XAML

查看:1424
本文介绍了“无法创建实例”在WPF XAML错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个公共类

 公共类FCabinetNames:列表<串GT; 

{
BusinessLogic admintasks =新BusinessLogic();
公共FCabinetNames()
{

{
名单,LT; CabinetData> CAB1 = admintasks.CabinetDataforGrid();
的foreach(在CAB1 VAR C1)
{
this.Add(c1.CabinetName);
}
}

{
}
}
}

现在在XAML页面,当我试图将该类添加为一个静态的资源,我得到了不能创建的实例如下



请指导。



更新:



这是很重要的一点,我错过告诉。应用程序编译罚款和XAML页面还加载罚款。我刨以此为数据源,果然是保持空白。


解决方案

我已经在过去类似的问题。大多数时候它的组件和引用您的自定义类是试图加载



两步解决方案最经常为我的作品:




  1. 将您的自定义类的构造给另一个函数,然后调用该函数在你的列表(或类)

  2. 的构造函数
  3. 在构造函数,但初始化组件后调用函数之前添加以下内容。 (我用这个用户控件,是不是真的有效你的情况,但是这取决于你如何设置的东西了。这可能是有用的)






 如果(System.ComponentModel.DesignerProperties.GetIsInDesignMode(本))
的回报;



这两个步骤有助于从可能与在调试状态下存在的问题装载组件的XAML设计。
@Heinzi是与东西引发错误,通常(在我的经验),其埋藏的东西,你会不会想到几乎现货。


I have a public class

public class FCabinetNames:List<string>

{
    BusinessLogic admintasks = new BusinessLogic();
    public FCabinetNames()
    {
        try
        {
            List<CabinetData> cab1 = admintasks.CabinetDataforGrid();
            foreach (var c1 in cab1)
            {
                this.Add(c1.CabinetName);
            }
        }
        catch
        {
        }
    }
}

Now in the xaml page when i try to add this class as a static resource i get the 'cannot create instance of' as below

Please guide.

UPDATE :

An important point i missed telling. The application compiles fine and the xaml page also loads fine. I was planing to use this as a datasource and expectedly that remains blank.

解决方案

I've had similar issues in the past. Most of the time its components and references that your custom class is trying to load.

The two step solution that works most often for me:

  1. Move the constructor of your custom class to another function, then call that function in in the constructor of your list (or class)
  2. Add the following before the function call in the constructor but after the Initialize component. (I use this for user controls and isn't really valid in your case, but depending on how you set stuff up. It could be useful)


if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
return;

Both steps help the xaml designer from loading components that may have issues with the being in a debug state. @Heinzi is pretty much spot on with "something" throwing errors, usually (in my experience) its something buried that you wouldn't expect.

这篇关于“无法创建实例”在WPF XAML错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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