/// <summary> /// Constructor - registers with the message mediator and hooks up any imports/exports /// with the default MEF catalog /// </summary> public ViewModel() { // Register with the message mediator - this will locate and bind all message // targets on this instance. Shouldn't really need to check for existence - it should // always be present, but just in case someone Unregisters it.. var mediator = Resolve <IMessageMediator>(); if (mediator != null) { mediator.Register(this); } // Hook up any MEF imports/exports IDynamicResolver loader = Resolve <IDynamicResolver>(); if (loader != null) { try { loader.Compose(this); } catch (CompositionException) { // Can throw if in invalid state - i.e. creating VM on behalf of a view. } } }
/// <summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// </summary> public void Dispose() { lock (_lock) { var id = _serviceContainer as IDisposable; if (id != null) { id.Dispose(); } _serviceContainer = null; _mefLoader = null; } }
/// <summary> /// Processes the dynamically bound properties. /// </summary> /// <remarks> /// This is a fairly inefficient implementation for the first cut, it would be /// nicer if a buffer could be reused to avoid reallocating huge strings for /// every single property, especially in the case of XML. /// </remarks> /// <param name="context">The Preprocessing context.</param> /// <param name="content">The content.</param> /// <returns>The buffer with any dynamically bound properties replaced</returns> private string ProcessDynamicallyBoundProperties(PreprocessingContext context, string content) { string resultBuffer = content; foreach (PreprocessingProperty property in context.Properties) { if (context.IsDynamicProperty(property.Key)) { IDynamicResolver resolver = property.GetDynamicResolver(context); if (resolver.ShouldProcess(context.SourceFile)) { string replacementValue = ResolveProperties(property.Value, context); resultBuffer = resolver.Replace(resultBuffer, replacementValue); } } } return(resultBuffer); }
public static void RegisterDynamicResolver(IDynamicResolver resolver) { dynamicResolvers.Add(resolver); }