




已阅读5页,还剩21页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
此文档收集于网络,如有侵权,请联系网站删除PRISM 4.0 TRAINING KITHands-On LabUsing MEF as the Dependency Injection ContainerLab version:1.0.0 Last updated:2/28/2020ContentsOVERVIEW3EXERCISE 1 - LOADING MODULES USING MEF CATALOGS4Task 1 Creating the Application Bootstrapper4Task 2 Loading Modules with Type Catalogs5Task 3 Adding a Region to the Shell6Task 4 Showing a view in the Shell7EXERCISE 2 - REGISTERING VIEWS USING THE VIEWEXPORT ATTRIBUTE9Task 1 Creating a view inside the shell project9Task 2 Decorating a view with the ViewExport Attribute10EXERCISE 3 - USING REMOTE MODULE LOADING12Task 1 Creating the new ModuleB13Task 2 Creating a XAML ModuleCatalog and Updating BootStrapper to Remotely Load ModuleB15EXERCISE 4 - MONITORING DOWNLOAD PROGRESS OF MODULES18Task 1 Configuring a Module to be Loaded on Demand18Task 2 Adding a Download Progress Indicator19OverviewThe Prism Library provides two options for dependency injection containers: Unity or MEF. Prism is extensible, thereby allowing other containers to be used instead with a little bit of work. Both Unity and MEF provide the same basic functionality for dependency injection, even though they work very differently. Some of the capabilities provided by both containers include the following: They both register types with the container. They both register instances with the container. They both imperatively create instances of registered types. They both inject instances of registered types into constructors. They both inject instances of registered types into properties. They both have declarative attributes for marking types and dependencies that need to be managed. They both resolve dependencies in an object graph. MEF provides several capabilities that Unity does not: It discovers assemblies in a directory. It uses XAP file download and assembly discovery. It recomposes properties and collections as new types are discovered. It automatically exports derived types. It is deployed with the .NET Framework.Exercise 1 - Loading Modules Using MEF CatalogsTask 1 Creating the Application BootstrapperIn this task, you will create a bootstrapper for your Prism application. Over the following tasks, you will add functionality to this bootstrapper class.1. Open the MEFCodeModuleLoadingBegin.sln solution located under the MEFExercise 1Begin directory of this training kit.2. Add references to the following assemblies to the Shell project. The assemblies are located under the Lib folder of this lab:a. Microsoft.Practices.Prism.dllb. Microsoft.Practices.Prism.MefExtensions.dllc. Microsoft.Practices.ServiceLocation.dlld. System.ComponentModel.Composition.dll3. Add a new class to the Shell project, named WorkshopBootstrapper.4. Add the following using statement at the top of the new class.C#using Microsoft.Practices.Prism.MefExtensions;5. Update the class signature to inherit from the MefBootstrapper class.C#public class WorkshopBootstrapper : MefBootstrapperNote: From the Prism documentation: “A bootstrapper is a class that is responsible for the initialization of an application built using the Prism Library. By using a bootstrapper, you have more control of how the Prism Library components are wired up to your application.” More information about this can be found here.6. Provide a fake implementation for the abstract CreateShell method, as shown in the following code.C#protected override DependencyObject CreateShell() return null;Note: Before the end of this exercise you will provide a real implementation for this method.7. In Visual Studio, save all changes.Task 2 Loading Modules with Type CatalogsIn this task you will create a class that implements the IModule interface (which is used to initialize modules). 1. Add a new class to the Shell project, named ModuleA.2. Add the following using statements to the ModuleA.cs file.C#using Microsoft.Practices.Prism.MefExtensions.Modularity;using Microsoft.Practices.Prism.Modularity;3. Decorate the ModuleA class with the ModuleExport attribute and make it implement the IModule interface, as shown in the following code.C#ModuleExport(typeof(ModuleA)public class ModuleA : IModuleNote: The ModuleExport attribute is used to make classes that implement the IModule interface automatically discoverable by MEF.4. Implement the interfaces Initialize method to display a message showing that the module has been loaded.C#public void Initialize() MessageBox.Show(Module A Loaded);5. Open the WorkshopBootstrapper.cs file.6. Add the following using statement at the top of the class.C#using System.ComponentModel.Composition.Hosting;7. Override the ConfigureAggregateCatalog method, to add ModuleA as a type recognizable by MEF.C#protected override void ConfigureAggregateCatalog() / Modules are added / registered in the aggregate catalog / More info: /en-us/library/ff921163(PandP.40).aspx base.ConfigureAggregateCatalog(); this.AggregateCatalog.Catalogs.Add(new TypeCatalog(new typeof(ModuleA) );8. Open the code behind file for App.xaml, and update the code for the Application_Startup method as shown in the following code.C#private void Application_Startup(object sender, StartupEventArgs e) new WorkshopBootstrapper().Run();9. In Visual Studio, press F5 to run the application. The message showing that the module has been loaded will appear, as shown in the following figure.Message showing that ModuleA has been loaded10. Click OK and close the browser window.Task 3 Adding a Region to the ShellIn this task you will add a control to the Shell and mark it as a region.1. Open the Shell.xaml file.2. Include the Microsoft.Practices.Prism.Regions namespace in the Shell.xaml file. This is shown in the following code.XAMLxmlns:regions=clr-namespace:Microsoft.Practices.Prism.Regions;assembly=Microsoft.Practices.Prism3. Add an ItemsControl to Shell.xaml (as a child to the Grid) and mark it as a Region named MainRegion. To do this, add the RegionManager.RegionName attached property to the control, as seen in the following code.C# Task 4 Showing a view in the ShellIn this task, you will display the Shell in the browser and add a view to its region.1. Open the code behind file for the Shell.2. Add the following using statement at the top of the file.C#using System.ComponentModel.Composition;3. Decorate the Shell class with the Export attribute, to make it discoverable for MEF.C#Exportpublic partial class Shell : UserControl public Shell() InitializeComponent(); 4. Open the WorkshopBootstrapper.cs file.5. Update the ConfigureAggregateCatalog to add the Shell as a type recognizable by MEF. This is shown in the following code.C#protected override void ConfigureAggregateCatalog() / Modules are added / registered in the aggregate catalog / More info: /en-us/library/ff921163(PandP.40).aspx base.ConfigureAggregateCatalog(); / Add the shell to the catalog so that it can be retrieved afterwards in the CreateShell method. this.AggregateCatalog.Catalogs.Add(new TypeCatalog(new typeof(Shell), typeof(ModuleA) ); Note: The ConfigureAggregateCatalog method gives you programmatic control over the registrations to be added to the AggregateCatalog.6. Update the CreateShell method to return as instance of the Shell class.C#protected override DependencyObject CreateShell() /Return an instance of the Shell, retrieving it from the MEF container. /As the shell has no dependencies (imports) it could just be created with new Shell(), but it is recommended to always export the shell accounting for future updates to shell dependencies. return this.Container.GetExportedValue();7. Override the InitializeShell method to set the Shell as the applications RootVisual element.C#protected override void InitializeShell() base.InitializeShell(); / Set the shell as the RootVisual (startup window in this case) of the application. Application.Current.RootVisual = (UIElement)this.Shell;8. Open the ModuleA.cs file.9. Add the following using statements at the top of the file.C#using System.ComponentModel.Composition;using Microsoft.Practices.Prism.Regions;10. Add an ImportingConstructor that imports an instance of the RegionManager and stores it in a field.C#private readonly IRegionManager regionManager;ImportingConstructorpublic ModuleA(IRegionManager regionManager) this.regionManager = regionManager;11. Update the Initialize method to show a TextBox in the region declared in the Shell.C#public void Initialize() TextBlock textBlock = new TextBlock(); textBlock.Text = Module A Loaded!. Module A is located on the Shell project and registered on the Aggregate catalog in the Bootstrapper.; this.regionManager.RegionsMainRegion.Add(textBlock);12. In Visual Studio, press F5 to run the application.13. Notice that the TextBox has been added to the Region.The TextBox has been added to the Region from the ModuleExercise 2 - Registering views using the ViewExport AttributeTask 1 Creating a view inside the shell projectIn this task you will create a view, which you will later register in a region through the ViewExport attribute.1. Open the MEFViewExportAttribute.sln solution located under the MEFExercise 3Begin folder of this training kit.2. In the Shell project, create a folder named Views.3. Add a new UserControl named ModuleAView to the Shell project, inside the Views folder.4. Add a TextBlock to the ModuleAView.xaml file, to display a message indicating that the view has been loaded.XAML View from Module A Loaded Task 2 Decorating a view with the ViewExport Attribute1. In the Prism.Workshop.Shell project create a folder named Infrastructure.2. Add the following classes from the Assets folder inside the Infrastructure folder: AutoPopulateExportedViewsBehavior.cs, IViewRegionRegistration.cs and ViewExportAttribute.cs. Those classes make it possible to register a view to a region through the use of a ViewExport attribute.3. Add the following using statements to the WorkshopBootstrapper.cs file.C#using Prism.Workshop.Shell.Infrastructure;using Prism.Workshop.Shell.Views;using Microsoft.Practices.Prism.Regions;4. In the ConfigureAggregateCatalog method delete the code that was used to add the Shell and ModuleA to the AggregateCatalog.C#/ Add the shell to the catalog so that it can be retrieved afterwards in the CreateShell method. this.AggregateCatalog.Catalogs.Add(new TypeCatalog(new typeof(Shell) );/ Add Module A to the catalogthis.AggregateCatalog.Catalogs.Add(new TypeCatalog(new typeof(ModuleA) );5. Update the ConfigureAggregateCatalog to add the Shell, the ModuleAView and the AutoPopulateExportedViewsBehavior as types recognizable by MEF.C#/ Add neccesary classes to the catalog this.AggregateCatalog.Catalogs.Add(new TypeCatalog(new typeof(Shell), / Add the shell to the catalog so that it can be retrieved afterwards in the CreateShell method. typeof(AutoPopulateExportedViewsBehavior), / Region behavior for finding viewExportstypeof(ModuleAView) / View of Module A.);Note: Alternatively, you could add the whole Prism.Workshop.Shell assembly to the AggregateCatalog, by adding the following code to the ConfigureAggregateCatalog method.this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Shell).Assembly);6. Override the ConfigureDefaultRegionBehaviors method to add the AutoPopulateExportedViewsBehavior, as shown in the following code.C#protected override IRegionBehaviorFactory ConfigureDefaultRegionBehaviors() / Add custom region behaviors. / More info: /en-us/library/gg430866(v=PandP.40).aspx var factory = base.ConfigureDefaultRegionBehaviors(); / behavior that registers all views decorated with the ViewExport attribute factory.AddIfMissing(AutoPopulateExportedViewsBehavior, typeof(AutoPopulateExportedViewsBehavior); return factory;7. Add the following using statements to the ModuleAView.cs file.C#using Prism.Workshop.Shell.Infrastructure;using System.ComponentModel.Composition;8. Decorate the ModuleAView class with the following attributes.C#PartCreationPolicy(CreationPolicy.NonShared) / creates a new instance of the view each time it is importedViewExport(RegionName = MainRegion) / registers the view with the MainRegion. More info: /en-us/library/ff921074(v=PandP.40).aspxpublic partial class ModuleAView : UserControl public ModuleAView() InitializeComponent(); Note: By default, MEF exports are created as singletons. That is, each time the part is imported, the same instance is returned. By setting the PartCreationPolicy to NonShared, a new instance is returned each time the part is imported.9. Delete the ModuleA.cs file, as it is no longer necessary.10. In Visual Studio, press F5 to run the application.11. Notice that the ModuleAView has been added to the MainRegion inside the Shell.The ModuleAView has been added to the MainRegionExercise 3 - Using Remote Module LoadingIn this exercise you will create a new module (ModuleB) on a separated project, and you will configure the bootstrapper in order to load the module remotely. The new module will add an additional view to the MainRegion, but in contrast with the view created on the previous exercises, the shell project will not have a reference to the ModuleB (or its views).You will observe that the necessary classes to support the ViewExport attributes, which on the previous exercises were located on the Shell project, now are in a separated Infrastructure project. This will allow views on any module to make use of the ViewExport attribute to register view on regions.Task 1 Creating the new ModuleBIn this task you will create a new module on a separated project. This project will contain the module class (ModuleB.cs), which will show a message box when it is initialized, and a view that will shows a simple label with its name.1. Open the MEFRemoteModuleLoading.sln solution located under the MEFExercise 3Begin folder of this training kit.2. On the MEFRemoteModuleLoading solution, create a new Silverlight Application project named Prism.Workshop.ModuleB. On the New Silverlight Application dialog, uncheck the Add a test page that references this application and make sure the project is hosted on the Prism.Workshop.Web project. This project will contain all ModuleBs assets.3. On the Prism.Workshop.ModuleB project, delete the App.xaml and MainPage.xaml files.4. On the Prism.Workshop.ModuleB project, add a new class named ModuleB. The ModuleB.cs file will open.5. On the Prism.Workshop.ModuleB project, add the following references:a. Microsoft.Practices.Prism.dllb. Microsoft.Practices.Prism.MefExtensions.dllc. System.ComponentModel.Composition.dllNote: Microsoft.Practices.Prism.dll and Microsoft.Practices.Prism.MefExtensions.dll are Prism libraries that can be found on the Lib folder on this Training Kit. System.ComponentModel.Composition.dll is part of .Net libraries.6. On the Properties for the Microsoft.Practices.Prism.MefExtensions.dll reference, set the Copy Local option to False.Note: If this step is not performed, the MEF libraries will be included (and imported by MEF) twice: one for the module and one for the Shell), causing an exception to be thrown. For more information you can read Chapter 4: Modular Application Development.7. In the ModuleB.cs file, add the following using statements:C#using System.Windows;using Microsoft.Practices.Prism.MefExtensions.Modularity;using Microsoft.Practices.Prism.Modularity;8. Update the ModuleB class signature to make the ModuleB inherit from the IModule interface, as shown below:C#public class ModuleB : IModule9. Implement the Initialize method on the ModuleB class. This will show a message box when the module is initialized.C#pub
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2026学年统编版(2024)小学语文三年级上册第一单元测试卷及答案
- 管理咨询公司合同付款管理办法
- 防暴反恐知识技能培训课件
- 城市文旅融合发展探索
- 2025年最简单土石方运输合同3篇
- 2025年高考政治总复习文化生活模块全套知识清单
- 知识图谱辅助关系抽取方法-洞察及研究
- 四川省成都市2025-2026学年七年级语文上学期第一次月考复习试卷(含答案)
- 2025-2026学年湖南省长沙市名校联考联合体高二(上)第一次联考(暨入学模拟考试)物理试卷(含答案)
- 部门生产安全培训纪要课件
- 河北建投集团招聘笔试题库2025
- 2025年自建房施工合同书 (包工不包料 C款)
- (高清版)DB33∕T 715-2018 公路泡沫沥青冷再生路面设计与施工技术规范
- 军事心理战试题及答案
- 托育园管理制度
- 2025年北京市第一次普通高中学业水平合格性考试历史试题(含答案)
- 2025年江西省高职单招文化统一考试真题及答案(网络版)
- 检验科消防安全知识培训
- 中国古代数学家求数列和的方法课件-高二上学期数学人教A版选择性
- 铁塔拆除施工方案
- DB3714-T 0010-2022 园林绿化养护管理规范
评论
0/150
提交评论