詳解Winform假框架的設(shè)計應(yīng)用
對于Winform假框架的設(shè)計很多人不太理解,但是對于SCSF(Smart Client Software Factory)相信大家不太陌生,本文將從Winform假框架的設(shè)計應(yīng)用開始談起。
學(xué)習(xí)SCSF 有寫日子了,對該框架有一些了解,于是自己腦子發(fā)熱寫了個假SCSF雖然不成熟,但是是對自己學(xué)習(xí)的一個總結(jié)。
主要框架示意圖(解決方案):

Winform假框架概念:
1.整個系統(tǒng)共用一個WorkItem(工作單元).
2.WorkItem中有 Service集合.
3.初始默認使用ShellForm.
WorkItem:
WorkItem 是自定義的靜態(tài)類,在程序啟動時加載默認設(shè)置,當前是代碼以后會使用XML配置。
WorkItem代碼:
- WorkItem
 - using System;
 - using System.Collections.Generic;
 - using System.Text;
 - using System.Windows.Forms;
 - using Bob.Library.UI;
 - using Bob.Library.Services;
 - namespace Bob.Library.WorkItems
 - {
 - public static class WorkItem
 - {
 - private static Shell _ShellForm;
 - private static IServices _Services;
 - public static void InitWorkItem()
 - {
 - InitServices();
 - InitShellForm();
 - }
 - public static Shell ShellForm
 - {
 - get
 - {
 - if (_ShellForm == null)
 - {
 - InitShellForm();
 - }
 - return _ShellForm;
 - }
 - }
 - private static void InitShellForm()
 - {
 - _ShellForm = new Shell();
 - }
 - public static Bob.Library.Services.IServices Services
 - {
 - get
 - {
 - if (_Services == null)
 - {
 - InitServices();
 - }
 - return _Services;
 - }
 - }
 - private static void InitServices()
 - {
 - _Services = new Services.Services();
 - }
 - }
 - }
 
WorkItem 中有一個 IServices 類型的屬性 Services,該屬性用于保存全局的Service,IService 有 AddService
代碼:
- IServices Services
 - //接口
 - using System;
 - using System.Collections.Generic;
 - using System.Text;
 - namespace Bob.Library.Services
 - {
 - public interface IServices
 - {
 - TService AddService(string key,TService service) where TService : class;
 - TService GetServiceByKey(string key) where TService : class;
 - void Clear();
 - }
 - }
 - //實現(xiàn)
 - using System;
 - using System.Collections.Generic;
 - using System.Text;
 - namespace Bob.Library.Services
 - {
 - public class Services :IServices
 - {
 - IDictionary _Services;
 - public Services()
 - {
 - InitServices();
 - }
 - private void InitServices()
 - {
 - _Services = new Dictionary();
 - }
 - IServices#region IServices
 - public TService AddService(string key, TService service) where TService : class
 - {
 - _Services[key] = service;
 - return _Services[key] as TService;
 - }
 - public TService GetServiceByKey(string key) where TService : class
 - {
 - object service = _Services[key];
 - return service is TService ? service as TService : null;
 - }
 - public void Clear()
 - {
 - InitServices();
 - }
 - #endregion
 - }
 - }
 
WorkItem 中還有一個 Shell 類型的ShellForm 屬性:該屬性是一個MDI窗口的實例,作為系統(tǒng)的父容器。
設(shè)計圖:
代碼:
- Shell
 - using System;
 - using System.Collections.Generic;
 - using System.ComponentModel;
 - using System.Data;
 - using System.Drawing;
 - using System.Text;
 - using System.Windows.Forms;
 - namespace Bob.Library.UI
 - {
 - public partial class Shell : Form
 - {
 - public Shell()
 - {
 - InitializeComponent();
 - _AppMenu.Text = AppMenuName;
 - _ExitMenu.Text = ExitString;
 - }
 - public string FormName
 - {
 - get
 - {
 - return this.ParentForm.Text;
 - }
 - set
 - {
 - this.ParentForm.Text = value;
 - }
 - }
 - public void StatusUpdate(string message)
 - {
 - _ShellStatus.Text = message;
 - }
 - private void _ExitAppMenu_Click(object sender, EventArgs e)
 - {
 - if (MessageBox.Show("Exit ?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
 - {
 - Environment.Exit(0);
 - }
 - }
 - MenuItemString#region MenuItemString
 - private string _AppName = "Application";
 - private string _ExitName = "Exit";
 - public string AppMenuName
 - {
 - get { return _AppName; }
 - set
 - {
 - _AppName = value;
 - _AppMenu.Text = _AppName;
 - }
 - }
 - public string ExitString
 - {
 - get { return _ExitName; }
 - set
 - {
 - _ExitName = value;
 - _ExitMenu.Text = _ExitName;
 - }
 - }
 - #endregion
 - public MenuStrip ShellMenu
 - {
 - get
 - {
 - return _ShellMenu;
 - }
 - }
 - }
 - }
 
Shell 中有一個菜單控件,一個狀態(tài)欄控件,將兩個控件作為屬性發(fā)布。初始加載了一個菜單項 _AppMenu ,將菜單項的Text屬性布.然后為_AppMenu 添加一個子菜單項ExitMenu 同時將他的Text屬性發(fā)布。為_ExitMenu 添加事件 _ExitAppMenu_Click;然后發(fā)布一個方法 StatusUpdate(string message) 在狀態(tài)欄顯示提示消息。
準備工作完成,開始項目開發(fā):首先創(chuàng)建一個普通的Winform項目,將 Bob.Library 應(yīng)用進來,在系統(tǒng)開始類Program.cs 中添加 WorkItem的加載 代碼如下:
- Program
 - using System;
 - using System.Collections.Generic;
 - using System.Linq;
 - using System.Windows.Forms;
 - using Bob.Library.WorkItems;
 - using ArchitectureDemo.Services;
 - namespace ArchitectureDemo
 - {
 - static class Program
 - {
 - [STAThread]
 - static void Main()
 - {
 - Application.EnableVisualStyles();
 - Application.SetCompatibleTextRenderingDefault(false);
 - InitWorkItem();
 - Application.Run(WorkItem.ShellForm);
 - }
 - private static void InitWorkItem()
 - {
 - WorkItem.InitWorkItem();
 - AddServices();
 - AddModules();
 - }
 - private static void AddModules()
 - {
 - WorkItem.ShellForm.AppMenuName = "程序";
 - WorkItem.ShellForm.ExitString = "退出";
 - AddCustomModule();
 - }
 - private static void AddCustomModule()
 - {
 - ToolStripMenuItem _btnCutomMenuItem = new ToolStripMenuItem("自定義模塊");
 - ToolStripItem _btnShowMyForm = new ToolStripButton("彈出");
 - _btnShowMyForm.Click += new EventHandler(ShowMyForm_Click);
 - _btnCutomMenuItem.DropDownItems.Add(_btnShowMyForm);
 - WorkItem.ShellForm.ShellMenu.Items.Add(_btnCutomMenuItem);
 - }
 - private static void ShowMyForm_Click(object sender, EventArgs e)
 - {
 - MyForm mForm = new MyForm();
 - mForm.MdiParent = WorkItem.ShellForm;
 - mForm.Show();
 - }
 - private static void AddServices()
 - {
 - IFormService service = WorkItem.Services.AddService("FormService", new FormService());
 - }
 - }
 - }
 
首先:加載WorkItem添加InitWorkItem() 方法,將Bob.Library中的ShellForm 實例化。然后加載Service 和模塊 AddServices() 添加一個Key為FormService的IFormService 實例,該實例在MyForm中有用到。
- GetService
 - private void btnGetGUID_Click(object sender, EventArgs e)
 - {
 - IFormService service = WorkItem.Services.GetServiceByKey("FormService");
 - txtGUID.Text = service.GetGuidString();
 - }
 
AddModules() ,模擬的添加一個自定義模塊,AddCustomModule(),為該模塊添加獨享的菜單,為該模塊添加子菜單,
為子菜單綁定事件.
然后我們讓程序開始Run 我們的 Shell Application.Run(WorkItem.ShellForm);
原文標題:Winform 應(yīng)用 【假框架】
鏈接:http://www.cnblogs.com/duoluodaizi/archive/2009/10/12/1582000.html
【編輯推薦】















 
 
 





 
 
 
 