阅读: 1247 评论: 4 作者: 周 金根 发表于 2010-05-27 21:06 原文链接
在信息系统开发平台OpenExpressApp - 发布for .Net4版本中介绍过现在从以前的compositewpf改为.Net4自带的MEF来作为扩展应用机制,MEF的主要架构可以通过之前写的.Net4下的MEF(Managed Extensibility Framework) 架构简介来简单了解一下,对于compositewpf不了解的可以去参考它的官方网站。本篇我将介绍一下OpenExpressApp升级到for .Net4版本时如何从从compositewpf更改到MEF,想了解MEF的简单应用的也可以看看。
compositewpf在OpenExpressApp for .Net3的应用主要有模块装载以及界面组合两个应用,下面分别介绍一下这两方面之前如何实现的,后面会介绍这两部分如何在MEF下实现。
模块加载
<
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
> protected override IModuleCatalog GetModuleCatalog()
{
ModuleCatalog catalog = new ModuleCatalog();
// 制定装载OpenExpressApp模块
catalog.AddModule( typeof (ExpressAppModule));
// 从目录装载
Directory.CreateDirectory( @" .\Module " );
var dirCatalog = new DirectoryModuleCatalog() { ModulePath = @".\Module" };
dirCatalog.Initialize();
foreach (var module in dirCatalog.Modules)
{
catalog.AddModule(module);
}
// 等业务模块加载完毕在加载OpenExpressApp.Module.WPF模块。否则提前创建的DefaultOpen模块没有按钮。
catalog.AddModule( typeof (WPFModule));
return catalog;
}
<
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
> public abstract class XXXModule : Microsoft.Practices.Composite.Modularity. IModule
{
#region IModule Members
public virtual void Initialize()
{ ......
}
UI组合
<
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
> < AvalonDock:DockingManager x:Name = " _dockingManager " >
< AvalonDock:ResizingPanel >
< AvalonDock:DockablePane AvalonDock:ResizingPanel.ResizeWidth = " 228 " >
< AvalonDock:DockableContent cal:RegionManager.RegionName ="Tools" Title = " 模块列表 " />
AvalonDock:DockablePane >
< AvalonDock:ResizingPanel Orientation = " Vertical " >
< AvalonDock:DocumentPane cal:RegionManager.RegionName="Workspace" />
< AvalonDock:DockablePane cal:RegionManager.RegionName="Pads" />
AvalonDock:ResizingPanel >
AvalonDock:ResizingPanel >
AvalonDock:DockingManager >
<
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
> this ._regionManager.RegisterViewWithRegion(ShellRegionNames.Tools, typeof (ModuleListPad));
<
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
> public static object OpenModule(Type boType)
{
var regions = GetRegions();
IRegion moduleRegion = regions[ShellRegionNames.Tools];
IRegion mainRegion = regions[ShellRegionNames.Workspace];
var modulePad = moduleRegion.ActiveViews.First() as ModuleListPad;
BusinessObjectInfo selectItem = GetModule(boType, modulePad);
Debug.Assert(selectItem != null , " 未找到这个模块,不能打开! " );
// 左边的控件
modulePad.lbModule.SelectedItem = selectItem;
// 右边的控件
object view = CreateView(mainRegion, selectItem);
// 日志
AuditLogService.LogAsync( new AuditLogItem()
{
Title = " 打开模块: " + selectItem.Label,
ModuleName = selectItem.Label,
Type = AuditLogType.OpenModule
});
return view;
}
private static object CreateView(IRegion mainRegion, BusinessObjectInfo selectItem)
{
object view = null;
//如果已经打开则激活模块,否则新增模块窗体
if (OpenModules.TryGetValue(selectItem.BOType, out view) == false)
{
if (selectItem.ModuleUIType != null)
{
view = Activator.CreateInstance(selectItem.ModuleUIType);
}
else
{
view = CreateModuleForm(selectItem);
}
mainRegion.Add(view); //添加到区域
OpenModules.Add(selectItem.BOType, view);
}
mainRegion.Activate(view);
return view;
}
<
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
> private bool Compose()
{
var catalog = new AggregateCatalog();
//添加模块程序集目录
catalog.Catalogs.Add( new AssemblyCatalog(Assembly.GetExecutingAssembly()));
catalog.Catalogs.Add( new AssemblyCatalog(
Assembly.GetAssembly( typeof (OpenExpressApp.Module.ExpressAppModule))));
catalog.Catalogs.Add( new AssemblyCatalog(
Assembly.GetAssembly( typeof (OpenExpressApp.Module.WPF.SystemModule.WPFModule))));
if (Directory.Exists( @" .\Module " )) catalog.Catalogs.Add( new DirectoryCatalog( @" .\Module " ));
_container = new CompositionContainer(catalog);
_container.ComposeExportedValue < CompositionContainer > (_container);
//执行模块的初始化,注意:这里的IModule接口是在OpenExpressApp.Library中实现的
var modules = _container.GetExportedValues < IModule > ();
foreach (var module in modules)
{
module.Initialize();
}
CompositionBatch batch = new CompositionBatch();
batch.AddPart( this );
_container.Compose(batch);
return true ;
}
在App()中启动
<
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
> if (Compose())
{
// Application.Current.Run(_container.GetExportedValue ("OpenExpressApp.MainWindow"));
_container.GetExportedValue ( " OpenExpressApp.MainWindow " ).Show();
}
<
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
> [Export(ContractName.MainWindow, typeof(Window))]
public partial class Shell : Window, IPartImportsSatisfiedNotification
{
[Export(ContractName.Pads, typeof (ItemsControl))]
public ItemsControl Pads
{
get
{
return Pads;
}
}
[Export(ContractName.Workspace, typeof (IWorkspace))]
private DocumentPaneWorkSpace _Workspace
{
get
{
return new DocumentPaneWorkSpace(workspace);
}
}
<
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
> public partial class Shell : Window, IPartImportsSatisfiedNotification
{
[ImportMany(ContractName.Tools)]
public IEnumerable < Control > Tools { get ; set ; }
public void OnImportsSatisfied() //实现 IPartImportsSatisfiedNotification, 组合完毕后执行
{
foreach (var item in Tools)
this .tools.Items.Add( new DockableContent() { Content = item });
}
<
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
> [Export( typeof (IModule))]
public class WPFModule : AdaptCommandModule
{
[Import]
private CompositionContainer _compositionContainer = null ;
///
/// 把ModuleListPad.xaml加入到region中。
///
/// 加入ComboDataGrid.xaml到Resource中
///
public override void Initialize()
{
base .Initialize();
OpenApplication.CompositionContainer = _compositionContainer;
<
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
> public static IWorkspace Workspace
{
get
{
return CompositionContainer.GetExportedValue (ContractName.Workspace);
}
}
public static Control ModuleListPad
{
get
{
return CompositionContainer.GetExportedValue (ContractName.ModuleListPad);
}
}
public static ItemsControl Pads
{
get
{
return CompositionContainer.GetExportedValue (ContractName.Pads);
}
}
public static Selector ModuleListBox
{
get
{
return CompositionContainer.GetExportedValue (ContractName.ModuleListBox);
}
}
以上是迁移的主要修改部分,具体细节请大家去下载代码 信息系统开发平台OpenExpressApp - 发布for .Net4版本
更多内容: 开源信息系统开发平台之OpenExpressApp框架.pdf
欢迎转载,转载请注明:转载自周金根 [ http://zhoujg.cnblogs.com/ ]
评论: 4 查看评论 发表评论
沪江网招聘ASP.NET开发工程师
最新新闻:
· 谷歌收购广告公司Invite Media(2010-06-02 22:16)
· AT&T拟推新数据计划 iPad 3G用户不再享有无限(2010-06-02 21:36)
· 支持ARM架构:新版嵌入式Windows 7 CTP发布(2010-06-02 19:51)
· Apple的平台之路(三)(2010-06-02 19:27)
· Ubuntu 10.04可支持iPhone(2010-06-02 18:14)
编辑推荐:关于Java与.NET的讨论
网站导航:博客园首页 个人主页 新闻 闪存 小组 博问 社区 知识库