这个demo把系统中的代码给简化了一部分,看起来,分析起来比较直观 我们看到的是一个主窗口是workbench,它带有一个菜单,一个工具栏,两个pad,一个contentview,
workbench实现了一个iworkbech的接口 从图上我们可以看到,其中一个是打开的文件,文件名是我写死到代码里边的
从我们发出打开一个文件的指令到,文件呈现在我们面前,这个过程依次是: btn发出打开一个文件的命令,调用->FileService,
FileService必须为这个文件创建一个ViewContent并加入workbench的ViewCollection的集合中 怎么找到与这个文件对应的ViewContent,
FileService调用DisplayBindingService,从这里找到能打开这个文件并能创建视图的一个IDisplayBinding
public interface IDisplayBinding
{
/// <remarks>
/// This function determines, if this display binding is able to create
/// an IViewContent for the file given by fileName.
/// </remarks>
/// <returns>
/// true, if this display binding is able to create
/// an IViewContent for the file given by fileName.
/// false otherwise
/// </returns>
bool CanCreateContentForFile(string fileName);

/// <remarks>
/// Creates a new IViewContent object for the file fileName
/// </remarks>
/// <returns>
/// A newly created IViewContent object.
/// </returns>
IViewContent CreateContentForFile(string fileName);
}
DisplayBindingService:从DisplayBindingDescriptor的集合中找到能打开这个文件的的IDisplayBinding
返回 IDisplayBinding,后,由FileService调用IDisplayBinding.CreateContentForFile方法创建视图,返回一个IViewContent 那么了解了这些,我们就做一个自己的binding了,和自己的Service就比较简单了
第一步,做一个实现了IDisplayBinding的类,指出我们可以打开的文件类型
public class HouseManDisplayBinding : IDisplayBinding
{
IDisplayBinding Members

}

第二,做与这个文件类型对应的IViewContent
public abstract class AbstractViewContent : IViewContent{//......}

public class ConDisplayBindingWrapper : AbstractViewContent
{

Panel _panel;

public override Control Control
{
get
{
return _panel;
}
}


public override void Load(string fileName)
{
Control con=new HouseCon();//一个基于usercontrol做的一个非常简单的一小控件
con.Location = new Point(1, 1);

_panel = new Panel();
_panel.AutoScroll = true;
_panel.Dock = DockStyle.Fill;
_panel.TabIndex = 1;
_panel.Controls.Add(con);

TitleName = System.IO.Path.GetFileName("控件");
}

public override void Dispose()
{
_panel.Controls.Clear();
_panel = null;
}
}第三步,在程序的addin文件中加入这个binding就可以了
<DisplayBinding id = "Control"
insertbefore = "Text"
supportedformats = "控件"
class = "SharpGui.ViewContent.HouseManDisplayBinding" />
|