开发者

Silverlight Xaml Editor error when inheriting from a custom class

开发者 https://www.devze.com 2023-01-29 17:58 出处:网络
I\'ve created an abstract class which inherits from UserControl, and has an abstract method. I have a couple controls implementing this abstract class. The Silverlight control runs fine, but in the XA

I've created an abstract class which inherits from UserControl, and has an abstract method. I have a couple controls implementing this abstract class. The Silverlight control runs fine, but in the XAML editor, I get an InstanceBuilderException thrown, and thus it won't let me view the XAML preview. Is there a way to get around this, or am I just doing something wrong? Thanks!

The abstract class is very simple:

public abstract class Widget : UserControl
{
    public abstract void Close();
}

The implementation is here:

public partial class OnlineUserWidget : Widget
{
 .....
}

And the XAML

<local:Widget x:Class="Prototype.Widgets.OnlineUserWidget"
    xmlns:local="clr-namespace:Prototype.Widgets"
 ....

 </local:Widget>

Finally the error:

Cannot create an instance of "Widget". at Microsoft.Expression.DesignModel.Core.InstanceBuilderOperations.InstantiateType(Type type, Boolean supportInternal, Boolean supportProtected) at Microsoft.Expression.DesignModel.InstanceBuilders.ClrObjectInstanceBuilder.InstantiateTargetType(IInstanceBuilderContext context, ViewNode viewNode) at Microsoft.Expression.DesignModel.InstanceBuilders.ClrObjectInstanceBuilder.Instantiate(IInstanceBuilderContext context, ViewNode viewNode) at Microsoft.Expression.Platform.Silverlight.InstanceBuilders.DependencyObjectInstanceBuilder.Instantiate(IInstanceBuilderContext context, ViewNode viewNode) at Microsoft.Expression.Platform.Silverlight.InstanceBuilders.FrameworkElementInstanceBuilder.Instantiate(IInstanceBuilderContext context, ViewNode viewNode) at Microsoft.Expression.Platform.Silverli开发者_开发问答ght.InstanceBuilders.UserControlInstanceBuilder.Instantiate(IInstanceBuilderContext context, ViewNode viewNode) at MS.Internal.Services.DesignModeValueProviderService.DesignModeValueProviderBuilder.Instantiate(IInstanceBuilderContext context, ViewNode viewNode) at Microsoft.Expression.DesignModel.Core.ViewNodeManager.Instantiate(ViewNode viewNode)


UserControl is the wrong class to use in situations like these. You'll probably want to look at control. To make sure your style gets loaded properly, use the following line in the controls ctor:

this.DefaultStyleKey=typeof(Widget);

Then just make sure you have a Style assigned to widget and you'll be rockin' and rollin'. UserControls are best for one-off situations where you don't need to re-template the control or inherit from it.

0

精彩评论

暂无评论...
验证码 换一张
取 消