Skip to content

Instantly share code, notes, and snippets.

/wemanager.snippet
Created Jan 13, 2011

Embed
What would you like to do?
Code snippet for a basic WeakEventManager class
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>WeakEventManager snippet</Title>
<Shortcut>wem</Shortcut>
<Description>Code snippet for a basic WeakEventManager class</Description>
<Author>Ivan Nikitin</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>className</ID>
<ToolTip>New weak event manager class name</ToolTip>
<Default>MyEventManager</Default>
</Literal>
<Literal>
<ID>source</ID>
<ToolTip>Event source type</ToolTip>
<Default>object</Default>
</Literal>
<Literal>
<ID>eventName</ID>
<ToolTip>Event name</ToolTip>
<Default>MyEvent</Default>
</Literal>
<Literal>
<ID>eventArgs</ID>
<ToolTip>Event arguments type</ToolTip>
<Default>EventArgs</Default>
</Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[public class $className$ : WeakEventManager {
static $className$ CurrentManager {
get {
Type managerType = typeof($className$);
$className$ currentManager =
($className$)WeakEventManager.GetCurrentManager(managerType);
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);
}
}
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
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.