Click here to Skip to main content
15,898,134 members
Articles / General Programming / Threads

Plugged.NET - An event based plug-in library for enterprise application integration and extensibility

Rate me:
Please Sign up or sign in to vote.
4.89/5 (19 votes)
25 Jul 2013CPOL6 min read 34.1K   1.4K   92  
An event based plug-in library for enterprise application integration, extensibility and cross business application management.
using System;
using System.Windows;

namespace Common.Plugin.SampleHost.Taskbar
{
  /// <summary>
  /// Helper class used by routed events of the
  /// <see cref="TaskbarIcon"/> class.
  /// </summary>
  internal static class RoutedEventHelper
  {
    #region RoutedEvent Helper Methods

    /// <summary>
    /// A static helper method to raise a routed event on a target UIElement or ContentElement.
    /// </summary>
    /// <param name="target">UIElement or ContentElement on which to raise the event</param>
    /// <param name="args">RoutedEventArgs to use when raising the event</param>
    internal static void RaiseEvent(DependencyObject target, RoutedEventArgs args)
    {
      if (target is UIElement)
      {
        (target as UIElement).RaiseEvent(args);
      }
      else if (target is ContentElement)
      {
        (target as ContentElement).RaiseEvent(args);
      }
    }

    /// <summary>
    /// A static helper method that adds a handler for a routed event 
    /// to a target UIElement or ContentElement.
    /// </summary>
    /// <param name="element">UIElement or ContentElement that listens to the event</param>
    /// <param name="routedEvent">Event that will be handled</param>
    /// <param name="handler">Event handler to be added</param>
    internal static void AddHandler(DependencyObject element, RoutedEvent routedEvent, Delegate handler)
    {
      UIElement uie = element as UIElement;
      if (uie != null)
      {
        uie.AddHandler(routedEvent, handler);
      }
      else
      {
        ContentElement ce = element as ContentElement;
        if (ce != null)
        {
          ce.AddHandler(routedEvent, handler);
        }
      }
    }

    /// <summary>
    /// A static helper method that removes a handler for a routed event 
    /// from a target UIElement or ContentElement.
    /// </summary>
    /// <param name="element">UIElement or ContentElement that listens to the event</param>
    /// <param name="routedEvent">Event that will no longer be handled</param>
    /// <param name="handler">Event handler to be removed</param>
    internal static void RemoveHandler(DependencyObject element, RoutedEvent routedEvent, Delegate handler)
    {
      UIElement uie = element as UIElement;
      if (uie != null)
      {
        uie.RemoveHandler(routedEvent, handler);
      }
      else
      {
        ContentElement ce = element as ContentElement;
        if (ce != null)
        {
          ce.RemoveHandler(routedEvent, handler);
        }
      }
    }

    #endregion
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
is a poor software developer and thinker. Presently working on a theory of "complementary perception". It's a work in progress.

Comments and Discussions