/// <summary>Adds a trigger for a specified property path on this object.</summary> /// <param name="source">The trigger source object.</param> /// <param name="path">The path to the trigger's source property or properties. Use a semicolon (;) to separate multiple /// property paths. The property path has a flexible syntax that supports defining arbitrarily deep trigger properties. /// See <see cref="BplPropertyPath"/> for details.</param> /// <param name="handler">The handler to invoke whenever the trigger property or properties change.</param> /// <remarks>The handler is invoked at least once, during AddTrigger call itself, representing "unset value" to "current value" change.</remarks> public static void AddTrigger(this BplObject source, string path, BplTriggerHandler handler) { try { if (source == null) throw new Exception("Trigger source is missing"); if (handler == null) throw new Exception("Trigger handler is missing"); var trigger = BplBindingTrigger.Create(source, path, handler); source.Triggers.Add(trigger); trigger.IsEnabled = true; } catch (Exception e) { BplRuntimeException.Throw("Failed to create trigger '{0}'. {1}".Substitute(path, e.Message), e, source); } }
/// <summary>Creates a new <see cref="BplTrigger"/> instance.</summary> protected BplTrigger(BplObject source, string path, BplTriggerHandler handler) { PathString = path; var pathStrings = PathString.Split(@"\s*\;\s*"); var pathCollection = new List<BplPropertyPath>(); for (var i = 0; i < pathStrings.Length; i++) { pathCollection.Add(new BplPropertyPath(pathStrings[i].Trim())); } PathCollection = new ReadOnlyCollection<BplPropertyPath>(pathCollection); if (PathCollection.Count == 0) { BplRuntimeException.Throw("Path string is empty"); } Source = source; TargetMethod = handler.Method; var targetObject = handler.Target; if (targetObject != null) { _targetObject = new WeakReference(targetObject); _isInstanceHandler = true; } }
/// <summary>Removes all triggers with the given handler from this object.</summary> public static void RemoveTriggers(this BplObject source, BplTriggerHandler handler) { source.RemoveTriggers(t => t.TargetMethod == handler.Method); }
/// <summary>Creates a new <see cref="BplBindingTrigger"/> instance.</summary> public static BplTrigger Create(BplObject source, string path, BplTriggerHandler handler) { return new BplBindingTrigger(source, path, handler); }
/// <summary>Creates a new <see cref="BplBindingTrigger"/> instance.</summary> protected BplBindingTrigger(BplObject source, string path, BplTriggerHandler handler) : base(source, path, handler) { }