WorkSpace扩展
自定义WorkSpace分两种方式:一种是Control_Based;一种是WorkSpace_Based。这两种的具体区别就是继承的接口不同,而且开发的方式也不同,前者以用户控件形式开发,后者是以组件方式开发。先看看两种方式的WorkSpace视图:
图 3.3.1
1. Control_Based方式
这个例子思路上很简单,就是在显示区域上增加一个Label,具体实现步骤如下:
l 创建一个Module项目并利用SCSF创建一个VIEW,添加一个Label和DeckWorkspace
如图 3.3.3 方式摆放
图 3.3.2
图 3.3.3
l 继承并实现IWorkSpace接口
public partial class myWorkSpace : UserControl, IWorkspace { public myWorkSpace() { InitializeComponent(); this.deckWorkspace1.SmartPartClosing += new EventHandler<WorkspaceCancelEventArgs>(deckWorkspace1_SmartPartClosing); }
void deckWorkspace1_SmartPartClosing(object sender, WorkspaceCancelEventArgs e) { this.label1.Text = ""; }
#region IWorkspace 成员
public void Activate(object smartPart) { this.deckWorkspace1.Activate(smartPart); this.label1.Text = "give me fouce!"; }
public object ActiveSmartPart { get { return this.deckWorkspace1.ActiveSmartPart; } }
public void ApplySmartPartInfo(object smartPart, ISmartPartInfo smartPartInfo) { this.deckWorkspace1.ApplySmartPartInfo(smartPart, smartPartInfo); }
public void Close(object smartPart) { this.deckWorkspace1.Close(smartPart); }
public void Hide(object smartPart) { this.deckWorkspace1.Hide(smartPart); }
public void Show(object smartPart) { this.deckWorkspace1.Show(smartPart); }
public void Show(object smartPart, ISmartPartInfo smartPartInfo) { this.deckWorkspace1.Show(smartPart, smartPartInfo); }
public event EventHandler<WorkspaceEventArgs> SmartPartActivated { add { this.deckWorkspace1.SmartPartActivated += value; } remove { this.deckWorkspace1.SmartPartActivated -= value; } }
public event EventHandler<WorkspaceCancelEventArgs> SmartPartClosing { add { this.deckWorkspace1.SmartPartClosing += value; } remove { this.deckWorkspace1.SmartPartClosing -= value; } }
public System.Collections.ObjectModel.ReadOnlyCollection<object> SmartParts { get { List<object> result = new List<object>(); foreach (Control ctrl in deckWorkspace1.SmartParts) result.Add(ctrl); return result.AsReadOnly(); } }
#endregion } |
l 编译后则可以直接在项目中作为WordSpace使用
2. WorkSpace_Base方式
这个新的WorkSpace就是在原来的WindowSpace的基础上增加一个ToolStrip,由于没有设计时界面,只能纯编程实现。
l 添加组件并命名为myWorkSpaceBase,并同时添加类myWorkSpaceSmartPartInfo(该例子中暂时不用,继承了ISmartPartInfo用于设置myWorkSpaceBase的其他属性)。
图 3.3.4
l 继承接口Panel, IComposableWorkspace<Control, myWorkSpaceSmartPartInfo>,添加实现代码:
public partial class myWorkSpaceBase : Panel, IComposableWorkspace<Control, myWorkSpaceSmartPartInfo> { private Dictionary<Control, string> myList = new Dictionary<Control, string>(); private WorkspaceComposer<Control, myWorkSpaceSmartPartInfo> composer;
public myWorkSpaceBase() { composer = new WorkspaceComposer<Control, myWorkSpaceSmartPartInfo>(this); initForm(); } [ServiceDependency] public WorkItem WorkItem { set { composer.WorkItem = value; } } #region 测试添加一个TOOLBAR到WorkSpace中 void initForm() { ToolStrip tool = new ToolStrip(); this.Controls.Add(tool); tool.BringToFront(); tool.Dock = DockStyle.Top;
ToolStripButton btn = new ToolStripButton(); btn.Text = "test"; btn.Click += new EventHandler(btn_Click); tool.Items.Add(btn); } void btn_Click(object sender, EventArgs e) { MessageBox.Show("my Workspace test"); } #endregion
#region IComposableWorkspace<Control,SmartPartInfo> 成员
public myWorkSpaceSmartPartInfo ConvertFrom(ISmartPartInfo source) { return SmartPartInfo.ConvertTo<myWorkSpaceSmartPartInfo>(source); }
public void OnActivate(Control smartPart) { smartPart.Show();
try { smartPart.Focus(); } catch (Exception) {
}
}
public void OnApplySmartPartInfo(Control smartPart, myWorkSpaceSmartPartInfo smartPartInfo) { addControlToSpace(smartPart, smartPartInfo); }
public void OnClose(Control smartPart) { if (myList.ContainsKey(smartPart)) { this.Controls.Remove(smartPart); myList.Remove(smartPart); } smartPart.Dispose(); }
public void OnHide(Control smartPart) { smartPart.Hide(); }
public void OnShow(Control smartPart, myWorkSpaceSmartPartInfo smartPartInfo) { addControlToSpace(smartPart, smartPartInfo); Activate(smartPart); }
public void RaiseSmartPartActivated(WorkspaceEventArgs e) { if (SmartPartActivated != null) { SmartPartActivated(this, e); } }
public void RaiseSmartPartClosing(WorkspaceCancelEventArgs e) { if (SmartPartClosing != null) { SmartPartClosing(this, e); } }
#endregion
#region IWorkspace 成员
public void Activate(object smartPart) { composer.Activate(smartPart); }
public object ActiveSmartPart { get { return composer.ActiveSmartPart; } }
public void ApplySmartPartInfo(object smartPart, ISmartPartInfo smartPartInfo) { composer.ApplySmartPartInfo(smartPart, smartPartInfo); }
public void Close(object smartPart) { composer.Close(smartPart); }
public void Hide(object smartPart) { composer.Hide(smartPart); }
public void Show(object smartPart) { composer.Show(smartPart); }
public void Show(object smartPart, ISmartPartInfo smartPartInfo) { composer.Show(smartPart, smartPartInfo); }
public event EventHandler<WorkspaceEventArgs> SmartPartActivated;
public event EventHandler<WorkspaceCancelEventArgs> SmartPartClosing;
public System.Collections.ObjectModel.ReadOnlyCollection<object> SmartParts { get { return composer.SmartParts; } }
#endregion
#region 私有方法 private void addControlToSpace(Control smartPart, myWorkSpaceSmartPartInfo smartPartInfo) {
if (myList.ContainsKey(smartPart)) {
smartPart.Show(); smartPart.BringToFront(); if (smartPartInfo.Dock!=null) { smartPart.Dock = smartPartInfo.Dock.Value; } else { smartPart.Dock = DockStyle.Fill; }
} else { this.Controls.Add(smartPart); myList.Add(smartPart, smartPart.Name); smartPart.Show(); smartPart.BringToFront(); if (smartPartInfo.Dock != null) { smartPart.Dock = smartPartInfo.Dock.Value; } else { smartPart.Dock = DockStyle.Fill; } }
} #endregion |
其中方法initForm用于界面的设计,WorkspaceComposer类型变量private WorkspaceComposer<Control, myWorkSpaceSmartPartInfo> composer继承于WorkSpace用于实现其中的共有部分方法的实现,如果需要用到SmartPartInfo部分则还需要增加以下代码,才能正常使用SmartPartInfo:
[ServiceDependency] public WorkItem WorkItem { set { composer.WorkItem = value; } }
|
l 编译通过后则可在设计器中使用