I hope you enjoyed learning about MEF and started thinking about all the possibilities around it in your applications, awesome right?. Well, now that you know a little more about things like MEF catalogues and MEF exports we can freely start talking about the basics in creating an extension for Expression Blend.
By default all the extensions in Expression blend made by 3rd party developers would live in a MEF directory catalogue under the folder Extensions in the installed path of your Blend application (the same happen for Expression Web). To “register” our application we need to provide a special bootstrapper known as Package, this special class implement the contract IPackage and takes care of our extension initialization (you know, things like registering your own services or adding things to blend) and when everything finish, provide support for cleaning up routines.
I mention a very common word in software development, services, the base of Expression extensibility. Every time we expose a new functionality to Blend we need to create and register a service and of course we will be consuming other services in our extension. The way to access this service collection is through the contract IServices, it exposes two important methods: Load and Unload, well, we will cover some important exposed core services in this series but if you want you could start taking a look by yourself using any “decompiler” tool like Red Gate’s Reflector, Jetbrains’ DotPeek, Telerik’s JustDecompile or the now popular ILSpy. Go feel free to start playing with them.
Well, no more to talk, let’s create our first simple package
- Add a reference to
Microsoft.Expression.Framework and Microsoft.Expression.Extensibility tell to Visual Studio to never copy those assemblies to output. Those assemblies are in the Expression Blend installation directory
- Add a reference to
System.ComponentModel.Composition, this library is in the GAC
- Create a class library for your extension
- Tell to Visual Studio to copy the output directly to your Expression Extensions directory
![CropperCapture[1] CropperCapture[1]](http://cprieto.com/wp-content/uploads/2011/09/CropperCapture1_thumb.jpg)
- Specify your library will be named with the postfix .Extension, this is needed for the MEF Extension Catalog to load the assembly, sort of convention.
![CropperCapture[2] CropperCapture[2]](http://cprieto.com/wp-content/uploads/2011/09/CropperCapture2_thumb.jpg)
- To easy your development effort, tell to Visual Studio to run Expression Blend when you debug the extension.
![CropperCapture[3] CropperCapture[3]](http://cprieto.com/wp-content/uploads/2011/09/CropperCapture3_thumb.jpg)
- Write the code for your “Package”
The code for your package would be very simple, let’s output to the debug output a simple message
using System.ComponentModel.Composition;
using Microsoft.Expression.Extensibility;
using System.Diagnostics;
namespace SampleExtension01 {
[Export(typeof(IPackage))]
public class SamplePackage : IPackage {
public void Load(IServices services) {
Debug.WriteLine("Hey, I'm in!");
}
public void Unload() {
Trace.WriteLine("Boo! time to go =(");
}
}
}
Now we just run the Extension and look, our package works!
![CropperCapture[4] CropperCapture[4]](http://cprieto.com/wp-content/uploads/2011/09/CropperCapture4_thumb.jpg)
There are some differences in the way we create and register an Add-In instead of an Extension, but I think the bloggers around there already pointed them.
Now, the next time we are going to start doing a little more complex things, I promise =)
Meanwhile I let you with some links to people who explain a little more about the same thing:
You can get my sample code from Bitbucket. https://bitbucket.org/cprieto/extendingblend
NOTE: The index of the series could be found here http://cprieto.com/index.php/2011/09/17/a-week-extending-blendintroduction/