WPF Designer “Could not create an instance of type”

后端 未结 11 1169
悲&欢浪女
悲&欢浪女 2020-12-20 11:45

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 display

相关标签:
11条回答
  • 2020-12-20 12:11

    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

    0 讨论(0)
  • 2020-12-20 12:12

    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.

    0 讨论(0)
  • 2020-12-20 12:17

    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.

    0 讨论(0)
  • 2020-12-20 12:22

    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.

    0 讨论(0)
  • 2020-12-20 12:25

    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.

    0 讨论(0)
提交回复
热议问题