コード例 #1
0
        /// <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));
        }
コード例 #2
0
        /// <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);
        }
コード例 #3
0
 /// <inheritdoc />
 ISinglePropertyBehaviorBuilder <TVM, TDescriptor, TValue> ISinglePropertyBehaviorBuilder <TVM, TDescriptor, TValue> .Configure <TBehavior>(
     BehaviorKey key,
     Action <TBehavior> configurationAction
     )
 {
     _propertyConfiguration.ConfigureBehavior <TBehavior>(key, configurationAction);
     return(this);
 }
コード例 #4
0
 /// <inheritdoc />
 ISinglePropertyBehaviorBuilder <TVM, TDescriptor, TValue> ISinglePropertyBehaviorBuilder <TVM, TDescriptor, TValue> .Enable(
     BehaviorKey key,
     IBehavior behaviorInstance
     )
 {
     _propertyConfiguration.Enable(key, behaviorInstance);
     return(this);
 }
コード例 #5
0
 // 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);
 }
コード例 #6
0
 // Todo: Should this be public ?
 public ViewModelBehaviorBuilder <TVM, TDescriptor> Enable(
     BehaviorKey key,
     IBehavior behaviorInstance = null
     )
 {
     _configuration.ViewModelConfiguration.Enable(key, behaviorInstance);
     return(this);
 }
コード例 #7
0
        /// <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);
            }
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        /// <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;
        }
コード例 #10
0
        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)
                );
        }
コード例 #11
0
        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);
        }
コード例 #12
0
        /// <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);
            }
        }
コード例 #13
0
        /// <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);
        }
コード例 #14
0
            /// <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);
            }
コード例 #15
0
        /// <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));
        }
コード例 #16
0
        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);
        }
コード例 #17
0
            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);
            }
コード例 #18
0
        /// <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);
        }
コード例 #19
0
        /// <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;
        }
コード例 #20
0
 /// <inheritdoc />
 ISinglePropertyBehaviorBuilder <TVM, TDescriptor, TValue> ISinglePropertyBehaviorBuilder <TVM, TDescriptor, TValue> .Disable(BehaviorKey key)
 {
     _propertyConfiguration.Disable(key);
     return(this);
 }
コード例 #21
0
 bool ISinglePropertyBehaviorBuilder <TVM, TDescriptor, TValue> .ContainsBehavior(BehaviorKey key)
 {
     return(_propertyConfiguration.Contains(key));
 }
コード例 #22
0
 public BehaviorChainItemTemplate(BehaviorKey key, DefaultBehaviorState state)
 {
     _key   = key;
     _state = state;
 }
コード例 #23
0
 public BehaviorChainItemConfiguration(BehaviorKey key)
 {
     Key       = key;
     IsEnabled = false;
 }
コード例 #24
0
        public bool Contains(BehaviorKey key)
        {
            Check.NotNull(key, nameof(key));

            return(_items.Find(x => x.Key == key) != null);
        }