自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解Winform假框架的設(shè)計(jì)應(yīng)用

開(kāi)發(fā) 后端
在這里我們將介紹Winform假框架的設(shè)計(jì)應(yīng)用,希望本文能對(duì)大家了解SCSF有所幫助。

對(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代碼:

  1. WorkItem  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Text;  
  5. using System.Windows.Forms;  
  6. using Bob.Library.UI;  
  7. using Bob.Library.Services;  
  8.  
  9. namespace Bob.Library.WorkItems  
  10. {  
  11.     public static class WorkItem  
  12.     {  
  13.         private static Shell _ShellForm;  
  14.         private static IServices _Services;  
  15.  
  16.         public static void InitWorkItem()  
  17.         {  
  18.             InitServices();  
  19.             InitShellForm();  
  20.         }  
  21.  
  22.  
  23.         public static Shell ShellForm  
  24.         {  
  25.             get 
  26.             {  
  27.                 if (_ShellForm == null)  
  28.                 {  
  29.                     InitShellForm();  
  30.                 }  
  31.                 return _ShellForm;  
  32.             }  
  33.         }  
  34.  
  35.         private static void InitShellForm()  
  36.         {  
  37.             _ShellForm = new Shell();  
  38.         }  
  39.  
  40.  
  41.         public static Bob.Library.Services.IServices Services  
  42.         {  
  43.             get 
  44.             {  
  45.                 if (_Services == null)  
  46.                 {  
  47.                     InitServices();  
  48.                 }  
  49.                 return _Services;  
  50.             }  
  51.         }  
  52.  
  53.         private static void InitServices()  
  54.         {  
  55.             _Services = new Services.Services();  
  56.         }  
  57.  
  58.     }  

WorkItem 中有一個(gè) IServices 類型的屬性 Services,該屬性用于保存全局的Service,IService 有 AddService、GetServiceByKey、Clear 三個(gè)方法:實(shí)現(xiàn) 添加、獲取、清空Service操作。

代碼:

  1. IServices Services  
  2. //接口  
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.Text;  
  6.  
  7. namespace Bob.Library.Services  
  8. {  
  9.     public interface IServices  
  10.     {  
  11.         TService AddService(string key,TService service) where TService : class;  
  12.  
  13.         TService GetServiceByKey(string key) where TService : class;  
  14.  
  15.         void Clear();  
  16.     }  
  17. }  
  18.  
  19.  
  20.  
  21. //實(shí)現(xiàn)  
  22. using System;  
  23. using System.Collections.Generic;  
  24. using System.Text;  
  25.  
  26. namespace Bob.Library.Services  
  27. {  
  28.     public class Services :IServices  
  29.     {  
  30.         IDictionary _Services;  
  31.  
  32.         public Services()  
  33.         {  
  34.             InitServices();  
  35.         }  
  36.  
  37.         private void InitServices()  
  38.         {  
  39.             _Services = new Dictionary();  
  40.         }  
  41.       
  42.         IServices#region IServices   
  43.  
  44.         public TService  AddService(string key, TService service) where TService : class 
  45.         {  
  46.             _Services[key] = service;  
  47.             return _Services[key] as TService;  
  48.         }  
  49.  
  50.         public TService  GetServiceByKey(string key) where TService : class 
  51.         {  
  52.             object service = _Services[key];  
  53.             return service is TService ? service as TService : null;  
  54.         }  
  55.  
  56.         public void  Clear()  
  57.         {  
  58.             InitServices();  
  59.         }  
  60.  
  61.         #endregion  
  62.     }  

WorkItem 中還有一個(gè) Shell 類型的ShellForm 屬性:該屬性是一個(gè)MDI窗口的實(shí)例,作為系統(tǒng)的父容器。

設(shè)計(jì)圖:

[[6263]]

代碼:

  1. Shell  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.ComponentModel;  
  5. using System.Data;  
  6. using System.Drawing;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.  
  10. namespace Bob.Library.UI  
  11. {  
  12.     public partial class Shell : Form  
  13.     {  
  14.         public Shell()  
  15.         {  
  16.             InitializeComponent();  
  17.             _AppMenu.Text = AppMenuName;  
  18.             _ExitMenu.Text = ExitString;  
  19.         }  
  20.  
  21.         public string FormName  
  22.         {  
  23.             get 
  24.             {  
  25.                 return this.ParentForm.Text;  
  26.             }  
  27.             set 
  28.             {  
  29.                 this.ParentForm.Text = value;  
  30.             }  
  31.         }  
  32.  
  33.           
  34.         public void StatusUpdate(string message)  
  35.         {  
  36.             _ShellStatus.Text = message;  
  37.         }  
  38.  
  39.         private void _ExitAppMenu_Click(object sender, EventArgs e)  
  40.         {  
  41.             if (MessageBox.Show("Exit ?""Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)  
  42.             {  
  43.                 Environment.Exit(0);  
  44.             }  
  45.         }  
  46.  
  47.  
  48.         MenuItemString#region MenuItemString  
  49.         private string _AppName = "Application";  
  50.         private string _ExitName = "Exit";  
  51.         public string AppMenuName  
  52.         {  
  53.             get { return _AppName; }  
  54.             set   
  55.             {   
  56.                 _AppName = value;  
  57.                 _AppMenu.Text = _AppName;  
  58.             }  
  59.         }  
  60.  
  61.         public string ExitString  
  62.         {  
  63.             get { return _ExitName; }  
  64.             set 
  65.             {  
  66.                 _ExitName = value;  
  67.                 _ExitMenu.Text = _ExitName;  
  68.             }  
  69.         }  
  70.  
  71.         #endregion  
  72.  
  73.         public MenuStrip ShellMenu  
  74.         {  
  75.             get 
  76.             {  
  77.                 return _ShellMenu;  
  78.             }  
  79.         }  
  80.  
  81.     }  

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的加載 代碼如下:

  1. Program  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Windows.Forms;  
  6. using Bob.Library.WorkItems;  
  7. using ArchitectureDemo.Services;  
  8.  
  9. namespace ArchitectureDemo  
  10. {  
  11.     static class Program  
  12.     {  
  13.         [STAThread]  
  14.         static void Main()  
  15.         {  
  16.             Application.EnableVisualStyles();  
  17.  
  18.             Application.SetCompatibleTextRenderingDefault(false);  
  19.  
  20.             InitWorkItem();  
  21.  
  22.             Application.Run(WorkItem.ShellForm);  
  23.         }  
  24.  
  25.         private static void InitWorkItem()  
  26.         {  
  27.             WorkItem.InitWorkItem();  
  28.  
  29.             AddServices();  
  30.  
  31.             AddModules();  
  32.         }  
  33.  
  34.  
  35.  
  36.         private static void AddModules()  
  37.         {  
  38.             WorkItem.ShellForm.AppMenuName = "程序";  
  39.  
  40.             WorkItem.ShellForm.ExitString = "退出";  
  41.  
  42.             AddCustomModule();  
  43.         }  
  44.  
  45.         private static void AddCustomModule()  
  46.         {  
  47.             ToolStripMenuItem _btnCutomMenuItem = new ToolStripMenuItem("自定義模塊");  
  48.  
  49.             ToolStripItem _btnShowMyForm = new ToolStripButton("彈出");  
  50.  
  51.             _btnShowMyForm.Click += new EventHandler(ShowMyForm_Click);  
  52.  
  53.             _btnCutomMenuItem.DropDownItems.Add(_btnShowMyForm);  
  54.  
  55.             WorkItem.ShellForm.ShellMenu.Items.Add(_btnCutomMenuItem);  
  56.         }  
  57.  
  58.         private static void ShowMyForm_Click(object sender, EventArgs e)  
  59.         {  
  60.             MyForm mForm = new MyForm();  
  61.  
  62.             mForm.MdiParent = WorkItem.ShellForm;  
  63.  
  64.             mForm.Show();  
  65.         }  
  66.  
  67.         private static void AddServices()  
  68.         {  
  69.             IFormService service = WorkItem.Services.AddService("FormService"new FormService());  
  70.         }  
  71.     }  

首先:加載WorkItem添加InitWorkItem() 方法,將Bob.Library中的ShellForm 實(shí)例化。然后加載Service 和模塊 AddServices() 添加一個(gè)Key為FormService的IFormService 實(shí)例,該實(shí)例在MyForm中有用到。

  1. GetService  
  2.         private void btnGetGUID_Click(object sender, EventArgs e)  
  3.         {  
  4.             IFormService service = WorkItem.Services.GetServiceByKey("FormService");  
  5.             txtGUID.Text = service.GetGuidString();  
  6.         } 

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

【編輯推薦】

  1. 詳解TripleDES實(shí)現(xiàn)C# 加密操作
  2. 淺析C# WinForm控件開(kāi)發(fā)前期準(zhǔn)備
  3. 詳解C# WinForm自定義控件的使用和調(diào)試
  4. C# Attribute的概念與使用淺析
  5. C# AttributeUsage的使用淺析
責(zé)任編輯:彭凡 來(lái)源: 博客園
相關(guān)推薦

2023-07-03 07:39:43

Spring框架設(shè)計(jì)模式

2012-11-20 10:04:46

Winform開(kāi)發(fā)

2010-06-11 14:55:20

2013-04-23 09:31:12

Winform開(kāi)發(fā)框架

2010-06-13 09:15:16

WinForm窗體

2010-08-27 09:11:27

Python單元測(cè)試

2010-08-11 10:24:46

Flex開(kāi)發(fā)

2009-11-30 08:38:35

WinForm

2011-05-03 09:45:25

噴墨打印機(jī)假故障

2010-08-10 17:13:58

Flex技術(shù)

2017-02-27 09:36:01

AndroidMVVM架構(gòu)

2023-02-07 07:43:27

微服務(wù)應(yīng)用框架

2023-01-12 08:00:00

SpringClou微服務(wù)框架

2012-08-21 11:26:17

Winform

2011-11-14 10:41:15

Winform數(shù)據(jù)管理模塊Items

2010-03-16 14:50:49

Python web框

2009-04-13 09:23:41

.NET 2.0Winform經(jīng)驗(yàn)

2024-10-24 17:13:55

WinformUI多線程

2012-12-11 10:15:02

Winform開(kāi)發(fā)框架

2009-11-26 14:37:37

Visual Stud
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)