Site Logo

Pixel-in-Gene

Exploring Frontend Development with Design / Graphics / Technology

Creating WPF based addins with System.AddIn

With Framework 3.5 we have a new namespace called System.AddIn which is useful for creating AddIn based application architectures. For an introduction to this namespace and its usage refer to:

WPF AddIns

When creating WPF AddIns one of the first things you would want is the ability to exchange some UI related classes across AppDomains. Doing so requires us to marshall these classes using a Contract (as per System.AddIn parlance). The WPF team has created a new namespace System.Windows.Presentation that provides this contract for exchanging WPF elements. Since the AddIn does not directly depend on the Contract, the exchange over the wire happens through an Adapter class. The Adapter will adapt the Contract to the View or the View to the Contract based on whether it is translating on Host side or the AddIn side.

System.Windows.Presentation contains a class called VisualAdapters that does exactly this job. When transmitting the Visual (System.Windows.Media.Visual) from the AddIn side, one needs to convert it to a Contract. The Contract interface defined for a Visual is called System.AddIn.Contract.INativeHandleContract defined in the System.AddIn.Contract assembly. VisualAdapters.ViewToContractAdapter is the method that does this conversion for us.

public static System.AddIn.Contract.INativeHandleContract ViewToContractAdapter (System.Windows.Media.Visual root) Member of System.AddIn.Pipeline.VisualAdapters

Similarly to translate from a Contract to a Visual we have the complementary method on VisualAdapters: ContractToViewAdapter. Note that this method is not exactly symmetrical with the previous one. It does not return a Visual, instead a FrameworkElement.

public static System.Windows.FrameworkElement ContractToViewAdapter(System.AddIn.Contract.INativeHandleContract nativeHandleContract) Member of System.AddIn.Pipeline.VisualAdapters

Passing Collections across AppDomains

Once you start building functionality into your addins you would soon hit the requirement to pass collections. Fortunately we already have contracts defined for them. System.AddIn.Contract defines the IListContract<T> which is a contract for passing IList. The adapter for this contract is defined in System.AddIn: CollectionAdapters. It contains a bunch of generic methods for translating between IList<T> and IListContract<T>.

I need more Contracts!

One of the AddIns I am trying to develop requires me to pass things like VisualBrush, Storyboard, etc. However there is no contract defined for these classes. Hopefully the WPF team will add it in the future or make some available via a CodePlex project ! I hope someone from the WPF team is reading this :)

Creating WPF based addins with System.AddIn
Pavan Podila
Pavan Podila
August 3rd, 2007