詳解Winform假框架的設(shè)計(jì)應(yīng)用
對(duì)于Winform假框架的設(shè)計(jì)很多人不太理解,但是對(duì)于SCSF(Smart Client Software Factory)相信大家不太陌生,本文將從Winform假框架的設(shè)計(jì)應(yīng)用開(kāi)始談起。
學(xué)習(xí)SCSF 有寫(xiě)日子了,對(duì)該框架有一些了解,于是自己腦子發(fā)熱寫(xiě)了個(gè)假SCSF雖然不成熟,但是是對(duì)自己學(xué)習(xí)的一個(gè)總結(jié)。
主要框架示意圖(解決方案):
Winform假框架概念:
1.整個(gè)系統(tǒng)共用一個(gè)WorkItem(工作單元).
2.WorkItem中有 Service集合.
3.初始默認(rèn)使用ShellForm.
WorkItem:
WorkItem 是自定義的靜態(tài)類,在程序啟動(dòng)時(shí)加載默認(rèn)設(shè)置,當(dāng)前是代碼以后會(huì)使用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 中有一個(gè) 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();
- }
- }
- //實(shí)現(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 中還有一個(gè) Shell 類型的ShellForm 屬性:該屬性是一個(gè)MDI窗口的實(shí)例,作為系統(tǒng)的父容器。
設(shè)計(jì)圖:
代碼:
- 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 中有一個(gè)菜單控件,一個(gè)狀態(tài)欄控件,將兩個(gè)控件作為屬性發(fā)布。初始加載了一個(gè)菜單項(xiàng) _AppMenu ,將菜單項(xiàng)的Text屬性布.然后為_(kāi)AppMenu 添加一個(gè)子菜單項(xiàng)ExitMenu 同時(shí)將他的Text屬性發(fā)布。為_(kāi)ExitMenu 添加事件 _ExitAppMenu_Click;然后發(fā)布一個(gè)方法 StatusUpdate(string message) 在狀態(tài)欄顯示提示消息。
準(zhǔn)備工作完成,開(kāi)始項(xiàng)目開(kāi)發(fā):首先創(chuàng)建一個(gè)普通的Winform項(xiàng)目,將 Bob.Library 應(yīng)用進(jìn)來(lái),在系統(tǒng)開(kāi)始類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 實(shí)例化。然后加載Service 和模塊 AddServices() 添加一個(gè)Key為FormService的IFormService 實(shí)例,該實(shí)例在MyForm中有用到。
- GetService
- private void btnGetGUID_Click(object sender, EventArgs e)
- {
- IFormService service = WorkItem.Services.GetServiceByKey("FormService");
- txtGUID.Text = service.GetGuidString();
- }
AddModules() ,模擬的添加一個(gè)自定義模塊,AddCustomModule(),為該模塊添加獨(dú)享的菜單,為該模塊添加子菜單,
為子菜單綁定事件.
然后我們讓程序開(kāi)始Run 我們的 Shell Application.Run(WorkItem.ShellForm);
原文標(biāo)題:Winform 應(yīng)用 【假框架】
鏈接:http://www.cnblogs.com/duoluodaizi/archive/2009/10/12/1582000.html
【編輯推薦】