Example #1
0
 /// <summary>
 /// 注销事件
 /// </summary>
 /// <param name="element"></param>
 public void RemoveEvent(UIElement element)
 {
     if (element != null)
     {
         element.RemoveHandler(UIElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(ElementMouseDown));
     }
 }
Example #2
0
        public static IObservable <EventPattern <TEventArgs> > RoutedEventRaised <TDelegate, TEventArgs>(
            this UIElement element,
            RoutedEvent @event,
            bool handledEventsToo)
            where TEventArgs : RoutedEventArgs
        {
            Contract.Requires(element != null);
            Contract.Requires(@event != null);
            Contract.Ensures(Contract.Result <IObservable <EventPattern <TEventArgs> > >() != null);

            return(Observable.Create <EventPattern <TEventArgs> >(
                       observer =>
            {
                RoutedEventHandler handler = (sender, e) => observer.OnNext(new EventPattern <TEventArgs>(sender, (TEventArgs)e));

#if UNIVERSAL
                var d = handler.GetMethodInfo().CreateDelegate(typeof(TDelegate), handler.Target);
#else
                var d = Delegate.CreateDelegate(typeof(TDelegate), handler.Target, handler.Method);
#endif

                /* Silverlight throws an ArgumentException with the word "handler" as the message if the handler is
                 * not the exact type used by the specified routed event.  Therefore, it is necessary for this extension
                 * method to have a TDelegate type parameter so that it can create the proper handler type via reflection.
                 *
                 * TODO: Already tested for WP 7.1 but not tested yet for WP 8.0, so TDelegate might not be required anymore.
                 */
                element.AddHandler(@event, d, handledEventsToo);

                return Disposable.Create(() => element.RemoveHandler(@event, d));
            }));
        }
Example #3
0
        /// <summary>
        /// Returns an observable that wraps the MouseLeftButtonDown event and
        /// returns even if one of the events involved is handled.
        /// </summary>
        /// <param name="that">The element to listen to.</param>
        /// <returns>An observable that that wraps the MouseLeftButtonDown event
        /// and returns even if one of the events involved is handled.</returns>
        internal static IObservable <Event <MouseButtonEventArgs> > GetMouseLeftButtonDownAlways(this UIElement that)
        {
            return(new AnonymousObservable <Event <MouseButtonEventArgs> >(
                       (observer) =>
            {
                MouseButtonEventHandler handler = (o, a) => observer.OnNext(new Event <MouseButtonEventArgs>(o, a));
                that.AddHandler(UIElement.MouseLeftButtonDownEvent, handler, true);

                return new AnonymousDisposable(() => that.RemoveHandler(UIElement.MouseLeftButtonDownEvent, handler));
            }));
        }
Example #4
0
        /// <summary>
        /// Returns an observable that wraps the KeyUp event and
        /// returns even if one of the events involved is handled.
        /// </summary>
        /// <param name="that">The element to listen to.</param>
        /// <returns>An observable that that wraps the KeyUp event
        /// and returns even if one of the events involved is handled.</returns>
        internal static IObservable <Event <KeyEventArgs> > GetKeyUpAlways(this UIElement that)
        {
            return(new AnonymousObservable <Event <KeyEventArgs> >(
                       (observer) =>
            {
                KeyEventHandler handler = (o, a) => observer.OnNext(new Event <KeyEventArgs>(o, a));
                that.AddHandler(UIElement.KeyUpEvent, handler, true);

                return new AnonymousDisposable(() => that.RemoveHandler(UIElement.KeyUpEvent, handler));
            }));
        }
        /// <summary>
        /// Returns an observable that wraps the KeyDown event and
        /// returns even if one of the events involved is handled.
        /// </summary>
        /// <param name="that">The element to listen to.</param>
        /// <returns>An observable that that wraps the KeyDown event
        /// and returns even if one of the events involved is handled.</returns>
        internal static IObservable <IEvent <KeyEventArgs> > GetKeyDownAlways(this UIElement that)
        {
            return(new AnonymousObservable <IEvent <KeyEventArgs> >(
                       (observer) =>
            {
                KeyEventHandler handler = (o, a) => observer.OnNext(Event.Create(o, a));
                that.AddHandler(UIElement.KeyDownEvent, handler, true);

                return new AnonymousDisposable(() => that.RemoveHandler(UIElement.KeyDownEvent, handler));
            })
                   .SubscribeOnDispatcher());
        }
Example #6
0
        /// <summary>
        /// Removes all event handlers subscribed to the specified routed event from the specified element.
        /// See http://stackoverflow.com/questions/6828054
        /// </summary>
        /// <param name="element">The UI element on which the routed event is defined.</param>
        /// <param name="routedEvent">The routed event for which to remove the event handlers.</param>
        public static void RemoveRoutedEventHandlers(UIElement element, RoutedEvent routedEvent)
        {
            // Get the EventHandlersStore instance which holds event handlers for the specified element.
              // The EventHandlersStore class is declared as internal.
              var eventHandlersStoreProperty = typeof(UIElement).GetProperty("EventHandlersStore",
            BindingFlags.Instance | BindingFlags.NonPublic);
              object eventHandlersStore = eventHandlersStoreProperty.GetValue(element, null);

              // Invoke the GetRoutedEventHandlers method on the EventHandlersStore instance
              // for getting an array of the subscribed event handlers.
              var getRoutedEventHandlers = eventHandlersStore.GetType().GetMethod("GetRoutedEventHandlers",
            BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
              var routedEventHandlers = (RoutedEventHandlerInfo[])getRoutedEventHandlers.Invoke(
            eventHandlersStore, new object[] { routedEvent });

              // Iteratively remove all routed event handlers from the element.
              if (routedEventHandlers != null && routedEventHandlers.Length != 0)
            foreach (var routedEventHandler in routedEventHandlers)
              element.RemoveHandler(routedEvent, routedEventHandler.Handler);
        }
Example #7
0
        /// <summary>
        /// Gets an observable sequence of event notifications for the specified <see cref="RoutedEvent"/>,
        /// raised by the specified <see cref="UIElement"/>.
        /// </summary>
        /// <typeparam name="TEventArgs">Type of the object containing the routed event's arguments.</typeparam>
        /// <param name="element">The <see cref="UIElement"/> on which to listen for the specified <paramref name="event"/>.</param>
        /// <param name="event">The <see cref="RoutedEvent"/> that is raised.</param>
        /// <param name="handledEventsToo"><see langword="true"/> to include events marked handled in their event data;
        /// <see langword="false"/> to exclude routed events that are already marked handled.</param>
        /// <returns>An observable sequence of event notifications for the specified <see cref="RoutedEvent"/>,
        /// raised by the specified <see cref="UIElement"/>.</returns>
        public static IObservable <EventPattern <TEventArgs> > RoutedEventRaised <TEventArgs>(
            this UIElement element,
            RoutedEvent @event,
            bool handledEventsToo)
            where TEventArgs : RoutedEventArgs
        {
            Contract.Requires(element != null);
            Contract.Requires(@event != null);
            Contract.Ensures(Contract.Result <IObservable <EventPattern <TEventArgs> > >() != null);

            return(Observable.Create <EventPattern <TEventArgs> >(
                       observer =>
            {
                RoutedEventHandler handler = (sender, e) => observer.OnNext(new EventPattern <TEventArgs>(sender, (TEventArgs)e));

                element.AddHandler(@event, handler, handledEventsToo);

                return Disposable.Create(() => element.RemoveHandler(@event, handler));
            }));
        }
Example #8
0
        /// <summary>
        /// Removes a handler for the QueryGripInteractionStatus attached event
        /// </summary>
        /// <param name="element">UIElement that listens to this event</param>
        /// <param name="handler">Event Handler to be removed</param>
        public static void RemoveQueryInteractionStatusHandler(UIElement element, EventHandler<QueryInteractionStatusEventArgs> handler)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            element.RemoveHandler(KinectRegion.QueryInteractionStatusEvent, handler);
        }
Example #9
0
        /// <summary>
        /// Removes a handler for the HandPointerLostCapture attached event
        /// </summary>
        /// <param name="element">UIElement that listens to this event</param>
        /// <param name="handler">Event Handler to be removed</param>
        public static void RemoveHandPointerLostCaptureHandler(UIElement element, EventHandler<HandPointerEventArgs> handler)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            element.RemoveHandler(KinectRegion.HandPointerLostCaptureEvent, handler);
        }
Example #10
0
        /// <summary>
        ///     Removes a handler for the SourceChanged event to the element.
        /// </summary>
        /// <param name="e">The element to remove the handler from.</param>
        /// <param name="handler">The hander to remove.</param>
        /// <remarks>
        ///     Even though this is a routed event handler, there are special
        ///     restrictions placed on this event.
        ///     1) You cannot use the UIElement or ContentElement RemoveHandler() method.
        /// </remarks>
        public static void RemoveSourceChangedHandler(IInputElement e, SourceChangedEventHandler handler)
        {
            if (e == null)
            {
                throw new ArgumentNullException("e");
            }

            if (!InputElement.IsValid(e))
            {
                throw new ArgumentException(SR.Get(SRID.Invalid_IInputElement), "e");
            }
            DependencyObject o = (DependencyObject)e;

            //             o.VerifyAccess();

            // I would rather throw an exception here, but the CLR doesn't
            // so we won't either.
            if (handler != null)
            {
                FrugalObjectList <RoutedEventHandlerInfo> info = null;
                EventHandlersStore store;

                // Either UIElement or ContentElement.
                if (InputElement.IsUIElement(o))
                {
                    UIElement uie = o as UIElement;
                    uie.RemoveHandler(SourceChangedEvent, handler);
                    store = uie.EventHandlersStore;
                    if (store != null)
                    {
                        info = store[SourceChangedEvent];
                    }
                    if (info == null || info.Count == 0)
                    {
                        uie.VisualAncestorChanged -= new Visual.AncestorChangedEventHandler(uie.OnVisualAncestorChanged);;
                        RemoveElementFromWatchList(uie);
                    }
                }
                else if (InputElement.IsUIElement3D(o))
                {
                    UIElement3D uie3D = o as UIElement3D;
                    uie3D.RemoveHandler(SourceChangedEvent, handler);
                    store = uie3D.EventHandlersStore;
                    if (store != null)
                    {
                        info = store[SourceChangedEvent];
                    }
                    if (info == null || info.Count == 0)
                    {
                        uie3D.VisualAncestorChanged -= new Visual.AncestorChangedEventHandler(uie3D.OnVisualAncestorChanged);;
                        RemoveElementFromWatchList(uie3D);
                    }
                }
                else
                {
                    ContentElement ce = o as ContentElement;
                    ce.RemoveHandler(SourceChangedEvent, handler);
                    store = ce.EventHandlersStore;
                    if (store != null)
                    {
                        info = store[SourceChangedEvent];
                    }
                    if (info == null || info.Count == 0)
                    {
                        RemoveElementFromWatchList(ce);
                    }
                }
            }
        }
Example #11
0
 /// <summary>
 /// Removes a handler for the ClearPropertyItem attached event
 /// </summary>
 /// <param name="element">the element to attach the handler</param>
 /// <param name="handler">the handler for the event</param>
 public static void RemoveClearPropertyItemHandler( UIElement element, PropertyItemEventHandler handler )
 {
   element.RemoveHandler( PropertyGrid.ClearPropertyItemEvent, handler );
 }
Example #12
0
        /// <summary>
        ///     Removes the handler from the element.
        /// </summary>
        /// <param name="element">The element from which to remove the handler.</param>
        /// <param name="handler">The handler to remove.</param>
        public static void RemovePreviewExecutedHandler(UIElement element, ExecutedRoutedEventHandler handler)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }
            if (handler == null)
            {
                throw new ArgumentNullException("handler");
            }

            element.RemoveHandler(PreviewExecutedEvent, handler);
        }
Example #13
0
 public static void RemoveQueryContinueDragHandler(DependencyObject element, QueryContinueDragEventHandler handler)
 {
     UIElement.RemoveHandler(element, QueryContinueDragEvent, handler);
 }
Example #14
0
 public static void RemovePreviewGiveFeedbackHandler(DependencyObject element, GiveFeedbackEventHandler handler)
 {
     UIElement.RemoveHandler(element, PreviewGiveFeedbackEvent, handler);
 }
Example #15
0
 public static void RemovePreviewDropHandler(DependencyObject element, DragEventHandler handler)
 {
     UIElement.RemoveHandler(element, PreviewDropEvent, handler);
 }
Example #16
0
 public static void RemoveDragOverHandler(DependencyObject element, DragEventHandler handler)
 {
     UIElement.RemoveHandler(element, DragOverEvent, handler);
 }