WPF Designer “Could not create an instance of type

2019-02-21 08:01发布

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.

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"

...

9条回答
来,给爷笑一个
2楼-- · 2019-02-21 08:30

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.

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:

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.

查看更多
闹够了就滚
3楼-- · 2019-02-21 08:31

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

查看更多
淡お忘
4楼-- · 2019-02-21 08:33

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
查看更多
登录 后发表回答