/// <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);
        }
예제 #2
0
        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);
        }
예제 #3
0
        public void Get()
        {
            Foo foo = new Foo();

            ISource <Foo> source = new WeakSource <Foo>(foo);

            Assert.Same(foo, source.Value);
        }
예제 #4
0
        public void Get()
        {
            Foo foo = new Foo();

            ISource<Foo> source = new WeakSource<Foo>(foo);

            Assert.Same(foo, source.Value);
        }
예제 #5
0
        public void Set()
        {
            ISource <Foo> source = new WeakSource <Foo>();

            Foo foo = new Foo();

            source.Value = foo;

            Assert.Same(foo, source.Value);
        }
예제 #6
0
        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);
            }
        }
예제 #8
0
        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);
        }
예제 #9
0
        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());
                    }
                }
            }
예제 #12
0
            /// <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;
            }