public void Test_WeakNotify_OneWay() { var viewModel = new TestViewModel(); var view = new TestView(); int changedCount = 0; BindingValueChangedHandler changeHandler = (source, args) => { changedCount++; Assert.IsTrue(args.Data != null); Assert.IsNull(args.OldValue); Assert.IsNull(args.NewValue); }; new WeakNotifyBinding(viewModel, "Age", view, null) .Initialize <WeakNotifyBinding>() .AttachSourceEvent("TestViewEvent") .OfType <WeakNotifyBinding>().SetSourceChanged(changeHandler); Assert.AreEqual(0, changedCount); GC.Collect(); view.RaiseTestViewEvent(); Assert.AreEqual(1, changedCount); }
/// <summary> /// Clear the instance. /// </summary> public virtual void Clear() { this.BindTarget.Clear(); this.BindSource.Clear(); this.sourceChanged = null; this.targetChanged = null; this.DetachTargetEvent(); this.DetachSourceEvent(); }
/// <summary> /// Sets the source changed. /// </summary> /// <param name="sourceChangedHandler"> /// The source changed handler. /// </param> /// <param name="isWeakReference"> /// Indicates whether the <see cref="WeakNotifyBinding"/> should hold the sourceChangedHandler in <see cref="WeakReference"/>, in this case, the callee should hold the strong reference of sourceChangedHandler to prevent it collected by GC. /// </param> /// <returns> /// The <see cref="WeakBinding"/>. /// </returns> public WeakBinding SetSourceChanged(BindingValueChangedHandler sourceChangedHandler, bool isWeakReference = false) { if (isWeakReference) { this.weakSourceChanged = new WeakReference(sourceChangedHandler); } else { this.sourceChanged = sourceChangedHandler; } return(this); }
/// <summary> /// Sets the target changed. /// </summary> /// <param name="targetChangedHandler"> /// The target changed handler. /// </param> /// <param name="isWeakReference"> /// Indicates whether the <see cref="WeakNotifyBinding"/> should hold the targetChangedHandler in <see cref="WeakReference"/>, in this case, the callee should hold the strong reference of targetChangedHandler to prevent it collected by GC. /// </param> /// <returns> /// The <see cref="WeakBinding"/>. /// </returns> public WeakBinding SetTargetChanged(BindingValueChangedHandler targetChangedHandler, bool isWeakReference = false) { if (isWeakReference) { this.weakTargetChanged = new WeakReference(targetChangedHandler); } else { this.targetChanged = targetChangedHandler; } return(this); }
public void Test_WeakNotify_WeakReference() { var button = new Button(); var view = new TestView(); int changedCount = 0; int targetCount = 0; BindingValueChangedHandler sourceChanged = (source, args) => { changedCount++; }; BindingValueChangedHandler targetChanged = (source, args) => { targetCount++; }; new WeakNotifyBinding(view, null, button, null) .Initialize <WeakNotifyBinding>() .AttachTargetEvent("TestViewEvent") .AttachSourceEvent("Click") .OfType <WeakNotifyBinding>() .SetTargetChanged(sourceChanged, true) .SetSourceChanged(targetChanged, true); Assert.AreEqual(0, changedCount); view.RaiseTestViewEvent(); Assert.AreEqual(1, changedCount); button.PerformClick(); Assert.AreEqual(1, targetCount); sourceChanged = null; GC.Collect(); view.RaiseTestViewEvent(); Assert.AreEqual(1, changedCount); targetChanged = null; GC.Collect(); button.PerformClick(); Assert.AreEqual(1, targetCount); }
/// <summary> /// Sets the source changed. /// </summary> /// <typeparam name="T"> /// The concrete <see cref="WeakBinding"/> to return. /// </typeparam> /// <param name="sourceChangedHandler"> /// The source changed. /// </param> /// <param name="isWeakReference"> /// Indicates whether the <see cref="WeakNotifyBinding"/> should hold the sourceChangedHandler in <see cref="WeakReference"/>, in this case, the callee should hold the strong reference of sourceChangedHandler to prevent it collected by GC. /// </param> /// <returns> /// The <see cref="T"/>. /// </returns> public T SetSourceChanged <T>(BindingValueChangedHandler sourceChangedHandler, bool isWeakReference = false) where T : WeakBinding { return(this.SetSourceChanged(sourceChangedHandler, isWeakReference) as T); }
/// <summary> /// Sets the target changed. /// </summary> /// <typeparam name="T"> /// The concrete <see cref="WeakBinding"/> to return. /// </typeparam> /// <param name="targetChangedHandler"> /// The source changed. /// </param> /// <param name="isWeakReference"> /// Indicates whether the <see cref="WeakNotifyBinding"/> should hold the targetChangedHandler in <see cref="WeakReference"/>, in this case, the callee should hold the strong reference of targetChangedHandler to prevent it collected by GC. /// </param> /// <returns> /// The <see cref="T"/>. /// </returns> public T SetTargetChanged <T>(BindingValueChangedHandler targetChangedHandler, bool isWeakReference = false) where T : WeakBinding { return(this.SetTargetChanged(targetChangedHandler) as T); }