<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Orbifold &#187; CAB</title>
	<atom:link href="http://visualizationtools.net/default/tag/cab/feed/" rel="self" type="application/rss+xml" />
	<link>http://visualizationtools.net/default</link>
	<description>Think. Visualize. Understand.</description>
	<lastBuildDate>Wed, 18 Jan 2012 09:08:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Dynamically loading modules in CAB.</title>
		<link>http://visualizationtools.net/default/dynamically-loading-modules-in-cab/</link>
		<comments>http://visualizationtools.net/default/dynamically-loading-modules-in-cab/#comments</comments>
		<pubDate>Sun, 06 May 2007 19:31:45 +0000</pubDate>
		<dc:creator>Francois Vanderseypen</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[CAB]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://www.orbifold.net/default/?p=706</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>Although this little bit of code and technique is specific to the WPF variant of CAB, I&#8217;m sure you can use it in the Windows Forms variant as well.</p>
<p>In the Cobalt.IDE I had developed a very similar structure to what you find in CAB but it was from the beginning clear to me that modules should be loaded on demand and not automtically at start-up (as it is in CAB now). The ideas was that you can specify in the configuration file some kind of AutoLoad attribute and that module information (ModuleInfo class in CAB) would be loaded but not necessarily the assembly. As it stands now CAB is rather poor in certain areas and dynamically loading assemblies is not configurable, however you can use the mechanisms burried in the architecture to load modules and workitems at runtime. Hope this will be useful to somebody since it seems only few people are interested in the WPF-CAB things (too difficult to handle?). Another area where I will shortly post about is the possibility to make the CAB shell interactive with some kind of macro&#8217;s. If you look into Cobalt and Netron you will see what I mean. Cobalt has a scripting plugin with which you can dive into the runtime hierarchy and execute C# stuff. More about this later.</p>
<p>What I did is the following:</p>
<ul>
<li>attaching a menu item in the shell which loads a precompiled assembly/module</li>
<li>the way the menu item is attached is a technique I used extensively in the free Unfold code. It&#8217;s based on the idea of coupling UI elements to &#8216;tools&#8217;. The tools are one-one with a specific <strong>Command </strong>as well as an Execute method and a Query method which is being called regularly by the WPF loop to see if menu item are still enabled.</li>
<li>the key to loading a module at runtime is hidden in the <strong>ModuleLoaderService </strong>of the CompositeUI assembly. This service is automatically loaded by the <strong>CABAppplication </strong>and available through the <strong>Services </strong>collection in diverse places. The <strong>IModuleLoaderService </strong>has two overloaded Load methods of which the easiest one is accepting an assembly to load and a workitem that acts as the host for the module. All you need to do is, hence, picking up and assembly and passing it to the service. Of course, you should not put a &lt;moduleinfo&gt; section in your ProfileCatalog for the dynamic module!</li>
</ul>
<p>The following snippet adds dynamically a new menu item and attaches a handler via a tool.</p>
<div class="wp_codebox">
<table>
<tr id="p7061">
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
8
9
10
11
</pre>
</td>
<td class="code" id="p706code1">
<pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> AfterShellCreated<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
<span style="color: #008000;">...</span>
<span style="color: #0000FF;">menuItem</span> <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> MenuItem<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
menuItem<span style="color: #008000;">.</span><span style="color: #0000FF;">Header</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Test&quot;</span><span style="color: #008000;">;</span>
TestTool testTool <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TestTool<span style="color: #008000;">&#40;</span>RootWorkItem<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
menuItem<span style="color: #008000;">.</span><span style="color: #0000FF;">CommandBindings</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> CommandBinding<span style="color: #008000;">&#40;</span>testTool<span style="color: #008000;">.</span><span style="color: #0000FF;">Command</span>, testTool<span style="color: #008000;">.</span><span style="color: #0000FF;">OnExecute</span>, testTool<span style="color: #008000;">.</span><span style="color: #0000FF;">OnQueryEnabled</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
menuItem<span style="color: #008000;">.</span><span style="color: #0000FF;">Command</span> <span style="color: #008000;">=</span> testTool<span style="color: #008000;">.</span><span style="color: #0000FF;">Command</span><span style="color: #008000;">;</span>
RootWorkItem<span style="color: #008000;">.</span><span style="color: #0000FF;">UIExtensionSites</span><span style="color: #008000;">&#91;</span>UIExtensionConstants<span style="color: #008000;">.</span><span style="color: #0000FF;">FILE</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>menuItem<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">...</span>
<span style="color: #008000;">&#125;</span></pre>
</td>
</tr>
</table>
</div>
<p>The tool is based on a ToolBase and is very similar to what you find in the Unfold code:</p>
<div class="wp_codebox">
<table>
<tr id="p7062">
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
</pre>
</td>
<td class="code" id="p706code2">
<pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> abstract <span style="color: #6666cc; font-weight: bold;">class</span> ToolBase
<span style="color: #008000;">&#123;</span>
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #008080; font-style: italic;">/// the Name field</span>
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> mName<span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #008080; font-style: italic;">/// the command attached to this tool</span>
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #0600FF; font-weight: bold;">protected</span> RoutedUICommand mCommand<span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #008080; font-style: italic;">/// Gets the routed command.</span>
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #008080; font-style: italic;">/// The command.</span>
<span style="color: #0600FF; font-weight: bold;">public</span> RoutedUICommand Command
<span style="color: #008000;">&#123;</span>
get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> mCommand<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Name
<span style="color: #008000;">&#123;</span>
get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> mName<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
set <span style="color: #008000;">&#123;</span> mName <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #008080;">#region Constructor</span>
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #008080; font-style: italic;">///Default constructor</span>
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #0600FF; font-weight: bold;">public</span> ToolBase<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> name<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
<span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">mName</span> <span style="color: #008000;">=</span> name<span style="color: #008000;">;</span>
mCommand <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> RoutedUICommand<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008000;">&#125;</span>
<span style="color: #008080;">#endregion</span>
<span style="color: #008080;">#region Methods</span>
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #008080; font-style: italic;">/// Determines if a command is enabled. Override to provide custom behavior. Do not call the</span>
<span style="color: #008080; font-style: italic;">/// base version when overriding.</span>
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnQueryEnabled<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, CanExecuteRoutedEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
e<span style="color: #008000;">.</span><span style="color: #0000FF;">CanExecute</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
e<span style="color: #008000;">.</span><span style="color: #0000FF;">Handled</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #008080; font-style: italic;">/// Function to execute the command.</span>
<span style="color: #008080; font-style: italic;">///</span>
<span style="color: #0600FF; font-weight: bold;">public</span> abstract <span style="color: #6666cc; font-weight: bold;">void</span> OnExecute<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, ExecutedRoutedEventArgs e<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080;">#endregion</span>
<span style="color: #008000;">&#125;</span></pre>
</td>
</tr>
</table>
</div>
<p>Finally, this is where the real work is done. Make sure you put the code where the <strong>RootWorkItem </strong>is accessible and not null&#8230;</p>
<div class="wp_codebox">
<table>
<tr id="p7063">
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre>
</td>
<td class="code" id="p706code3">
<pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> TestTool <span style="color: #008000;">:</span> ToolBase
<span style="color: #008000;">&#123;</span>
&nbsp;
WorkItem worker<span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> TestTool<span style="color: #008000;">&#40;</span>WorkItem worker<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Test tool&quot;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
<span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">worker</span> <span style="color: #008000;">=</span> worker<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnExecute<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, ExecutedRoutedEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
Microsoft<span style="color: #008000;">.</span><span style="color: #0000FF;">Practices</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CompositeUI</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Services</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IModuleLoaderService</span> loader <span style="color: #008000;">=</span> worker<span style="color: #008000;">.</span><span style="color: #0000FF;">Services</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>loader <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
<span style="color: #6666cc; font-weight: bold;">string</span> testAssembly <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Unfold.StartModule.dll&quot;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">System.<span style="color: #0000FF;">Reflection</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Assembly</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> assemblies <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #000000;">System.<span style="color: #0000FF;">Reflection</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Assembly</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> LoadAssembly<span style="color: #008000;">&#40;</span>testAssembly<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
loader<span style="color: #008000;">.</span><span style="color: #0000FF;">Load</span><span style="color: #008000;">&#40;</span>worker, assemblies<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> GetModulePath<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> assemblyFile<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Path</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsPathRooted</span><span style="color: #008000;">&#40;</span>assemblyFile<span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span>
assemblyFile <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Path</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Combine</span><span style="color: #008000;">&#40;</span>AppDomain<span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentDomain</span><span style="color: #008000;">.</span><span style="color: #0000FF;">BaseDirectory</span>, assemblyFile<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">return</span> assemblyFile<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Reflection</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Assembly</span> LoadAssembly<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> assemblyFile<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
&nbsp;
assemblyFile <span style="color: #008000;">=</span> GetModulePath<span style="color: #008000;">&#40;</span>assemblyFile<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">FileInfo</span> file <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">FileInfo</span><span style="color: #008000;">&#40;</span>assemblyFile<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">System.<span style="color: #0000FF;">Reflection</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Assembly</span> assembly <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">try</span>
<span style="color: #008000;">&#123;</span>
assembly <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Reflection</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Assembly</span><span style="color: #008000;">.</span><span style="color: #0000FF;">LoadFrom</span><span style="color: #008000;">&#40;</span>file<span style="color: #008000;">.</span><span style="color: #0000FF;">FullName</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception ex<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>ex<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">return</span> assembly<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre>
</td>
</tr>
</table>
</div>
<p>Note that there are other ways to do this, you could go much deeper and plug your own module loader service which could handle configuration attributes. Or you could go even further and extend the CompositeUI on top of the ObjectBuilder mechanisms. CAB is a wonderful playground for ideas.</p>
]]></content:encoded>
			<wfw:commentRss>http://visualizationtools.net/default/dynamically-loading-modules-in-cab/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Persistence service in CAB</title>
		<link>http://visualizationtools.net/default/the-persistence-service-in-cab/</link>
		<comments>http://visualizationtools.net/default/the-persistence-service-in-cab/#comments</comments>
		<pubDate>Wed, 02 May 2007 19:35:25 +0000</pubDate>
		<dc:creator>Francois Vanderseypen</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[CAB]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://www.orbifold.net/default/?p=701</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>Seems nobody is writing down simple examples of how to use the CAB mechanisms, so here is a snippet that might be useful to somebody one day&#8230;<br />
[csharp]<br />
public IStatePersistenceService PersistenceService<br />
{<br />
    get<br />
    {<br />
        return Services.Get<istatepersistenceservice>();<br />
    }<br />
}<br />
&#8230;<br />
if (PersistenceService.Contains(&#8220;SomeKey&#8221;))<br />
                Console.WriteLine(PersistenceService.Load(&#8220;SomeKey&#8221;)["a"].ToString());<br />
            else<br />
            {<br />
                State s = new State(&#8220;ab&#8221;);<br />
                s["a"] = &#8221; Some string you want to persist, e.g. a favorite item.&#8221;;<br />
                s["b"] = new SomeObjectYouNeedToSave();</p>
<p>                PersistenceService.Save(s);<br />
            }<br />
[/csharp]</istatepersistenceservice></p>
]]></content:encoded>
			<wfw:commentRss>http://visualizationtools.net/default/the-persistence-service-in-cab/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  www.visualizationtools.net/default/tag/cab/feed/ ) in 0.90088 seconds, on Feb 9th, 2012 at 7:08 am UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 9th, 2012 at 8:08 am UTC -->
