Created
February 14, 2011 15:34
-
-
Save jcdickinson/826043 to your computer and use it in GitHub Desktop.
Selectors for MEF
This file contains 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
[Export(typeof(LanguageSelector))] | |
[PartCreationPolicy(CreationPolicy.Shared)] | |
public class LanguageSelector : Selector<ILanguageProvider> | |
{ | |
protected override string IdentityFor(ILanguageProvider import) | |
{ | |
return import.Name; | |
} | |
protected override ILanguageProvider ImportFor(string identity) | |
{ | |
foreach (var item in base.Options) | |
{ | |
if (item.Name == identity) | |
return item; | |
} | |
return base.Options.First(); | |
} | |
} |
This file contains 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
/// <summary> | |
/// Event arguments about a selection change. | |
/// </summary> | |
public class SelectionChangedEventArgs<TImport> : EventArgs | |
{ | |
/// <summary> | |
/// Gets the new selection. | |
/// </summary> | |
public TImport NewSelection | |
{ | |
get; | |
private set; | |
} | |
/// <summary> | |
/// Initializes a new instance of the <see cref="SelectionChangedEventArgs<TImport>"/> class. | |
/// </summary> | |
public SelectionChangedEventArgs(TImport newSelection) | |
{ | |
NewSelection = newSelection; | |
} | |
} |
This file contains 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
/// <summary> | |
/// Represens a selector. | |
/// </summary> | |
/// <typeparam name="TImport">The type of the import.</typeparam> | |
public abstract class Selector<TImport> | |
{ | |
/// <summary> | |
/// Gets the options. | |
/// </summary> | |
[ImportMany(typeof(TImport))] | |
public IEnumerable<TImport> Options | |
{ | |
get; | |
private set; | |
} | |
private TImport _selectedValue; | |
/// <summary> | |
/// Gets the selected value. | |
/// </summary> | |
public TImport SelectedValue | |
{ | |
get | |
{ | |
return SelectedValue; | |
} | |
private set | |
{ | |
_selectedValue = value; | |
var temp = SelectionChanged; | |
if (temp != null) | |
{ | |
temp(this, new SelectionChangedEventArgs<TImport>(value)); | |
} | |
} | |
} | |
/// <summary> | |
/// Occurs when the selection is changed. | |
/// </summary> | |
public event EventHandler<SelectionChangedEventArgs<TImport>> SelectionChanged; | |
/// <summary> | |
/// Initializes a new instance of the <see cref="Selector<TImport>"/> class. | |
/// </summary> | |
public Selector() | |
{ | |
Monoflector.CompositionServices.ComposeParts(this); | |
} | |
protected abstract string IdentityFor(TImport import); // We need this for saving settings later. | |
protected abstract TImport ImportFor(string identity); | |
} |
This file contains 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
public class MustUseSelectedLanguage : IPartImportsSatisfiedNotification | |
{ | |
[Import(typeof(LanguageSelector))] | |
private LanguageSelector _selector; | |
void IPartImportsSatisfiedNotification.OnImportsSatisfied() | |
{ | |
_selector.SelectionChanged += new EventHandler<SelectionChangedEventArgs<ILanguageProvider>>(_selector_SelectionChanged); | |
_theCombo.SelectedItem = _selector.SelectedValue; | |
UpdateDissasemblers(); | |
} | |
void _selector_SelectionChanged(object sender, SelectionChangedEventArgs<ILanguageProvider> e) | |
{ | |
_theCombo.SelectedItem = _selector.SelectedValue; | |
UpdateDissasemblers(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment