Udostępnij za pośrednictwem


ToolboxExampleAttribute - a new extension point in Blend 4 (and a few other extensibility related changes)

For Blend 4, we have added a new extension point to our Asset library - ToolboxExampleAttribute. Like most of our other attributes

Consider this scenario (which we ran into as we were adding new content into our SDK - the various shapes like Star, Arc, Donut, etc): say you have a type Foo that is a Control you want to show in the Blend asset library. However, when Foo is instantiated, you want to set a set of properties. In the past, you could have done this with the DefaultInitializer concept, but the problem is that there can be only one DefaultInitializer associated with a type. And you don't really want to subclass Foo to have different types just for the purpose of having them show up in the Blend asset library.

image

Enter ToolboxExampleAttribute. Now you can have multiple "examples" of the same type, each of which will result in the generation of different XAML. To use this attribute is very straightforward - you would hook this up just like any other attribute in the design time library. Here is a simple code snippet of how you could use this feature:

internal class ExampleOne : IToolboxExample
    {
        public ModelItem CreateExample(EditingContext context)
        {
            //Return a ModelItem instance that sets appropriate properties
        }
        public Stream GetImageStream(System.Windows.Size desiredSize)
        {
            //Return an icon image stream
        }
        public string DisplayName
        {
            get { return "Something"; }
        }
    }

And to register the example:

internal class MetaDataStore : IProvideAttributeTable
    {
        // Called by the designer to register any design-time metadata.
        public AttributeTable AttributeTable
        {
            get
            {
                AttributeTableBuilder tableBuilder = new AttributeTableBuilder();
                    typeof(Inker),
                    new ToolboxExampleAttribute(typeof(ExampleOne)));

                return tableBuilder.CreateTable();
            }
        }
    }

Also, you will be happy to know that Silverlight 4 adds a bunch of new attributes to the core platform that would have required you to author a design-time library in the past to supply for your custom controls - some examples of newly added attributes that I really like as the greatly enhance the design-time experience:
System.ComponentModel.AlternateContentPropertyAttribute in System.Windows.dll
System.ComponentModel.DefaultBindingPropertyAttribute in System.Windows.dll

Comments

  • Anonymous
    September 15, 2010
    Thanks for highlighting this new functionality. I tried to implement this feature in my application, however got stuck in creating the ModelItem that would be returned by IToolboxExample.CreateExample. Could you give any guidance on how this can be achieved, as ModelItem is an abstract class? Thank you!