270 likes | 462 Views
MEF & Silverlight 4: Extensible A pplications. Applicazioni estensibili impiegando MEF e Silverlight 4. Davide Zordan – MVP Silverlight, Silverlight Insider. Cos’è MEF. Perché MEF. MEF: concetti fondamentali. Esportazione di una classe:. [Export ]
E N D
MEF & Silverlight 4: ExtensibleApplications Applicazioni estensibili impiegando MEF e Silverlight 4 • Davide Zordan – MVP Silverlight, Silverlight Insider
Cos’è MEF www.xedotnet.org
Perché MEF www.xedotnet.org
MEF: concetti fondamentali www.xedotnet.org
Esportazione di una classe: [Export] public partialclassShellView: UserControl { public ShellView() { InitializeComponent(); } } www.xedotnet.org
Esportazione tramite proprietà: public classLoggerpart { [Export] public ILogLogger { get{ returnLogManager.GetLogger("Logger"); } } } www.xedotnet.org
Importazione tramite proprieta' e campi [Export] public classViewFactory { [Import] public ShellViewshellView{ get; set; } } www.xedotnet.org
Importazione tramite costruttori [Export] public classViewFactory { [ImportingConstructor] public ViewFactory(ShellViewshellView) { } } www.xedotnet.org
Composizione Composizione mediante il CompositionInitializer: CompositionInitializer.SatisfyImports(this); www.xedotnet.org
PartCreationPolicy [PartCreationPolicy(CreationPolicy.NonShared)] [Export(typeof(IShellView))] public partialclassShellView: UserControl, IShellView { public ShellView() { } } www.xedotnet.org
Esportazioni multiple [Export] public classViewFactory { [ImportMany] IEnumerable<IView> Views { get; set; } } [Export(typeof(IView))] public partialclassShellView: UserControl, IView { } public interfaceIView{} www.xedotnet.org
Attributi personalizzati di esportazione [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] public classExportMainPageVMAttribute : ExportAttribute { public ExportMainPageVMAttribute(): base(typeof(ViewModelBase)) { } } www.xedotnet.org
Utilizzo public partial class MainPageView : UserControl { public MainPageView() { InitializeComponent(); CompositionInitializer.SatisfyImports(this); this.DataContext = mainPageViewModel; } [ImportMainPageVMAttribute] public object mainPageViewModel { get; set; } } www.xedotnet.org
Esportazioni multiple e Metadata public classSimpleMEFProvider { [Export("SimpleText")] [ExportMetadata("Index",0)] public string aSimpleTextProvider { get { return "This is a simple Text MEF Imported"; } } [Export("SimpleText")] [ExportMetadata("Index",1)] public string anotherSimpleTextProvider { get { return "This is another simple Text MEF Imported"; } } } www.xedotnet.org
Import Metadata public partialclassMainPage : UserControl { public MainPage() { InitializeComponent(); CompositionInitializer.SatisfyImports(this); } //Multiple Exports detected: use ImportMany [ImportMany("SimpleText")] public IEnumerable<Lazy<string,IMetadataContent>> textProperty { get; set; } //Used to retrieve the value of the Metadata Exported by MEF public interfaceIMetadataContent { int Index { get; } } } www.xedotnet.org
Demo 1: Simple Composition Ok OK, show me the samples www.xedotnet.org
Demo 1: Export, Import, Compose! Demo 1: EXPORT, IMPORT, COMPOSE! www.xedotnet.org
Demo 2: Export multipli Demo 2: Export multipli www.xedotnet.org
Demo 3: Export multipli Demo 3: Export metadata www.xedotnet.org
Demo 4: Simple MEF-MVVM Demo 4: simplemef-mvvm www.xedotnet.org
Demo 5: Simple MEF-MVVM Demo 5: attributi custom www.xedotnet.org
Demo 6: Export Factory Demo 6: export factory www.xedotnet.org
Demo 7: MEF & (PRISM) EventAggregator Demo 7: MEF & (prism) eventaggregator www.xedotnet.org
Demo 8: MEF & DeploymentCatalog Demo 8: MEF & Deployment Catalog www.xedotnet.org
Riepilogo www.xedotnet.org
Link How to reach me • http://www.davidezordan.net/blog • info@davidezordan.net • http://silverlightexperts.net • http://twitter.com/davidezordan www.xedotnet.org