/// <summary> /// Sets the binding. /// </summary> /// <typeparam name="T"> /// The concrete <see cref="WeakBinding"/> to return. /// </typeparam> /// <param name="targetProperty"> /// The target property. /// </param> /// <param name="source"> /// The source. /// </param> /// <param name="sourceProperty"> /// The source property. /// </param> /// <param name="activate"> /// if set to <c>true</c> activate. /// </param> /// <returns> /// The <see cref="T"/>. /// </returns> /// <exception cref="System.NotSupportedException"> /// The new binding is not compatible with the existing binding. /// </exception> public T SetBinding <T>(string targetProperty, object source, string sourceProperty, bool activate = true) where T : WeakBinding { var entry = new WeakEntry(targetProperty, source, sourceProperty); WeakBinding binding; if (this.bindings.ContainsKey(entry)) { binding = this.bindings[entry]; if (typeof(T) != binding.GetType()) { throw new NotSupportedException( string.Format( "The new {0} is not compatible with the existing {1}, ", typeof(T), binding.GetType()) + "please clear the binding and rebind or update WeakTarget.SetBinding method to clear the existing binding first"); } binding.Update(targetProperty, source, sourceProperty); } else { binding = DynamicEngine.CreateInstance <T>(this.Target, targetProperty, source, sourceProperty); binding.Initialize <T>(activate); this.bindings.Add(entry, binding); } return(binding as T); }
/// <summary> /// Updates the enable values. /// </summary> /// <returns><c>true</c> if enable, <c>false</c> otherwise.</returns> private bool UpdateEnableValues() { object target = this.BindSource.Value; bool enable = target != null && InvokeCanExecuteChangedWithParameter(target, this.parameter.TryGetValue()); foreach (string property in this.enablesProperties) { DynamicEngine.SetProperty(this.BindTarget.Source, property, enable); } return(enable); }
/// <summary> /// Invokes this instance. /// </summary> public void Invoke() { object source = this.Context.Value; if (source != null) { object[] paras = this.Parameters != null?this.Parameters.Select(item => item.Value).ToArray() : null; var methodInfo = DynamicEngine.GetMethodInfo(source.GetType(), this.Method, paras, false); if (methodInfo != null) { DynamicEngine.InvokeMethod(source, methodInfo, paras); } } }
/// <summary> /// Handles the <see cref="E:AttachEventOccurred" /> event. /// </summary> /// <param name="sender">The sender.</param> /// <param name="args">The <see cref="EventArgs"/> instance containing the event data.</param> internal void OnAttachEventOccurred(object sender, EventArgs args) { if (!this.IsAlive || !this.eventTarget.IsNotNullAndAlive()) { this.DetachEvent(); return; } if (this.eventFilter != null && !this.eventFilter(sender, args)) { return; } DynamicEngine.InvokeMethod(this.Target, this.HandlerName, new[] { sender, args }); }
/// <summary> /// Determines whether this instance can invoke. /// </summary> /// <returns><c>true</c> if this instance can invoke; otherwise, <c>false</c>.</returns> /// <exception cref="MissingMemberException">The attach method does not return boolean value.</exception> public bool CanInvoke() { object source = this.Context.Value; if (source != null) { object[] paras = this.Parameters != null?this.Parameters.Select(item => item.Value).ToArray() : null; var methodInfo = DynamicEngine.GetMethodInfo(source.GetType(), this.Method, paras, false); if (methodInfo != null) { object value = DynamicEngine.InvokeMethod(source, methodInfo, paras); if (!(value is bool)) { throw new MissingMemberException( string.Format("The attach method {0} does not return Boolean value", this.Method)); } return((bool)value); } } return(false); }