コード例 #1
0
ファイル: Stream.cs プロジェクト: stjordanis/sodium
        internal IStrongListener ListenImpl(Action <T> handler)
        {
            IWeakListener  innerListener = this.ListenWeakImpl(handler);
            StrongListener listener      = null;

            listener = new StrongListener(
                () =>
            {
                innerListener.Unlisten();

                // ReSharper disable AccessToModifiedClosure
                if (listener != null)
                {
                    lock (this.KeepListenersAlive)
                    {
                        this.KeepListenersAlive.StopKeepingListenerAlive(listener);
                    }
                }
                // ReSharper restore AccessToModifiedClosure
            },
                innerListener);

            lock (this.KeepListenersAlive)
            {
                this.KeepListenersAlive.KeepListenerAlive(listener);
            }

            return(listener);
        }
コード例 #2
0
        public void TestMultipleUnlistenWeak()
        {
            StreamSink <int> s = Stream.CreateSink <int>();

            List <int> @out = new List <int>();

            ((Action)(() =>
            {
                // ReSharper disable once UnusedVariable
                IWeakListener l = s.ListenWeak(@out.Add);

                s.Send(1);

                l.Unlisten();
                l.Unlisten();

                s.Send(2);

                l.Unlisten();
            }))();

            s.Send(3);
            s.Send(4);

            Assert.AreEqual(1, @out.Count);
        }
コード例 #3
0
        protected override void OnSourceChanged(object oldSource, object newSource)
        {
            base.OnSourceChanged(oldSource, newSource);

            if (Listener != null)
            {
                Listener.Detach();
                Listener = null;
            }

            if (newSource is INotifyCollectionChanged)
            {
                Listener = new WeakCollectionChangedListener((INotifyCollectionChanged)newSource, this);
            }

            GetIndexer();
        }
コード例 #4
0
        public void TestListenWeak()
        {
            StreamSink <int> s = Stream.CreateSink <int>();

            List <int> @out = new List <int>();

            ((Action)(() =>
            {
                // ReSharper disable once UnusedVariable
                IWeakListener l = s.ListenWeak(@out.Add);

                s.Send(1);
                s.Send(2);
            }))();

            GC.Collect(0, GCCollectionMode.Forced);
            s.Send(3);
            s.Send(4);

            Assert.AreEqual(2, @out.Count);
        }
コード例 #5
0
        internal virtual void OnItemsSourceChanged(IEnumerable oldSource, IEnumerable newSource)
        {
            if (CollectionListener != null)
            {
                CollectionListener.Detach();
                CollectionListener = null;
            }

            if (newSource != null)
            {
                if (newSource is INotifyCollectionChanged)
                {
                    CollectionListener = new WeakCollectionChangedListener(((INotifyCollectionChanged)newSource), this);
                }

                Items.SetIsReadOnly(true);
                itemsIsDataBound = true;
                Items.ClearImpl();

                foreach (var v in newSource)
                {
                    Items.AddImpl(v);
                }

                // Setting itemsIsDataBound to true prevents normal notifications from propagating, so do it manually here
                OnItemsChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
            }
            else
            {
                itemsIsDataBound = false;
                Items.SetIsReadOnly(false);
                Items.ClearImpl();
            }
            // Yes this is stupid and shouldn't be here, but DRT 348 sets an empty collection as the ItemsSource
            // and expects the LayoutUpdated event to be raised. This is the only way that makes sense for this
            // to happen. It's all very strange.
            InvalidateMeasure();
        }
コード例 #6
0
 public static IWeakListener AppendWeak(IWeakListener listener1, IWeakListener listener2) =>
 CreateWeakComposite(new[] { listener1, listener2 });
コード例 #7
0
ファイル: IndexedPropertyPathNode.cs プロジェクト: snorp/moon
		protected override void OnSourceChanged (object oldSource, object newSource)
		{
			base.OnSourceChanged (oldSource, newSource);

			if (Listener != null) {
				Listener.Detach ();
				Listener = null;
			}

			if (newSource is INotifyCollectionChanged)
				Listener = new WeakCollectionChangedListener ((INotifyCollectionChanged) newSource, this);

			GetIndexer ();
		}
コード例 #8
0
 public void AddListener(IWeakListener listener)
 {
     this.listeners.Add(listener);
 }
コード例 #9
0
ファイル: ItemsControl.cs プロジェクト: dfr0/moon
		internal virtual void OnItemsSourceChanged (IEnumerable oldSource, IEnumerable newSource)
		{
			if (CollectionListener != null) {
				CollectionListener.Detach ();
				CollectionListener = null;
			}

			if (newSource != null) {
				if (newSource is INotifyCollectionChanged) {
					CollectionListener = new WeakCollectionChangedListener (((INotifyCollectionChanged)newSource), this);
				}
				
				Items.SetIsReadOnly (true);
				itemsIsDataBound = true;
				Items.ClearImpl ();

				foreach (var v in newSource)
					Items.AddImpl (v);
				
				// Setting itemsIsDataBound to true prevents normal notifications from propagating, so do it manually here
				OnItemsChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Reset));
			} else {
				itemsIsDataBound = false;
				Items.SetIsReadOnly (false);		
				Items.ClearImpl ();
			}
			// Yes this is stupid and shouldn't be here, but DRT 348 sets an empty collection as the ItemsSource
			// and expects the LayoutUpdated event to be raised. This is the only way that makes sense for this
			// to happen. It's all very strange.
			InvalidateMeasure ();
		}