Skip to content

Instantly share code, notes, and snippets.

Created Jan 13, 2011

What would you like to do?
Code snippet for a basic WeakEventManager class
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="">
<CodeSnippet Format="1.0.0">
<Title>WeakEventManager snippet</Title>
<Description>Code snippet for a basic WeakEventManager class</Description>
<Author>Ivan Nikitin</Author>
<ToolTip>New weak event manager class name</ToolTip>
<ToolTip>Event source type</ToolTip>
<ToolTip>Event name</ToolTip>
<ToolTip>Event arguments type</ToolTip>
<Code Language="csharp">
<![CDATA[public class $className$ : WeakEventManager {
static $className$ CurrentManager {
get {
Type managerType = typeof($className$);
$className$ currentManager =
if(currentManager == null) {
currentManager = new $className$();
WeakEventManager.SetCurrentManager(managerType, currentManager);
return currentManager;
$className$() { }
public static void AddListener($source$ source, IWeakEventListener listener) {
CurrentManager.ProtectedAddListener(source, listener);
public static void RemoveListener($source$ source, IWeakEventListener listener) {
CurrentManager.ProtectedRemoveListener(source, listener);
protected override void StartListening(object source) {
(($source$)source).$eventName$ += EventHandler;
protected override void StopListening(object source) {
(($source$)source).$eventName$ -= EventHandler;
void EventHandler(object sender, $eventArgs$ e) {
base.DeliverEvent(sender, e);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.