Created
April 29, 2015 02:18
-
-
Save dbuksbaum/10aff3f7aed7512c8ea9 to your computer and use it in GitHub Desktop.
Caliburn.Micro the MEFtacluar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <Application x:Class="HelloMef.App" | |
| xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
| xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
| xmlns:local="clr-namespace:HelloMef"> | |
| <Application.Resources> | |
| <ResourceDictionary> | |
| <ResourceDictionary.MergedDictionaries> | |
| <ResourceDictionary> | |
| <local:MefBootStrapper x:Key="bootstrapper" /> | |
| </ResourceDictionary> | |
| </ResourceDictionary.MergedDictionaries> | |
| </ResourceDictionary> | |
| </Application.Resources> | |
| </Application> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.ComponentModel.Composition; | |
| using System.Linq; | |
| using System.Text; | |
| using Caliburn.Micro; | |
| using HelloMef.Contracts; | |
| namespace HelloMef.ViewModels | |
| { | |
| [Export(typeof(IShell))] | |
| public class MainViewModel : PropertyChangedBase, IShell | |
| { | |
| private string _name; | |
| private string _helloString; | |
| public string Name | |
| { | |
| get { return _name; } | |
| set | |
| { | |
| _name = value; | |
| NotifyOfPropertyChange(() => Name); | |
| NotifyOfPropertyChange(() => CanSayHello); | |
| } | |
| } | |
| public string HelloString | |
| { | |
| get { return _helloString; } | |
| private set | |
| { | |
| _helloString = value; | |
| NotifyOfPropertyChange(() => HelloString); | |
| } | |
| } | |
| public bool CanSayHello | |
| { | |
| get { return !string.IsNullOrWhiteSpace(Name); } | |
| } | |
| public void SayHello(string name) | |
| { | |
| HelloString = string.Format("Hello {0}.", Name); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.ComponentModel.Composition; | |
| using System.ComponentModel.Composition.Hosting; | |
| using System.ComponentModel.Composition.Primitives; | |
| using System.Linq; | |
| using System.Reflection; | |
| using Caliburn.Micro; | |
| using HelloMef.Contracts; | |
| namespace HelloMef | |
| { | |
| public class MefBootStrapper : Bootstrapper<IShell> | |
| { | |
| #region Fields | |
| private CompositionContainer _container; | |
| #endregion | |
| #region Overrides | |
| protected override void Configure() | |
| { // configure container | |
| #if SILVERLIGHT | |
| _container = CompositionHost.Initialize( | |
| new AggregateCatalog(AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>())); | |
| #else | |
| _container = new CompositionContainer( | |
| new AggregateCatalog(AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>())); | |
| #endif | |
| var batch = new CompositionBatch(); | |
| batch.AddExportedValue<IWindowManager>(new WindowManager()); | |
| batch.AddExportedValue<IEventAggregator>(new EventAggregator()); | |
| batch.AddExportedValue(_container); | |
| _container.Compose(batch); | |
| } | |
| protected override object GetInstance(Type serviceType, string key) | |
| { | |
| string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key; | |
| var exports = _container.GetExportedValues<object>(contract); | |
| if (exports.Count() > 0) | |
| return exports.First(); | |
| throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract)); | |
| } | |
| protected override IEnumerable<object> GetAllInstances(Type serviceType) | |
| { | |
| return _container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType)); | |
| } | |
| protected override void BuildUp(object instance) | |
| { | |
| _container.SatisfyImportsOnce(instance); | |
| } | |
| #endregion | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment