Thursday, August 30, 2007

Building Custom UIElementAdapters

Create a custom UIElement Adapter

1. Add a new class to the shell application
2. Add references and using statements for the following libraries
Microsoft.Practices.CompositeUI
Microsoft.Practices.CompositeUI.WinForms
System.Windows.Forms
3. Inherit the class from
a. IUIElementAdapter - for specialized implementation

OR

b. UIElementAdapter - for basic common behavior
4. Implement the members of the interface as follows
a. Add - this method should add an element to the control
b. Remove - this method should remove an item from the specified control
If you want to be able to register extension sites using an instance of the control, you must also create factory for the types of control your adapter handles.


Create a UIElement adapter factory

1. Add a new class to the shell application
2. Specify that the class will implement the IUIElementAdpaterFactory interface from the
Microsoft.Practices.CompositeUI.UIElements namespace
public class MyAdapterFactory : IUIElementAdapterFactory
{
...
}
3. Implement the IUIElementAdapterFactory interface. You must provide the Supports method that determines whether the object passed to the method is of the right type. For example, if your adapter is designed to work with the Windows Forms TreeNode class, the code you require is:
public bool Supports(object myElement)
{
return (myElement is TreeNode);
}
4. You must also implement the GetAdapter method to return an instance of your custom adapter that is suitable for the object type:
public IUIElementAdapter GetAdapter(object myElement)
{
return new MyTreeNodeAdapter(...);
}
Once you have created the custom adapter factory, you must register it with the adapter factory catalog.


Register a UIElement adapter factory in the adapter factory catalog:


Override the AfterShellCreated method in your application class to get a reference to the IUIElementAdpterFactoryCatalog from the Services collection of the RootWorkItem and call the RegisterFactory method:
public class MyApplication : FormShellApplication<..., ...>
{
...
protected override void AfterShellCreated()
{
base.AfterShellCreated();
IUIElementAdpterFactoryCatalog catalog;
catalog = RootWorkItem.Services.Get();
catalog.RegisterFactory(new MyAdapterFactory());
}
...
}

No comments: