<?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>Everything Silverlight &#187; control</title>
	<atom:link href="http://www.michaelsnow.com/tag/control/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.michaelsnow.com</link>
	<description></description>
	<lastBuildDate>Thu, 28 Apr 2011 21:43:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Silverlight Tip of the Day #14 &#8211; Dynamically Loading a Control from a DLL on a Server</title>
		<link>http://www.michaelsnow.com/2010/05/06/silverlight-tip-of-the-day-14-dynamically-loading-a-control-from-a-dll-on-a-server/</link>
		<comments>http://www.michaelsnow.com/2010/05/06/silverlight-tip-of-the-day-14-dynamically-loading-a-control-from-a-dll-on-a-server/#comments</comments>
		<pubDate>Thu, 06 May 2010 16:20:26 +0000</pubDate>
		<dc:creator>Snowman</dc:creator>
				<category><![CDATA[controls]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[control]]></category>
		<category><![CDATA[dll]]></category>

		<guid isPermaLink="false">http://www.silverlightdev.net/2010/05/06/silverlight-tip-of-the-day-14-dynamically-loading-a-control-from-a-dll-on-a-server/</guid>
		<description><![CDATA[In this tip I will be showing you how to load a Silverlight control from a DLL that is on a server somewhere. This technique is useful if you want the initial XAP download size of your application kept to a minimum. That is, the controls and resources you place in the DLL are items [...]]]></description>
			<content:encoded><![CDATA[<p>In this tip I will be showing you how to load a Silverlight control from a DLL that is on a server somewhere. This technique is useful if you want the initial XAP download size of your application kept to a minimum. That is, the controls and resources you place in the DLL are items that you can download on demand when your application actually needs to load them. </p>
<p>To start, I will create a simple control that displays an image. This control will be packaged in a Silverlight class library DLL.</p>
<p>1. Open Visual Studio. </p>
<p>2. Create a new Silverlight application</p>
<p>3. Right click on the top node in your Solution Explorer. Choose&#160; Add-&gt;New Project.</p>
<p>4. Select “Silverlight Class Library” and give it a name (mine is called TestCtrl). This project will create the DLL that will contain your control(s).</p>
<p>5. In your class library add a new a new Silverlight User Control (mine is called ImageTest).</p>
<p>6. Open up the XAML page (ImageTest.xaml) for this user control and add something like an Image control. If you are using a relative path to an image, make certain the image you reference is also included in your class library project.</p>
<pre class="csharpcode">&lt;Image Source=<span class="str">&quot;scarlett.jpg&quot;</span>&gt;&lt;/Image&gt; </pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>At this point your class library DLL is good to go. Build the and place it on your server in a location you can reference. Note that after building the DLL you will find it in your projects Bin\Debug folder. Clicking the “Show all Files” in the toolbar for your Solution Explorer will make this bin folder visible.</p>
<p><a href="http://www.michaelsnow.com/wp-content/uploads/2010/05/image6.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.michaelsnow.com/wp-content/uploads/2010/05/image_thumb6.png" width="179" height="73" /></a> </p>
<p>7. Next, proceed to your Silverlight Application opening up MainPage.xaml.cs</p>
<p>8. Add a reference to System.Reflection;</p>
<p>9. Use the WebClient class to download the DLL from the server. From the purpose of this demo, I have placed my test DLL at <a title="http://www.flaired.com/TestCtrl.dll" href="http://www.flaired.com/TestCtrl.dll">http://www.flaired.com/TestCtrl.dll</a>&#160; </p>
<div class="csharpcode">
<pre class="alt"><span class="kwrd">private</span> Assembly _testCtrl; </pre>
<pre>&#160;</pre>
<pre class="alt"><span class="kwrd">public</span> MainPage() </pre>
<pre>{ </pre>
<pre class="alt">    InitializeComponent(); </pre>
<pre>&#160;</pre>
<pre class="alt">    WebClient downloader = <span class="kwrd">new</span> WebClient(); </pre>
<pre>    <span class="kwrd">string</span> path = <span class="str">&quot;http://www.flaired.com/TestCtrl.dll&quot;</span>; </pre>
<pre class="alt">    downloader.OpenReadCompleted += <span class="kwrd">new</span> OpenReadCompletedEventHandler </pre>
<pre>      (downloader_OpenReadCompleted);  </pre>
<pre class="alt">    downloader.OpenReadAsync(<span class="kwrd">new</span> Uri(path, UriKind.Absolute)); </pre>
<pre>} </pre>
<pre class="alt">&#160;</pre>
<pre><span class="kwrd">void</span> downloader_OpenReadCompleted(<span class="kwrd">object</span> sender, OpenReadCompletedEventArgs e) </pre>
<pre class="alt">{ </pre>
<pre>    AssemblyPart assemblyPart = <span class="kwrd">new</span> AssemblyPart(); </pre>
<pre class="alt">    _testCtrl = assemblyPart.Load(e.Result); </pre>
<pre>    UserControl control = (UserControl) </pre>
<pre class="alt">    _testCtrl.CreateInstance(<span class="str">&quot;TestCtrl.ImageTest&quot;</span>); </pre>
<pre>    LayoutRoot.Children.Add(control); </pre>
<pre class="alt">}</pre>
</div>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>10. Before you run this code, as a final step, make certain to have a ClientAccessPolicy.xml file located in the same location as your DLL. Silverlight will look for this file for permissions when making calls cross-domain. By default Silverlight only allows site-of -origin communication. </p>
<div class="csharpcode">
<pre class="alt">&lt;?xml version=<span class="str">&quot;1.0&quot;</span> encoding=<span class="str">&quot;utf-8&quot;</span>?&gt; </pre>
<pre>&lt;access-policy&gt; </pre>
<pre class="alt">  &lt;cross-domain-access&gt; </pre>
<pre>    &lt;policy&gt; </pre>
<pre class="alt">      &lt;allow-from http-request-headers= <span class="str">&quot;*&quot;</span>&gt; </pre>
<pre>        &lt;domain uri=<span class="str">&quot;*&quot;</span>/&gt; </pre>
<pre class="alt">      &lt;/allow-from&gt; </pre>
<pre>      &lt;grant-to&gt; </pre>
<pre class="alt">        &lt;resource path=<span class="str">&quot;/&quot;</span> include-subpaths=<span class="str">&quot;true&quot;</span>/&gt; </pre>
<pre>      &lt;/grant-to&gt; </pre>
<pre class="alt">    &lt;/policy&gt; </pre>
<pre>  &lt;/cross-domain-access&gt; </pre>
<pre class="alt">&lt;/access-policy&gt;</pre>
</div>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:573fcf22-05db-4cb8-89f1-7f0a363f04c8" class="wlWriterEditableSmartContent">
<p>Source <a href="http://www.michaelsnow.com/wp-content/uploads/2010/06/Tip14_DLLLoading.zip" target="_blank">Tip14_DLLLoading</a></p>
</div>
<p>Demo:</p>
<p>[silverlight: Tip14_DLLLoading.xap]</p>
<p>Thank you,<br />
  <br />&#8211;Mike</p>
]]></content:encoded>
			<wfw:commentRss>http://www.michaelsnow.com/2010/05/06/silverlight-tip-of-the-day-14-dynamically-loading-a-control-from-a-dll-on-a-server/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

