/// <summary> /// Clears the property binding. /// </summary> /// <param name="source">The source.</param> /// <param name="target">The target.</param> /// <param name="sourceProp">The source prop.</param> /// <param name="targetProp">The target prop.</param> public static void ClearPropertyBinding(Object source, INotifyPropertyChanged target, string sourceProp, string targetProp) { WeakEntry entry = new WeakEntry(source.GetType(), target.GetType(), sourceProp, targetProp); Delegate setAction = GetExpressionAction(entry, source, true); WeakSource.UnRegister(source, setAction, targetProp); }
public void WeakSource() { Foo foo = new Foo(); //WeakReference weakFoo = new WeakReference(foo); //Assert.Same(foo, weakFoo.Target); //foo = null; //GC.Collect(); //GC.WaitForPendingFinalizers(); //Assert.Null(weakFoo.Target); ISource <Foo> weakFoo = new WeakSource <Foo>(foo); Assert.Same(foo, weakFoo.Value); foo = null; GC.Collect(); GC.WaitForPendingFinalizers(); Assert.Null(weakFoo.Value); }
public void Get() { Foo foo = new Foo(); ISource <Foo> source = new WeakSource <Foo>(foo); Assert.Same(foo, source.Value); }
public void Get() { Foo foo = new Foo(); ISource<Foo> source = new WeakSource<Foo>(foo); Assert.Same(foo, source.Value); }
public void Set() { ISource <Foo> source = new WeakSource <Foo>(); Foo foo = new Foo(); source.Value = foo; Assert.Same(foo, source.Value); }
public void Set() { ISource<Foo> source = new WeakSource<Foo>(); Foo foo = new Foo(); source.Value = foo; Assert.Same(foo, source.Value); }
/// <summary> /// Sets the property binding. /// </summary> /// <param name="source">The source.</param> /// <param name="target">The target.</param> /// <param name="sourceProp">The source prop.</param> /// <param name="targetProp">The target prop.</param> /// <param name="notify">if set to <c>true</c> update immediately.</param> /// <param name="converter">The converter.</param> /// <param name="parameter">The converter parameter.</param> public static void SetPropertyBinding(Object source, INotifyPropertyChanged target, string sourceProp, string targetProp, bool notify = true, IDataConverter converter = null, object parameter = null) { WeakEntry entry = new WeakEntry(source.GetType(), target.GetType(), sourceProp, targetProp); Delegate setAction = GetExpressionAction(entry, source, true, converter); WeakSource wSource = WeakSource.Register(source, target, setAction, targetProp, converter, parameter); if (notify) { wSource.NotifyPropertyChanged(target, targetProp); } }
public void GarbageCollect() { Foo foo = new Foo(); ISource <Foo> source = new WeakSource <Foo>(foo); GC.Collect(); GC.WaitForPendingFinalizers(); Assert.Same(foo, source.Value); foo = null; GC.Collect(); GC.WaitForPendingFinalizers(); Assert.Null(source.Value); }
public void GarbageCollect() { Foo foo = new Foo(); ISource<Foo> source = new WeakSource<Foo>(foo); GC.Collect(); GC.WaitForPendingFinalizers(); Assert.Same(foo, source.Value); foo = null; GC.Collect(); GC.WaitForPendingFinalizers(); Assert.Null(source.Value); }
/// <summary> /// Registers the specified source. /// </summary> /// <param name="source">The source.</param> /// <param name="target">The target.</param> /// <param name="action">The action.</param> /// <param name="targetProp">The target prop.</param> /// <param name="converter">The converter.</param> /// <param name="parameter">The converter parameter.</param> /// <returns></returns> public static WeakSource Register(Object source, INotifyPropertyChanged target, Delegate action, string targetProp, IDataConverter converter = null, object parameter = null) { WeakSource wSource = _weakSources.ContainsKey(source.GetHashCode()) ? _weakSources[source.GetHashCode()] : null; if (wSource == null) { wSource = new WeakSource(source); _weakSources.Add(source.GetHashCode(), wSource); } IList <WeakAction> actions; if (wSource.Actions.ContainsKey(targetProp)) { actions = wSource.Actions[targetProp]; } else { actions = new List <WeakAction>(); wSource.Actions.Add(targetProp, actions); } actions.Add(new WeakAction(action, converter, parameter)); IList <string> props; if (!wSource.Targets.ContainsKey(target.GetHashCode())) { props = new List <string>(); props.Add(targetProp); wSource.Targets.Add(target.GetHashCode(), props); target.PropertyChanged += new PropertyChangedEventHandler(wSource.HandlePropertyChanged); } else { props = wSource.Targets[target.GetHashCode()]; if (!props.Contains(targetProp)) { props.Add(targetProp); } } return(wSource); }
/// <summary> /// Unregister the specified source. /// </summary> /// <param name="source">The source.</param> /// <param name="action">The action.</param> /// <param name="targetProp">The target prop.</param> public static void UnRegister(Object source, Delegate action, string targetProp) { WeakSource wSource = _weakSources.ContainsKey(source.GetHashCode()) ? _weakSources[source.GetHashCode()] : null; if (wSource != null && wSource.Actions.ContainsKey(targetProp)) { IList <WeakAction> actions = wSource.Actions[targetProp]; var wAction = actions.FirstOrDefault(item => item.Action == action); if (wAction != null) { actions.Remove(wAction); if (actions.Count == 0) { wSource.Actions.Remove(targetProp); } } if (wSource.Actions.Count() == 0) { _weakSources.Remove(wSource.GetHashCode()); } } }
/// <summary> /// Registers the specified source. /// </summary> /// <param name="source">The source.</param> /// <param name="target">The target.</param> /// <param name="action">The action.</param> /// <param name="targetProp">The target prop.</param> /// <param name="converter">The converter.</param> /// <param name="parameter">The converter parameter.</param> /// <returns></returns> public static WeakSource Register(Object source, INotifyPropertyChanged target, Delegate action, string targetProp, IDataConverter converter = null, object parameter = null) { WeakSource wSource = _weakSources.ContainsKey(source.GetHashCode()) ? _weakSources[source.GetHashCode()] : null; if (wSource == null) { wSource = new WeakSource(source); _weakSources.Add(source.GetHashCode(), wSource); } IList<WeakAction> actions; if (wSource.Actions.ContainsKey(targetProp)) { actions = wSource.Actions[targetProp]; } else { actions = new List<WeakAction>(); wSource.Actions.Add(targetProp, actions); } actions.Add(new WeakAction(action, converter, parameter)); IList<string> props; if (!wSource.Targets.ContainsKey(target.GetHashCode())) { props = new List<string>(); props.Add(targetProp); wSource.Targets.Add(target.GetHashCode(), props); target.PropertyChanged += new PropertyChangedEventHandler(wSource.HandlePropertyChanged); } else { props = wSource.Targets[target.GetHashCode()]; if (!props.Contains(targetProp)) { props.Add(targetProp); } } return wSource; }