/// <summary> /// Adds a disabled behavior configuration to the end of the chain. /// </summary> internal void Append(BehaviorKey key) { Check.NotNull(key, nameof(key)); RequireNotSealed(); _items.Add(new BehaviorChainItemConfiguration(key)); }
/// <summary> /// Creates a new <see cref="BehaviorChainConfiguration"/> from this /// template. /// </summary> // TODO: Comment. internal BehaviorChainConfiguration CreateConfiguration( IBehaviorFactoryConfiguration factoryConfiguration ) { Check.NotNull(factoryConfiguration, nameof(factoryConfiguration)); var config = new BehaviorChainConfiguration(); IBehaviorFactory factory = factoryConfiguration.GetFactory(_factoryProvider); foreach (BehaviorChainItemTemplate itemTemplate in _itemTemplates) { BehaviorKey key = itemTemplate.Key; if (itemTemplate.State == DefaultBehaviorState.DisabledWithoutFactory) { config.Append(key); } else { IBehavior instance = factory.Create(key); config.Append(key, instance); if (itemTemplate.State == DefaultBehaviorState.Enabled) { config.Enable(key); } } } return(config); }
/// <inheritdoc /> ISinglePropertyBehaviorBuilder <TVM, TDescriptor, TValue> ISinglePropertyBehaviorBuilder <TVM, TDescriptor, TValue> .Configure <TBehavior>( BehaviorKey key, Action <TBehavior> configurationAction ) { _propertyConfiguration.ConfigureBehavior <TBehavior>(key, configurationAction); return(this); }
/// <inheritdoc /> ISinglePropertyBehaviorBuilder <TVM, TDescriptor, TValue> ISinglePropertyBehaviorBuilder <TVM, TDescriptor, TValue> .Enable( BehaviorKey key, IBehavior behaviorInstance ) { _propertyConfiguration.Enable(key, behaviorInstance); return(this); }
// Todo: Should this be public ? public ViewModelBehaviorBuilder <TVM, TDescriptor> Configure <TBehavior>( BehaviorKey key, Action <TBehavior> configurationAction ) where TBehavior : IBehavior { _configuration.ViewModelConfiguration.ConfigureBehavior <TBehavior>(key, configurationAction); return(this); }
// Todo: Should this be public ? public ViewModelBehaviorBuilder <TVM, TDescriptor> Enable( BehaviorKey key, IBehavior behaviorInstance = null ) { _configuration.ViewModelConfiguration.Enable(key, behaviorInstance); return(this); }
/// <summary> /// Calls <see cref="BehaviorChainConfiguration.Enable"/> on /// all configurations contained by this collection that contain the /// specified '<paramref name="key"/>'. /// </summary> public void Enable(BehaviorKey key) { Check.NotNull(key, nameof(key)); foreach (BehaviorChainConfiguration config in _propertyConfigurations.Values) { config.Enable(key); } }
public TBehavior GetBehavior <TBehavior>(BehaviorKey withKey) where TBehavior : IBehavior { Check.NotNull(withKey, nameof(withKey)); Check.Requires <InvalidOperationException>(Contains(withKey)); RequireNotSealed(); BehaviorChainItemConfiguration item = GetItem(withKey); return((TBehavior)item.Instance); }
/// <summary> /// Removes the behavior defined by '<paramref name="key"/>' from the behavior /// chain returned by <see cref="CreateChain"/>. /// </summary> public void Disable(BehaviorKey key) { Check.NotNull(key, nameof(key)); Check.Requires <InvalidOperationException>(Contains(key)); RequireNotSealed(); var item = GetItem(key); item.IsEnabled = false; }
public void AddChangeHandler(Action <TVM, ChangeArgs> changeHandler) { // TODO: Make this more official... var key = new BehaviorKey("ChangeListener"); _configuration.ViewModelConfiguration.Append(key); _configuration.ViewModelConfiguration.Enable( key, new ChangeListenerBehavior <TVM>(changeHandler) ); }
public void AddBehavior(IBehavior behaviorInstance, BehaviorKey key = null) { string keyString = String.Format( "{0} (manually configured)", TypeService.GetFriendlyTypeName(behaviorInstance) ); key = key ?? new BehaviorKey(keyString); Configuration.ViewModelConfiguration.Append(key, behaviorInstance); Configuration.ViewModelConfiguration.Enable(key, behaviorInstance); }
/// <summary> /// Calls <see cref="BehaviorChainConfiguration.ConfigureBehavior"/> on /// all configurations contained by this collection that contain the /// specified '<paramref name="key"/>'. /// </summary> public void ConfigureBehavior <T>( BehaviorKey key, Action <T> configurationAction ) where T : IBehavior { Check.NotNull(key, nameof(key)); Check.NotNull(configurationAction, nameof(configurationAction)); foreach (BehaviorChainConfiguration config in _propertyConfigurations.Values) { config.ConfigureBehavior(key, configurationAction); } }
/// <summary> /// Adds a disabled behavior configuration to the top of the chain. /// </summary> internal void Prepend(BehaviorKey key, IBehavior instance) { Check.NotNull(key, nameof(key)); Check.NotNull(instance, nameof(instance)); RequireNotSealed(); var item = new BehaviorChainItemConfiguration(key) { Instance = instance }; _items.Insert(0, item); }
/// <inheritdoc /> ISinglePropertyBehaviorBuilder <TVM, TDescriptor, TValue> ISinglePropertyBehaviorBuilder <TVM, TDescriptor, TValue> .AddChangeHandler(Action <TVM, ChangeArgs> changeHandler) { // TODO: Make this more official... var key = new BehaviorKey("ChangeListener"); Configuration.ViewModelConfiguration.Append(key); Configuration.ViewModelConfiguration.Enable( key, new ChangeListenerBehavior <TVM>(changeHandler) ); return(this); }
/// <summary> /// Returns a new <see cref="BehaviorChainTemplate"/> with a behavior /// chain item template added to the end of the item template list of /// this behavior chain template. /// </summary> /// <param name="state"> /// If 'Disabled', the behavior is not enabled in a <see cref="BehaviorChainConfiguration"/> /// created from this template. This means it will not be included in the finally /// created <see cref="BehaviorChain"/> unless <see cref="BehaviorChainConfiguration.Enable"/> /// is called. /// </param> public BehaviorChainTemplate Append( BehaviorKey key, DefaultBehaviorState state = DefaultBehaviorState.Enabled ) { Check.NotNull(key, nameof(key)); var itemTemplatesClone = new List <BehaviorChainItemTemplate>(_itemTemplates); itemTemplatesClone.Add(new BehaviorChainItemTemplate(key, state)); return(new BehaviorChainTemplate(_factoryProvider, itemTemplatesClone)); }
private BehaviorChainItemConfiguration GetItem(BehaviorKey key) { var item = _items.Find(x => x.Key == key); if (item == null) { throw new ArgumentException( ExceptionTexts.BehaviorKeyNotInConfiguration.FormatWith(key) ); } return(item); }
ISinglePropertyBehaviorBuilder <TVM, TDescriptor, TValue> ISinglePropertyBehaviorBuilder <TVM, TDescriptor, TValue> .PrependBehavior( IBehavior behaviorInstance, BehaviorKey key ) { string keyString = String.Format( "{0} (manually configured)", TypeService.GetFriendlyTypeName(behaviorInstance) ); key = key ?? new BehaviorKey(keyString); _propertyConfiguration.Prepend(key, behaviorInstance); _propertyConfiguration.Enable(key, behaviorInstance); return(this); }
/// <summary> /// Calls the '<paramref name="configurationAction"/>' with the behavior /// specified by '<paramref name="key"/>' that will be inserted in the /// behavior chain. This method implicitly calls <see /// cref="Enable"/>. /// </summary> /// <typeparam name="T"> /// The type of the behavior to configure. This may be the concrete type /// of the behavior or a base type/interface. /// </typeparam> public void ConfigureBehavior <T>( BehaviorKey key, Action <T> configurationAction ) where T : IBehavior { Check.NotNull(key, nameof(key)); Check.Requires <InvalidOperationException>(Contains(key)); Check.NotNull(configurationAction, nameof(configurationAction)); RequireNotSealed(); BehaviorChainItemConfiguration item = GetItem(key); Enable(key); T behavior = (T)item.Instance; configurationAction(behavior); }
/// <summary> /// Makes sure that the behavior defined by '<paramref name="key"/>' gets /// included in the behavior chain returned by <see cref="CreateChain"/>. /// </summary> /// <param name="behaviorInstance"> /// Specifies or overrides the default behavior instance provided by the /// <see cref="BehaviorChainTemplate"/>. This parameter is required if /// the template has defined the behavior with the <see /// cref="DefaultBehaviorState.DisabledWithoutFactory"/> option. /// </param> public void Enable(BehaviorKey key, IBehavior behaviorInstance = null) { Check.NotNull(key, nameof(key)); Check.Requires <InvalidOperationException>(Contains(key)); RequireNotSealed(); var item = GetItem(key); if (behaviorInstance != null) { item.Instance = behaviorInstance; } else { if (item.Instance == null) { throw new ArgumentException( ExceptionTexts.CannotEnableBehavior.FormatWith(key) ); } } item.IsEnabled = true; }
/// <inheritdoc /> ISinglePropertyBehaviorBuilder <TVM, TDescriptor, TValue> ISinglePropertyBehaviorBuilder <TVM, TDescriptor, TValue> .Disable(BehaviorKey key) { _propertyConfiguration.Disable(key); return(this); }
bool ISinglePropertyBehaviorBuilder <TVM, TDescriptor, TValue> .ContainsBehavior(BehaviorKey key) { return(_propertyConfiguration.Contains(key)); }
public BehaviorChainItemTemplate(BehaviorKey key, DefaultBehaviorState state) { _key = key; _state = state; }
public BehaviorChainItemConfiguration(BehaviorKey key) { Key = key; IsEnabled = false; }
public bool Contains(BehaviorKey key) { Check.NotNull(key, nameof(key)); return(_items.Find(x => x.Key == key) != null); }