Beispiel #1
0
        /// <summary>
        /// Indicates that a property change should cause <see cref="InvalidateVisual"/> to be
        /// called.
        /// </summary>
        /// <typeparam name="T">The control which the property affects.</typeparam>
        /// <param name="properties">The properties.</param>
        /// <remarks>
        /// This method should be called in a control's static constructor with each property
        /// on the control which when changed should cause a redraw. This is similar to WPF's
        /// FrameworkPropertyMetadata.AffectsRender flag.
        /// </remarks>
        protected static void AffectsRender <T>(params AvaloniaProperty[] properties)
            where T : Visual
        {
            void Invalidate(AvaloniaPropertyChangedEventArgs e)
            {
                if (e.Sender is T sender)
                {
                    if (e.OldValue is IAffectsRender oldValue)
                    {
                        WeakEventHandlerManager.Unsubscribe <EventArgs, T>(oldValue, nameof(oldValue.Invalidated), sender.AffectsRenderInvalidated);
                    }

                    if (e.NewValue is IAffectsRender newValue)
                    {
                        WeakEventHandlerManager.Subscribe <IAffectsRender, EventArgs, T>(newValue, nameof(newValue.Invalidated), sender.AffectsRenderInvalidated);
                    }

                    sender.InvalidateVisual();
                }
            }

            foreach (var property in properties)
            {
                property.Changed.Subscribe(Invalidate);
            }
        }
Beispiel #2
0
        void ConnectAnchor(ITypeDescriptorContext context)
        {
            // This is a horrible hack to have the control keep the binding alive.
            var window = context.GetFirstParent <Window>();

            _anchor.Tag = this;
            WeakEventHandlerManager.Subscribe <Window, EventArgs, LocalizedResourceExtension>(window, nameof(Window.Closed), DisconnectAnchor);
        }
Beispiel #3
0
 private void SubscribeToChanges()
 {
     if (_reference.TryGetTarget(out var o) && o is INotifyPropertyChanged inpc)
     {
         WeakEventHandlerManager.Subscribe <INotifyPropertyChanged, PropertyChangedEventArgs, InpcPropertyAccessor>(
             inpc,
             nameof(INotifyPropertyChanged.PropertyChanged),
             OnNotifyPropertyChanged);
     }
 }
Beispiel #4
0
        private void SubscribeToApplicationEvents()
        {
            WeakEventHandlerManager.Subscribe <ClientApplication, ConnectionStateChangedEventArgs, MainViewModel>(
                Application.Instance, nameof(Application.Instance.ConnectionStateChanged), OnConnectionStateChanged);
            WeakEventHandlerManager.Subscribe <ClientApplication, ReconnectionTimeChangedEventArgs, MainViewModel>(
                Application.Instance, nameof(Application.Instance.ReconnectionTimeChanged), OnReconnectionTimeChanged);

            IsConnected      = Application.Instance.IsConnected;
            ReconnectionTime = Application.Instance.ReconnectionTime;
        }
        public void EventShouldBePassedToSubscriber()
        {
            bool handled    = false;
            var  subscriber = new Subscriber(() => handled = true);
            var  source     = new EventSource();

            WeakEventHandlerManager.Subscribe <EventSource, EventArgs, Subscriber>(source, "Event",
                                                                                   subscriber.OnEvent);
            source.Fire();
            Assert.True(handled);
        }
Beispiel #6
0
 protected override void SubscribeCore()
 {
     base.SubscribeCore();
     if (_reference.TryGetTarget(out var o) && o is INotifyCollectionChanged incc)
     {
         WeakEventHandlerManager.Subscribe <INotifyCollectionChanged, NotifyCollectionChangedEventArgs, IndexerAccessor>(
             incc,
             nameof(INotifyCollectionChanged.CollectionChanged),
             OnNotifyCollectionChanged);
     }
 }
        public void EventShouldNotBeRaisedAfterUnsubscribe()
        {
            bool handled    = false;
            var  subscriber = new Subscriber(() => handled = true);
            var  source     = new EventSource();

            WeakEventHandlerManager.Subscribe <EventSource, EventArgs, Subscriber>(source, "Event",
                                                                                   subscriber.OnEvent);

            WeakEventHandlerManager.Unsubscribe <EventArgs, Subscriber>(source, "Event",
                                                                        subscriber.OnEvent);

            source.Fire();

            Assert.False(handled);
        }
Beispiel #8
0
            static void InvalidateAndSubscribe(AvaloniaPropertyChangedEventArgs e)
            {
                if (e.Sender is T sender)
                {
                    if (e.OldValue is IAffectsRender oldValue)
                    {
                        WeakEventHandlerManager.Unsubscribe <EventArgs, T>(oldValue, nameof(oldValue.Invalidated), sender.AffectsRenderInvalidated);
                    }

                    if (e.NewValue is IAffectsRender newValue)
                    {
                        WeakEventHandlerManager.Subscribe <IAffectsRender, EventArgs, T>(newValue, nameof(newValue.Invalidated), sender.AffectsRenderInvalidated);
                    }

                    sender.InvalidateVisual();
                }
            }
 private void AddCollectableSubscriber(EventSource source, string name, Action func)
 {
     WeakEventHandlerManager.Subscribe <EventSource, EventArgs, Subscriber>(source, name, new Subscriber(func).OnEvent);
 }
 private void Subscribe(Window window)
 {
     WeakEventHandlerManager.Subscribe <Window, EventArgs, LocalizedResourceExtension>(window, nameof(Window.Closed), DisconnectAnchor);
 }