Sometimes especially working with off-designer you may end up in situations where Expression Blend can no longer open your user controls. You may get some error like:
Cannot create an instance of [User Control class]
A lot of times even attaching a debugger to Blend/VS and putting breakpoints in your constructor / breaking on error would not yield any benefits. Why?
The reason is Expression Blend and Visual Studio designer(s) have stringent conditions that a user control has to follow in order for its type to be even declared compatible for loading. So if any of these conditions are violated, type will be declared unfit and designer won't even bother locating / invoking the constructor of your user control. I have identified two such conditions (there may be more):
1. In no path of your inheritance hiararchy can there be an abstract class - So if you have a control that is derived from an abstract control class you are out of luck. Even though your end control is concrete it won't work !
2. You need a public default constructor at each level of inheritance hierarchy - That means even if you have a default constructor in your most derived user control class, it is not sufficient. All base classes must also have public default constructors (even if none of the base classes call it) ! Good news is you can assert if these constructors are called in designer only mode by the following code:
if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
throw new InvalidOperationException("Constructor is only meant to be used by designer");
}
Sunday, April 19, 2009
Subscribe to:
Posts (Atom)