WPF 设计器“无法创建类型的实例”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/861580/
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
WPF Designer "Could not create an instance of type"
提问by jchadhowell
In my UI XAML I'm essentially inheriting from a class "BaseView" that contains functionality common to several forms, however this is preventing the designer from displaying the form: "Could not create instance of type BaseView". The code will compile and run, but it is frustrating not to be able to see the form in the Designer. Is there a better way? Thanks.
在我的 UI XAML 中,我本质上是从包含多个表单通用功能的类“BaseView”继承的,但是这阻止了设计器显示表单:“无法创建 BaseView 类型的实例”。代码将编译并运行,但无法在设计器中看到表单令人沮丧。有没有更好的办法?谢谢。
XAML:
XAML:
<vw:BaseView
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vw="clr-namespace:ReviewClient"
x:Class="ReviewClient.MainPage"
...
...
回答by jchadhowell
The problem was that the base class was defined as abstract. This caused the designer to fail. This problem is described in more detail in the comments section of Laurent Bugnion's blog: http://geekswithblogs.net/lbugnion/archive/2007/03/02/107747.aspx
问题是基类被定义为抽象类。这导致设计者失败。这个问题在Laurent Bugnion博客的评论部分有更详细的描述:http: //geekswithblogs.net/lbugnion/archive/2007/03/02/107747.aspx
回答by angad.stjudes
I found a very useful solution to this on : http://www.progware.org/Blog/post/WPF-Designer-Error-Could-not-create-an-instance-of-type.aspx.
我在以下位置找到了一个非常有用的解决方案:http: //www.progware.org/Blog/post/WPF-Designer-Error-Could-not-create-an-instance-of-type.aspx。
This link explains how the WPF designer window runs the Constructor to display the UI in XAML and the remedy: adding the following snippet to any part of constructor code which might be giving error:
此链接解释了 WPF 设计器窗口如何运行构造函数以在 XAML 中显示 UI 以及补救措施:将以下代码段添加到可能出错的构造函数代码的任何部分:
if(!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
//code producing exception
}
the function name is self explanatory. :) This link also provides solutions on debugging issues with XAML.
函数名称是不言自明的。:) 此链接还提供有关调试 XAML 问题的解决方案。
回答by Andy Dent
Another possible cause, as we just found here, so I'm adding this answer for future users, is if the project is hosted on an untrusted source, such as a file server.
另一个可能的原因,正如我们刚刚在这里发现的那样,所以我为未来的用户添加了这个答案,是项目是否托管在不受信任的来源上,例如文件服务器。
In that case, the designer wouldn't load the assembly and so gave the same "Could not create instance..." error. The solution would still build and debug OK.
在这种情况下,设计人员不会加载程序集,因此会出现相同的“无法创建实例...”错误。该解决方案仍将构建和调试 OK。
回答by Seva Alekseyev
Another cause. My control class had a static field that was initialized from resources, like this:
另一个原因。我的控件类有一个从资源初始化的静态字段,如下所示:
static Color s_ImgColor = (Color)TheApp.Resources["PhoneForegroundColor"];
That would throw a null reference exception in XAML editor, since the resources are not available in design mode. Were it not a color resource (say, a brush), this won't be a problem, but a typecast to value type throws up on a null reference.
这将在 XAML 编辑器中引发空引用异常,因为资源在设计模式下不可用。如果它不是颜色资源(例如画笔),这不会成为问题,但是对值类型的类型转换会引发空引用。
回答by paul
Yet another possible cause.
还有一个可能的原因。
I have a user control which has child controls which generate events e.g. selection_changed on list control. The select_changed event handler makes changes to other child controls.
我有一个用户控件,它具有生成事件的子控件,例如列表控件上的 selection_changed。select_changed 事件处理程序对其他子控件进行更改。
During initialisation the selected item property of the list box gets changed and triggers a selection_changed event. The handler tries to update the other child controls but cannot because they have not yet been instantiated. This leads to a null pointer exception and causes the problem.
在初始化期间,列表框的选定项属性发生更改并触发 selection_changed 事件。处理程序尝试更新其他子控件但无法更新,因为它们尚未实例化。这会导致空指针异常并导致问题。
Once the null pointer problem was handled the control was able to be instantiated and appeared in the parent control.
一旦处理了空指针问题,控件就能够被实例化并出现在父控件中。
回答by RoQ
I have the problem that my MVVM class have acces to the database in the constructor and that was the problem, it throws an exception. I only have to check if application is runing in Design mode.
我的问题是我的 MVVM 类可以访问构造函数中的数据库,这就是问题所在,它会引发异常。我只需要检查应用程序是否在设计模式下运行。
回答by Carl G
In WinForms, it's possible to use the designer with abstract controls if you use a custom TypeDescriptionProvider
to inform the designer of a concrete implementation:
在 WinForms 中,如果您使用自定义TypeDescriptionProvider
来通知设计器具体实现,则可以将设计器与抽象控件一起使用:
I'm using the solution in this answerto another question, which links this article. The article recommends using a custom TypeDescriptionProvider
and concrete implementation of the abstract class. The designer will ask the custom provider which types to use, and your code can return the concrete class so that the designer is happy while you have complete control over how the abstract class appears as a concrete class.
我正在使用this answerto another question中的解决方案,该问题链接了本文。文章推荐使用TypeDescriptionProvider
抽象类的自定义和具体实现。设计器将询问自定义提供者使用哪些类型,并且您的代码可以返回具体类,这样设计器就会很高兴,同时您可以完全控制抽象类作为具体类的显示方式。
回答by Maxim Kamalov
And another possible situation (this is actual for at least SL for WP):
还有另一种可能的情况(至少对于 WP 的 SL 来说这是实际情况):
If you create instanse of your class (ex. <local:MyDataSource />
) then it should be public. If your class is internal, it will work at design-time but will fail with this exception at runtime.
如果您创建类的实例(例如<local:MyDataSource />
),那么它应该是 public。如果您的类是内部类,它将在设计时工作,但在运行时会因此异常而失败。
回答by TechnoTim
The reason why I was getting this error was simple but tough for me to track down. My converter class was not public. Simply changing the class's accessibility fixed it.
我收到此错误的原因很简单,但对我来说很难追查。我的转换器类不是公开的。只需更改类的可访问性即可修复它。
public class StringToLowerConverter : IValueConverter