Month List

RecentComments

Comment RSS

WPF Designer Error "Could not create an instance of type"

by Ioannis 20. September 2009 09:48

Suppose that in a WPF application there are some custom User Controls implemented.

During development in some windows that use the custom User Control you may find that the designer cannot load them and instead it gives the following error:

"Could not create an instance of type YOURCONTROLHERE"

This happens because the designer runs the constructor for the User Control whenever it needs to display it in design mode. Within the constructor something throws an exception. Sometimes this does not mean necessarily that the excepion will be thrown during runtime. So you need a way to allow the designer show the window but also not alter the code that runs correctly at runtime.

A way to solve this is to change your code as follows:

Original:

public UserControlConstructor()
{
    InitializeComponent();
    //Code that throws the exception
}

Change To:

public UserControlConstructor()
{
    InitializeComponent();
    if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
    {
        //Code that throws the exception
    }
}

The directive GetIsInDesignMode(this) does the trick of knowing whether the constructor is run in Design or Runtime mode.

 

 kick it on DotNetKicks.com Shout it

Tags:

WPF

Comments (2) -

9/29/2010 11:59:07 AM #

Mike Gledhill


Strangely, I found that my UserControl was preventing VS2008 from displaying my WPF pages (which contained such a UserControl) correctly, until I added this:

public partial class FXRatesUserControl : UserControl
{
    private FXRatesUserControlViewModel _vm;

    public FXRatesUserControl()
    {
        if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
        {
            InitializeComponent();
        }

        //  Other construction code  (which wasn't causing any errors)
    }
  }


Very odd.
I actually have to tell VS not to run it's own InitialiseComponent function in Design mode, in order to work properly.

I haven't worked out WHY this was needed though.

Mike Gledhill Switzerland

11/9/2010 12:17:18 PM #

Sean Murphy

My user controls were stopping VS2008 from display WPF pages too.  I discovered the error was from a stack panel resource reference:

  <StackPanel Style="{StaticResource VerticalFieldSetItem}">

once I changed it to:

  <StackPanel Style="{DynamicResource VerticalFieldSetItem}">

the designer worked fine.  Seems the designer didn't know where to find the key for the UserControl.  Strange it didn't complain when I opened the usercontrol directly though Frown


Sean Murphy

Pingbacks and trackbacks (2)+

Add comment




biuquote
  • Comment
  • Preview
Loading






Powered by BlogEngine.NET 2.0.0.36

ITPRO DevConnections 2011

Creative Commons License

Programming Blogs - BlogCatalog Blog Directory Add to Technorati Favorites

MVP Award


Ioannis Panagopoulos





This blog is using BlogEngine.Net and is hosted in the hoster below. I have not experienced any problems installing BlogEngine.Net in the host and I am satisfied with the host's response times. Therefore I recommend it.


DiscountASP Add