18

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"

...

jchadhowell
  • 1,131
  • 1
  • 14
  • 25

11 Answers11

21

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: Link

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
jchadhowell
  • 1,131
  • 1
  • 14
  • 25
  • 2
    Thanks! I was just having this problem myself. – Stephen Oct 09 '09 at 21:25
  • I can't see the solution, for the designer, in the link you wrote down. – Luis Filipe Jan 22 '10 at 10:43
  • The solution is to make the base class concrete (i.e. remove the abstract keyword from the base classe). Not a very satisfying solution, but it works. – Torben Junker Kjær Nov 24 '10 at 12:17
  • I put this in my code: /// /// todo: This class should really be abstract. However, due to a bug in the XAML editor, all controls based on this will be shown as if in an error state (although they will run perfectly well). /// Therefore it is currently not abstract. This should be changed when this bug has been fixed. /// – Torben Junker Kjær Nov 24 '10 at 12:20
  • Expression Blend 4 is capable of rendering views that inherit from abstract classes. Not sure about earlier versions. – HotN Oct 17 '11 at 18:37
9

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.

Storm
  • 684
  • 9
  • 20
angad.stjudes
  • 186
  • 1
  • 6
4

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.

Andy Dent
  • 17,578
  • 6
  • 88
  • 115
2

enter image description here

Yeay! I solved it. The form ended up showing on the Display & error gone after I set the Control Display Options to Only Display Platform Control. Check out the image to locate it.

Boken
  • 4,825
  • 10
  • 32
  • 42
  • Well you simply bypass rendering of nested controls with this, but it was helpful for me, because I wanted to disable this after enabling it, accidently. – Martin Braun Nov 15 '21 at 11:17
2

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.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
1

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.

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.

Once the null pointer problem was handled the control was able to be instantiated and appeared in the parent control.

paul
  • 13,312
  • 23
  • 81
  • 144
0

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:

I'm using the solution in this answer to 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.

Community
  • 1
  • 1
Carl G
  • 17,394
  • 14
  • 91
  • 115
0

And another possible situation (this is actual for at least SL for WP):

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.

Maxim Kamalov
  • 745
  • 9
  • 23
0

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
TechnoTim
  • 3,065
  • 1
  • 23
  • 28
0

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.

akjoshi
  • 15,374
  • 13
  • 103
  • 121
RoQ
  • 11
  • 2
0

I don't know what the problem is, but I solved it like this:

You should not execute the code in the constructor of your BaseClass but from the constructor of the parent class.

You create a method in the BaseClass that you execute from the constructor of the parent class.

A. Morel
  • 9,210
  • 4
  • 56
  • 45