WPF XAML 中的“无法创建实例”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21118380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'Cannot create an instance of' error in WPF XAML
提问by simba
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

现在在 xaml 页面中,当我尝试将此类添加为静态资源时,我得到如下所示的“无法创建实例”

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.
我错过了重要的一点。应用程序编译正常,xaml 页面也加载正常。我打算将它用作数据源,但预计它仍然是空白的。
回答by Gauthier
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:
最适合我的两步解决方案:
- Move the constructor of your custom class to another function, then call that function in in the constructor of your list (or class)
- 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)
- 将自定义类的构造函数移动到另一个函数,然后在列表(或类)的构造函数中调用该函数
- 在构造函数中的函数调用之前但在 Initialize 组件之后添加以下内容。(我将它用于用户控件,在您的情况下并不是真正有效,但取决于您如何设置内容。它可能很有用)
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.
这两个步骤都可以帮助 xaml 设计人员加载可能在调试状态下存在问题的组件。@Heinzi 几乎是“某事”抛出错误的地方,通常(根据我的经验)它是你意想不到的东西。

