详解Winform假框架的设计应用

开发 后端
在这里我们将介绍Winform假框架的设计应用,希望本文能对大家了解SCSF有所帮助。

对于Winform假框架的设计很多人不太理解,但是对于SCSF(Smart Client Software Factory)相信大家不太陌生,本文将从Winform假框架的设计应用开始谈起。

学习SCSF 有写日子了,对该框架有一些了解,于是自己脑子发热写了个假SCSF虽然不成熟,但是是对自己学习的一个总结。

主要框架示意图(解决方案):

主要框架示意图

Winform假框架概念:

1.整个系统共用一个WorkItem(工作单元).

2.WorkItem中有 Service集合.

3.初始默认使用ShellForm.

WorkItem:

WorkItem 是自定义的静态类,在程序启动时加载默认设置,当前是代码以后会使用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 中有一个 IServices 类型的属性 Services,该属性用于保存全局的Service,IService 有 AddService、GetServiceByKey、Clear 三个方法:实现 添加、获取、清空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. //实现  
  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 中还有一个 Shell 类型的ShellForm 属性:该属性是一个MDI窗口的实例,作为系统的父容器。

设计图:

[[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 中有一个菜单控件,一个状态栏控件,将两个控件作为属性发布。初始加载了一个菜单项 _AppMenu ,将菜单项的Text属性布.然后为_AppMenu 添加一个子菜单项ExitMenu 同时将他的Text属性发布。为_ExitMenu 添加事件 _ExitAppMenu_Click;然后发布一个方法 StatusUpdate(string message) 在状态栏显示提示消息。

准备工作完成,开始项目开发:首先创建一个普通的Winform项目,将 Bob.Library 应用进来,在系统开始类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 实例化。然后加载Service 和模块 AddServices() 添加一个Key为FormService的IFormService 实例,该实例在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() ,模拟的添加一个自定义模块,AddCustomModule(),为该模块添加独享的菜单,为该模块添加子菜单,
为子菜单绑定事件.

然后我们让程序开始Run 我们的 Shell   Application.Run(WorkItem.ShellForm);

原文标题:Winform 应用 【假框架】

链接:http://www.cnblogs.com/duoluodaizi/archive/2009/10/12/1582000.html

【编辑推荐】

  1. 详解TripleDES实现C# 加密操作
  2. 浅析C# WinForm控件开发前期准备
  3. 详解C# WinForm自定义控件的使用和调试
  4. C# Attribute的概念与使用浅析
  5. C# AttributeUsage的使用浅析
责任编辑:彭凡 来源: 博客园
相关推荐

2023-07-03 07:39:43

Spring框架设计模式

2012-11-20 10:04:46

Winform开发

2013-04-23 09:31:12

Winform开发框架

2010-06-11 14:55:20

2010-06-13 09:15:16

WinForm窗体

2010-08-27 09:11:27

Python单元测试

2010-08-11 10:24:46

Flex开发

2009-11-30 08:38:35

WinForm

2011-05-03 09:45:25

喷墨打印机假故障

2011-11-14 10:41:15

Winform数据管理模块Items

2010-08-10 17:13:58

Flex技术

2017-02-27 09:36:01

AndroidMVVM架构

2023-02-07 07:43:27

微服务应用框架

2012-08-21 11:26:17

Winform

2023-01-12 08:00:00

SpringClou微服务框架

2010-03-16 14:50:49

Python web框

2009-04-13 09:23:41

.NET 2.0Winform经验

2012-12-11 10:15:02

Winform开发框架

2009-11-26 14:37:37

Visual Stud

2009-10-10 14:54:44

treeView1控件
点赞
收藏

51CTO技术栈公众号