Winform 应用 【假框架】

实例代码下载

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

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


概念:
      1.整个系统共用一个WorkItem(工作单元).
      2.WorkItem中有 Service集合.
      3.初始默认使用ShellForm.

WorkItem:
      WorkItem 是自定义的静态类,在程序启动时加载默认设置,当前是代码以后会使用XML配置。
WorkItem代码:

ContractedBlock.gif ExpandedBlockStart.gif 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
ExpandedBlockStart.gifContractedBlock.gif
{
    
public static class WorkItem
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
private static Shell _ShellForm;
        
private static IServices _Services;

        
public static void InitWorkItem()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            InitServices();
            InitShellForm();
        }



        
public static Shell ShellForm
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (_ShellForm == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    InitShellForm();
                }

                
return _ShellForm;
            }

        }


        
private static void InitShellForm()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            _ShellForm 
= new Shell();
        }



        
public static Bob.Library.Services.IServices Services
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (_Services == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    InitServices();
                }

                
return _Services;
            }

        }


        
private static void InitServices()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            _Services 
= new Services.Services();
        }


    }

}


WorkItem 中有一个 IServices 类型的属性 Services,该属性用于保存全局的Service,
IService 有 AddService<TService>、GetServiceByKey<TService>、Clear 三个方法:
      实现 添加、获取、清空Service操作。
代码:

 

ContractedBlock.gif ExpandedBlockStart.gif IServices Services
//接口
using System;
using System.Collections.Generic;
using System.Text;

namespace Bob.Library.Services
ExpandedBlockStart.gifContractedBlock.gif
{
    
public interface IServices
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        TService AddService
<TService>(string key,TService service) where TService : class;

        TService GetServiceByKey
<TService>(string key) where TService : class;

        
void Clear();
    }

}




//实现
using System;
using System.Collections.Generic;
using System.Text;

namespace Bob.Library.Services
ExpandedBlockStart.gifContractedBlock.gif
{
    
public class Services :IServices
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        IDictionary
<stringobject> _Services;

        
public Services()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            InitServices();
        }


        
private void InitServices()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            _Services 
= new Dictionary<stringobject>();
        }

    
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IServices#region IServices 

        
public TService  AddService<TService>(string key, TService service) where TService : class
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            _Services[key] 
= service;
            
return _Services[key] as TService;
        }


        
public TService  GetServiceByKey<TService>(string key) where TService : class
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
object service = _Services[key];
            
return service is TService ? service as TService : null;
        }


        
public void  Clear()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            InitServices();
        }


        
#endregion

    }

}


WorkItem 中还有一个 Shell 类型的ShellForm 属性:该属性是一个MDI窗口的实例,作为系统的父容器。
设计图:


代码:

ContractedBlock.gif ExpandedBlockStart.gif 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
ExpandedBlockStart.gifContractedBlock.gif
{
    
public partial class Shell : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
public Shell()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            InitializeComponent();
            _AppMenu.Text 
= AppMenuName;
            _ExitMenu.Text 
= ExitString;
        }


        
public string FormName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return this.ParentForm.Text;
            }

            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
this.ParentForm.Text = value;
            }

        }


        
        
public void StatusUpdate(string message)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            _ShellStatus.Text 
= message;
        }


        
private void _ExitAppMenu_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (MessageBox.Show("Exit ?""Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                Environment.Exit(
0);
            }

        }



ContractedSubBlock.gifExpandedSubBlockStart.gif        
MenuItemString#region MenuItemString
        
private string _AppName = "Application";
        
private string _ExitName = "Exit";
        
public string AppMenuName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return _AppName; }
            
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif            

                _AppName 
= value;
                _AppMenu.Text 
= _AppName;
            }

        }


        
public string ExitString
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return _ExitName; }
            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                _ExitName 
= value;
                _ExitMenu.Text 
= _ExitName;
            }

        }


        
#endregion


        
public MenuStrip ShellMenu
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return _ShellMenu;
            }

        }


    }

}


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

准备工作完成,开始项目开发:首先创建一个普通的winform项目,将 Bob.Library 应用进来,
在系统开始类 Program.cs 中添加 WorkItem的加载 代码如下:

ContractedBlock.gif ExpandedBlockStart.gif Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Bob.Library.WorkItems;
using ArchitectureDemo.Services;

namespace ArchitectureDemo
ExpandedBlockStart.gifContractedBlock.gif
{
    
static class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        [STAThread]
        
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Application.EnableVisualStyles();

            Application.SetCompatibleTextRenderingDefault(
false);

            InitWorkItem();

            Application.Run(WorkItem.ShellForm);
        }


        
private static void InitWorkItem()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            WorkItem.InitWorkItem();

            AddServices();

            AddModules();
        }




        
private static void AddModules()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            WorkItem.ShellForm.AppMenuName 
= "程序";

            WorkItem.ShellForm.ExitString 
= "退出";

            AddCustomModule();
        }


        
private static void AddCustomModule()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            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)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            MyForm mForm 
= new MyForm();

            mForm.MdiParent 
= WorkItem.ShellForm;

            mForm.Show();
        }


        
private static void AddServices()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            IFormService service 
= WorkItem.Services.AddService<IFormService>("FormService"new FormService());
        }

    }

}


首先: 加载WorkItem  添加InitWorkItem() 方法,将Bob.Library 中的ShellForm 实例化。
然后加载 Service 和 模块  
AddServices() 添加一个 Key 为 FormService 的 IFormService 实例,该实例在MyForm中有用到。

ContractedBlock.gif ExpandedBlockStart.gif GetService
        private void btnGetGUID_Click(object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
{
            IFormService service 
= WorkItem.Services.GetServiceByKey<IFormService>("FormService");
            txtGUID.Text 
= service.GetGuidString();
        }

AddModules() ,模拟的添加一个自定义模块,AddCustomModule(),为该模块添加独享的菜单,为该模块添加子菜单,
为子菜单绑定事件.

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

 

转载于:https://www.cnblogs.com/duoluodaizi/archive/2009/10/12/1582000.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值