In Q2 we introduced the first official version of RadRichTextBox for Silverlight. In this blog post we want to share with you one of the main principles which we have followed while developing our brand new rich text editor for Silverlight - total extensibility.
One of the key features which we have included is the ability to import/export from/to various document formats. For the first release we have already included some of the most widely used formats, such as .html/.docx/.xaml/.txt and in the next releases there are still more to come. As MEF is a wonderful technology, we have built our document format providers on top of it. What does this architecture provide? To list a few things:
You can still use the provider classes directly if MEF is not available.
Let’s make a short tour through the document format provider’s API. The main interface which is used for import/export is IDocumentFormatProvider. As its name says, it describes a single document format provider. It has a name, list of supported extensions, whether it can import and/or export and of course – methods implementing this functionality.
To use providers transparently, without knowing their exact type and other details, you can use them through MEF. The class DocumentFormatProvidersManager gives you all the needed information about the currently present document format providers. You can ask it for a list of all supported extensions, get provider by name, extension or even register one manually. Let’s see a sample usage of this class:
RadDocument doc;
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == true)
{
IDocumentFormatProvider provider =
DocumentFormatProvidersManager.GetProviderByExtension(
ofd.File.Extension.ToLower());
if (provider != null)
{
Stream steream = ofd.File.OpenRead();
doc = provider.Import(steream);
}
}
[CustomDocumentFormatProvider]
public class MyXamlFormatProvider:IDocumentFormatProvider
{
public IEnumerable<string> SupportedExtensions
{
get { return".xaml"; }
}
//… the rest of the IDocumentFormatProvider implementation
}
As this provider uses the ".xaml" extension, this will override the default XamlFormatProvider implementation. Then the method GetProviderByExtension will return our new format provider transparently for the application.
As we have already mentioned, you can use the providers directly, without using MEF. You can do this by instantiating a particular provider:
IDocumentFormatProvider provider = new DocxFormatProvider();
provider.Export(document, output);
RadDocument doc = provider.Import(inputStream);
As we have shown, using and extending RadRichTextBox document format providers for import and export is very easy. In the near future we are planning to add a bunch of new providers for formats including MHT, PDF and XPS.
Finally, all dialog boxes, such as the Insert Symbol, Hyperlink, Paragraph properties, etc., integrate with RadRichTextBox using MEF. This means that you can customize them easily or even replace them with your own implementation. For example, to use a custom Insert Hyperlink dialog, you need a simple class like:
[CustomInsertHyperlink]
public partial class CustomHyperlinkDialog:RadWindow,IInsertHyperlinkDialog
…
MEF will take care of the rest for you, binding an instance of your custom dialog to RadRichTextBox when you select "Add Hyperlink Link" from the Ribbon UI or when executing ShowInsertHyperlinkDialogCommand.
Moreover, as with custom format-providers, if for some reason you do not want to use MEF, we have introduced APIs to integrate your custom dialogs with just a few lines of code without any external dependencies.
Comments will be as always - most welcome!