Пример #1
0
        /// <summary>
        /// Raises a specific routed event. The <see cref="RoutedEvent"/> to be raised is identified within the <see cref="RoutedEventArgs"/> instance
        /// that is provided (as the <see cref="RoutedEvent"/> property of that event data).
        /// </summary>
        /// <param name="e">A <see cref="RoutedEventArgs"/> that contains the event data and also identifies the event to raise.</param>
        /// <exception cref="ArgumentNullException"><paramref name="e"/> is null.</exception>
        /// <exception cref="InvalidOperationException">The type of the routed event argument <paramref name="e"/> does not match the event handler second argument type.</exception>
        public void RaiseEvent(RoutedEventArgs e)
        {
            if (e == null)
            {
                throw new ArgumentNullException(nameof(e));
            }

            if (e.RoutedEvent == null)
            {
                return;
            }

            if (!e.RoutedEvent.HandlerSecondArgumentType.GetTypeInfo().IsAssignableFrom(e.GetType().GetTypeInfo()))
            {
                throw new InvalidOperationException("The type of second parameter of the handler (" + e.RoutedEvent.HandlerSecondArgumentType
                                                    + ") is not assignable from the parameter 'e' type (" + e.GetType() + ").");
            }

            // temporarily disable errors from add/removes of entities during events in other threads, as it is always safe to do it now
            bool previousEnforce = EntityManager.EnforceThreads;

            EntityManager.EnforceThreads = false;

            var sourceWasNull = e.Source == null;

            if (sourceWasNull) // set the source to default if needed
            {
                e.Source = this;
            }

            e.StartEventRouting();

            PropagateRoutedEvent(e);

            e.EndEventRouting();

            // return to enforcing thread add/removes, if we were before
            EntityManager.EnforceThreads = previousEnforce;

            if (sourceWasNull) // reset the source if it was not explicitly set (event might be reused again for other sources)
            {
                e.Source = null;
            }
        }
Пример #2
0
        /// <summary>
        /// Raises a specific routed event. The <see cref="RoutedEvent"/> to be raised is identified within the <see cref="RoutedEventArgs"/> instance
        /// that is provided (as the <see cref="RoutedEvent"/> property of that event data).
        /// </summary>
        /// <param name="e">A <see cref="RoutedEventArgs"/> that contains the event data and also identifies the event to raise.</param>
        /// <exception cref="ArgumentNullException"><paramref name="e"/> is null.</exception>
        /// <exception cref="InvalidOperationException">The type of the routed event argument <paramref name="e"/> does not match the event handler second argument type.</exception>
        public void RaiseEvent(RoutedEventArgs e)
        {
            if (e == null)
            {
                throw new ArgumentNullException(nameof(e));
            }

            if (e.RoutedEvent == null)
            {
                return;
            }

            if (!e.RoutedEvent.HandlerSecondArgumentType.GetTypeInfo().IsAssignableFrom(e.GetType().GetTypeInfo()))
            {
                throw new InvalidOperationException("The type of second parameter of the handler (" + e.RoutedEvent.HandlerSecondArgumentType
                                                    + ") is not assignable from the parameter 'e' type (" + e.GetType() + ").");
            }

            var sourceWasNull = e.Source == null;

            if (sourceWasNull) // set the source to default if needed
            {
                e.Source = this;
            }

            e.StartEventRouting();

            PropagateRoutedEvent(e);

            e.EndEventRouting();

            if (sourceWasNull) // reset the source if it was not explicitly set (event might be reused again for other sources)
            {
                e.Source = null;
            }
        }