Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Cannot create an instance of' error in WPF XAML

Tags:

c#

wpf

xaml

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 enter image description here

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.

like image 935
simba Avatar asked Jan 14 '14 16:01

simba


1 Answers

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.

like image 108
Gauthier Avatar answered Oct 16 '22 03:10

Gauthier