Beispiel #1
0
        /// <summary>
        /// Subscribes to the given MessageType during the livetime of the given control.
        /// Events OnHandleCreated and OnHandleDestroyed are used for subscribing / unsubscribing.
        /// </summary>
        /// <typeparam name="MessageType">Type of the message.</typeparam>
        /// <param name="actionOnMessage">Action to perform on incoming message.</param>
        /// <param name="target">The target control.</param>
        public void SubscribeOnControl <MessageType>(System.Windows.Forms.Control target, Action <MessageType> actionOnMessage)
            where MessageType : SeeingSharpMessage
        {
            target.EnsureNotNull(nameof(target));
            actionOnMessage.EnsureNotNull(nameof(actionOnMessage));
            MessageSubscription subscription = null;

            //Create handler delegates
            EventHandler onHandleCreated = (sender, eArgs) =>
            {
                if (subscription == null)
                {
                    subscription = Subscribe(actionOnMessage);
                }
            };
            EventHandler onHandleDestroyed = (inner, eArgs) =>
            {
                if (subscription != null)
                {
                    Unsubscribe(subscription);
                    subscription = null;
                }
            };

            //Attach to events and subscribe on message, if handle is already created
            target.HandleCreated   += onHandleCreated;
            target.HandleDestroyed += onHandleDestroyed;
            if (target.IsHandleCreated)
            {
                subscription = Subscribe(actionOnMessage);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Subscribes all receiver-methods of the given target object to this Messenger.
        /// Subscribe and unsubscribe is automatically called when the control is created/destroyed.
        /// </summary>
        /// <param name="target">The target win.forms control.</param>
        public void SubscribeAllOnControl(System.Windows.Forms.Control target)
        {
            target.EnsureNotNull(nameof(target));

            IEnumerable <MessageSubscription> generatedSubscriptions = null;

            // Create handler delegates
            EventHandler onHandleCreated = (sender, eArgs) =>
            {
                if (generatedSubscriptions == null)
                {
                    generatedSubscriptions = SubscribeAll(target);
                }
            };
            EventHandler onHandleDestroyed = (inner, eArgs) =>
            {
                if (generatedSubscriptions != null)
                {
                    foreach (MessageSubscription actSubscription in generatedSubscriptions)
                    {
                        actSubscription.Unsubscribe();
                    }
                    generatedSubscriptions = null;
                }
            };

            // Attach to events and subscribe on message, if handle is already created
            target.HandleCreated   += onHandleCreated;
            target.HandleDestroyed += onHandleDestroyed;
            if (target.IsHandleCreated)
            {
                generatedSubscriptions = SubscribeAll(target);
            }
        }