<?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>Gaurav &#187; ruby</title>
	<atom:link href="http://allyourcodearebelongto.me/blog/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://allyourcodearebelongto.me/blog</link>
	<description>All About Everything</description>
	<lastBuildDate>Sun, 23 Nov 2008 19:44:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Context sensitive auto-completion using PowerShell, PowerTab and GIT</title>
		<link>http://allyourcodearebelongto.me/blog/2008/10/13/context-sensitive-auto-completion-using-powershell-powertab-and-git/</link>
		<comments>http://allyourcodearebelongto.me/blog/2008/10/13/context-sensitive-auto-completion-using-powershell-powertab-and-git/#comments</comments>
		<pubDate>Mon, 13 Oct 2008 19:33:46 +0000</pubDate>
		<dc:creator>Gaurav</dc:creator>
				<category><![CDATA[GIT]]></category>
		<category><![CDATA[powershell]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[vista]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://techblogging.wordpress.com/2008/10/13/context-sensitive-auto-completion-using-powershell-powertab-and-git/</guid>
		<description><![CDATA[Powertab is an Awesome PowerShell TabExpansion extension. It extends the default PowerShell autocompletion and shows the results like:
 
The best part is that it is easily customizable.
One way to customize it is by editing it Tab Expansion database file which is located in your powershell profile directory:
TabExpansion.xml

To display some simple git commands in autocompletion all [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://thepowershellguy.com/blogs/posh/pages/powertab.aspx">Powertab</a> is an Awesome PowerShell TabExpansion extension. It extends the default PowerShell autocompletion and shows the results like:</p>
<p><a href="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/10/image7.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/10/image-thumb8.png" width="541" height="488"></a> </p>
<p>The best part is that it is easily customizable.</p>
<p>One way to customize it is by editing it Tab Expansion database file which is located in your powershell profile directory:</p>
<p><strong>TabExpansion.xml</strong></p>
<p><span id="more-113"></span>
<p>To display some simple git commands in autocompletion all the following entries to it:</p>
</p>
<div style="border-bottom:gray 1px solid;border-left:gray 1px solid;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:consolas, 'Courier New', courier, monospace;max-height:200px;font-size:8pt;overflow:auto;border-top:gray 1px solid;cursor:text;border-right:gray 1px solid;margin:20px 0 10px;padding:4px;">
<div style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;">
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Custom</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>   <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Filter</span><span style="color:#0000ff;">&gt;</span>git<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Filter</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>   <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Text</span><span style="color:#0000ff;">&gt;</span>status<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Text</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>   <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Type</span><span style="color:#0000ff;">&gt;</span>Custom<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Type</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Custom</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Custom</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>   <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Filter</span><span style="color:#0000ff;">&gt;</span>git<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Filter</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>   <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Text</span><span style="color:#0000ff;">&gt;</span>add .<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Text</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>   <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Type</span><span style="color:#0000ff;">&gt;</span>Custom<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Type</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span> <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Custom</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Custom</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>   <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Filter</span><span style="color:#0000ff;">&gt;</span>git<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Filter</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>   <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Text</span><span style="color:#0000ff;">&gt;</span>pull<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Text</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>   <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Type</span><span style="color:#0000ff;">&gt;</span>Custom<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Type</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span> <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Custom</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courie
r, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Custom</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span>   <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Filter</span><span style="color:#0000ff;">&gt;</span>git<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Filter</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  18:</span>   <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Text</span><span style="color:#0000ff;">&gt;</span>push<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Text</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  19:</span>   <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Type</span><span style="color:#0000ff;">&gt;</span>Custom<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Type</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  20:</span> <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Custom</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  21:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Custom</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  22:</span>   <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Filter</span><span style="color:#0000ff;">&gt;</span>git<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Filter</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  23:</span>   <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Text</span><span style="color:#0000ff;">&gt;</span>commit -m ""<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Text</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  24:</span>   <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Type</span><span style="color:#0000ff;">&gt;</span>Custom<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Type</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  25:</span> <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Custom</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  26:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Custom</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  27:</span>   <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Filter</span><span style="color:#0000ff;">&gt;</span>git<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Filter</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  28:</span>   <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Text</span><span style="color:#0000ff;">&gt;</span>commit -a -m ""<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Text</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  29:</span>   <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Type</span><span style="color:#0000ff;">&gt;</span>Custom<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Type</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  30:</span> <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Custom</span><span style="color:#0000ff;">&gt;</span></pre>
</div>
</div>
<p>To invoke this list add the following to <strong>TabExpansion.ps1</strong> file which should be in the source directory:</p>
<div style="border-bottom:gray 1px solid;border-left:gray 1px solid;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:consolas, 'Courier New', courier, monospace;max-height:200px;font-size:8pt;overflow:auto;border-top:gray 1px solid;cursor:text;border-right:gray 1px solid;margin:20px 0 10px;padding:4px;">
<div style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;">
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#006080;">'git(.*)'</span> {</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>   <span style="color:#0000ff;">if</span>(Test-Path .git) {</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     $<span style="color:#0000ff;">global</span>:dsTabExpansion.Tables[<span style="color:#006080;">'Custom'</span>].select(<span style="color:#006080;">"filter = 'git' AND type = 'Custom'"</span>) | % {$_.text} | Invoke-TabItemSelector  -SelectionHandler $SelectionHandler</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>   }</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> }</pre>
</div>
</div>
<p>This should be added in the Main tabcompletion block inside the switch statement. The function is basically matching the current command name with <strong>git</strong> and then invoking autocompletion.</p>
<p>Now when you type git&lt;tab&gt; then the output should look like below:</p>
<p><a href="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/10/image8.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/10/image-thumb9.png" width="481" height="303"></a> </p>
<p>I am sure this is not the best way to do custom autocompletion but i couldn’t find an easier way.</p>
<p>This is all static stuff. To add dynamic autocompletion (like providing branch names) add following to the <strong>TabExpansion.ps1 </strong>file:</p>
</p>
<div style="border-bottom:gray 1px solid;border-left:gray 1px solid;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:consolas, 'Courier New', courier, monospace;max-height:200px;font-size:8pt;overflow:auto;border-top:gray 1px solid;cursor:text;border-right:gray 1px solid;margin:20px 0 10px;padding:4px;">
<div style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;">
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#006080;">'git(.*)'</span> {</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>   <span style="color:#0000ff;">if</span>(Test-Path .git) {</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#008000;"># git checkout</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     <span style="color:#0000ff;">if</span>($lastBlock <span style="color:#cc6633;">-match</span> <span style="color:#006080;">'git checkout'</span>){</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>       git branch | %{ <span style="color:#0000ff;">if</span>($_ <span style="color:#cc6633;">-match</span> <span style="color:#006080;">"^\*?\s*(.*)"</span>){ $matches[1] } } | Invoke-TabItemSelector  -SelectionHandler $SelectionHandler</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>     }</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>     <span style="color:#0000ff;">else</span>{</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>       $<span style="color:#0000ff;">global</span>:dsTabExpansion.Tables[<span style="color:#006080;">'Custom'</span>].select(<span style="color:#006080;">"filter = 'git' AND type = 'Custom'"</span>) | % {$_.text} | Invoke-TabItemSelector  -SelectionHandler $SelectionHandler</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>     }</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>   }</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span> }</pre>
</div>
</div>
<p>Now when you type <strong>git checkout &lt;tab&gt;</strong> then it should show the following in the autocomplete list:</p>
<p><a href="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/10/image9.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/10/image-thumb10.png" width="499" height="220"></a> </p>
<p>This function checks if the user has entered <strong>git checkout</strong> as the command then it runs the <strong>git branch</strong> command, parses it and shows the result as autocomplete list.</p>
<p>Here are couple more commands that I wrote while trying this out:</p>
<div style="border-bottom:gray 1px solid;border-left:gray 1px solid;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:consolas, 'Courier New', courier, monospace;max-height:200px;font-size:8pt;overflow:auto;border-top:gray 1px solid;cursor:text;border-right:gray 1px solid;margin:20px 0 10px;padding:4px;">
<div style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;">
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#006080;">'git(.*)'</span> {</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>   <span style="color:#0000ff;">if</span>(Test-Path .git) {</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#008000;"># git checkout</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     <span style="color:#0000ff;">if</span>($lastBlock <span style="color:#cc6633;">-match</span> <span style="color:#006080;">'git checkout'</span>){</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>       git branch | %{ <span style="color:#0000ff;">if</span>($_ <span style="color:#cc6633;">-match</span> <span style="color:#006080;">"^\*?\s*(.*)"</span>){ $matches[1] } } | Invoke-TabItemSelector  -SelectionHandler $SelectionHandler</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>     }</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>     </pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>     <span style="color:#008000;"># git add</span></pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>     Elseif($lastBlock <span style="color:#cc6633;">-match</span> <span style="color:#006080;">'git add'</span>){</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>       $s = $False ; git status | %{ <span style="color:#0000ff;">if</span>($s <span style="color:#cc6633;">-eq</span> $True -and $_ <span style="color:#cc6633;">-match</span> <span style="color:#006080;">"^#\s{1,1}(\S.+)$"</span>){ $matches[1] } ElseIf($_ <span style="color:#cc6633;">-match</span> <span style="color:#006080;">'Untracked files:'</span>){ $s = $True } } | Invoke-TabItemSelector  -SelectionHandler $SelectionHandler</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>     }</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>     </pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>     <span style="color:#008000;"># git rm</span></pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>     Elseif($lastBlock <span style="color:#cc6633;">-match</span> <span style="color:#006080;">'git rm'</span>){</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>       git status | %{ <span style="color:#0000ff;">if</span>($_ <span style="color:#cc6633;">-match</span> <span style="color:#006080;">"^#\s+deleted:\s+(\S.+)$"</span>){ $matches[1] } } | Invoke-TabItemSelector  -SelectionHandler $SelectionHandler</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span>     }</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span>     </pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  18:</span>     <span style="color:#0000ff;">else</span>{</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  19:</span>       $<span style="color:#0000ff;">global</span>:dsTabExpansion.Tables[<span style="color:#006080;">'Custom'</span>].select(<span style="color:#006080;">"filter = 'git' AND type = 'Custom'"</span>) | % {$_.text} | Invoke-TabItemSelector  -SelectionHandler $SelectionHandler</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  20:</span>     }</pre>
<pre style="line-height:12pt;background-color:white;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  21:</span>   }</pre>
<pre style="line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas, 'Courier New', courier, monospace;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  22:</span> }</pre>
</div>
</div>
<p>These will autocomplete parameters for <strong>git add</strong> and <strong>git rm </strong>commands by running the <strong>git status </strong>command and parsing the output.</p>
<p>For example:</p>
<p><a href="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/10/image10.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/10/image-thumb11.png" width="533" height="322"></a> </p>
<p>&nbsp;</p>
<p>I think a lot more interesting stuff can be created using PowerTab and GIT.</p>
<p>&nbsp;</p>
<h2>Update 1:</h2>
<p>Turns out as pointed by MOW that there is a much easier way to add custom expansions instead of editing the <strong>TabExpansion.xml</strong> file. </p>
<p>Just run this command inside PowerShell:</p>
<p>Invoke-TabExpansionEditor</p>
<p>It will open up a dialog box like:</p>
<p><a href="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/10/image15.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/10/image-thumb16.png" width="338" height="185"></a> </p>
<p>Here custom items can be added by clicking the <strong>Custom</strong> link and then the database change can by saved by running the command:</p>
<p>Export-tabExpansionDataBase</p>
]]></content:encoded>
			<wfw:commentRss>http://allyourcodearebelongto.me/blog/2008/10/13/context-sensitive-auto-completion-using-powershell-powertab-and-git/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Creating a chat application using Socket in Silverlight</title>
		<link>http://allyourcodearebelongto.me/blog/2008/04/17/creating-a-chat-application-using-socket-in-silverlight/</link>
		<comments>http://allyourcodearebelongto.me/blog/2008/04/17/creating-a-chat-application-using-socket-in-silverlight/#comments</comments>
		<pubDate>Thu, 17 Apr 2008 21:12:52 +0000</pubDate>
		<dc:creator>Gaurav</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[chat application]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[socket]]></category>
		<category><![CDATA[visual studio 2008]]></category>

		<guid isPermaLink="false">http://techblogging.wordpress.com/2008/04/17/creating-a-chat-application-using-socket-in-silverlight/</guid>
		<description><![CDATA[Here I will explain how to communicate with the server using the Socket class. The socket class allows Asynchronous communication between the client and server. This application will show how to share data in real-time in two different browser windows using Silverlight.
This application will contain two parts:

Server application using Ruby.
Client application using C# in Silverlight.

Creating [...]]]></description>
			<content:encoded><![CDATA[<p>Here I will explain how to communicate with the server using the <strong>Socket </strong>class. The socket class allows Asynchronous communication between the client and server. This application will show how to share data in real-time in two different browser windows using Silverlight.</p>
<p>This application will contain two parts:</p>
<ol>
<li>Server application using Ruby.</li>
<li>Client application using C# in Silverlight.</li>
</ol>
<h5><u>Creating the server application:</u></h5>
<p>Making a multi-threaded server application in ruby is really easy and that is the reason I chose Ruby to create the server in least amount of time possible.</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> require <span style="color:#008000;">'socket'</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> HOST = <span style="color:#008000;">'localhost'</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> PORT = 4505</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span> server = TCPServer.<span style="color:#0000ff;">new</span>(HOST, PORT)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span> # array <span style="color:#0000ff;">to</span> store all the active connections</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span> sessions = []</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span> <span style="color:#0000ff;">while</span> (session = server.accept)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>   # push the current session(socket) <span style="color:#0000ff;">in</span> the array</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>   sessions &lt;&lt; session</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>   # initialize a <span style="color:#0000ff;">new</span> thead <span style="color:#0000ff;">for</span> <span style="color:#0000ff;">each</span> connection</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>   Thread.<span style="color:#0000ff;">new</span>(session) <span style="color:#0000ff;">do</span> |local_session|</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>     # <span style="color:#0000ff;">each</span> time a client sends some data send it <span style="color:#0000ff;">to</span> all the connections</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span>     <span style="color:#0000ff;">while</span>(<span style="color:#0000ff;">true</span>)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span>       data = local_session.gets</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  18:</span>       sessions.<span style="color:#0000ff;">each</span> <span style="color:#0000ff;">do</span> |s| </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  19:</span>         begin</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  20:</span>           s.puts data</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  21:</span>         rescue Errno::ECONNRESET</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  22:</span>           # an exception <span style="color:#0000ff;">is</span> raised, that means the connection <span style="color:#0000ff;">to</span> the client <span style="color:#0000ff;">is</span> broken</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier,
 monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  23:</span>           sessions.delete(s)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  24:</span>         <span style="color:#0000ff;">end</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  25:</span>       <span style="color:#0000ff;">end</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  26:</span>     <span style="color:#0000ff;">end</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  27:</span>   <span style="color:#0000ff;">end</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  28:</span> end</pre>
</div>
</div>
<p>&nbsp;</p>
<p><a href="http://pastie.caboo.se/182562">Here is the pastie</a>.</p>
<p>Save this file as chat_server.rb</p>
<p><span id="more-88"></span></p>
<p>&nbsp;</p>
<p>This code is really quite simple and all it is doing is creating a new thread for each client connection and rejecting the invalid ones. Whenever a client sends some data it is propagated to all the clients.</p>
<p>&nbsp;</p>
<h5><u>Creating the client application:</u></h5>
<p>This is the difficult part among the two. As I mentioned earlier I will be using the <strong>Socket </strong>class. The important instance methods of this class are:</p>
<ol>
<li>ConnectAsync</li>
<li>SendAyns</li>
<li>ReceiveAync</li>
</ol>
<p>As their names suggest all of them are asynchronous. I am not sure of the reason. May be its because the communication does not block the UI thread and making it a bad experience for the client or may be its because of browser&#8217;s limitations.</p>
<p>Also in the beta-1 implementation the Silverlight <strong>Socket</strong> class only allows connection to 4502 &#8211; 4532 port range. I guess this limitation will be removed in the later versions.</p>
<p>Open up Visual Studio 2008 (with Silverlight <a href="http://techblogging.wordpress.com/2008/03/05/silverlight-20-beta-1-now-available-and-10-version-ported-to-nokia-mobiles/">tools</a> installed. You can also download a trial version of Visual Studio 2008 from <a href="http://msdn2.microsoft.com/en-us/vstudio/products/aa700831.aspx">here</a>). And create a new project:</p>
<p><a href="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/04/image1.png"><img style="border-right:0;border-top:0;border-left:0;border-bottom:0;" height="447" alt="image" src="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/04/image-thumb1.png" width="588" border="0"></a> </p>
<p>Name the application ChatClient and select rest of the default options.</p>
<p>Run the application. Visual Studio will prompt you to enable debugging, click OK to enable debugging and continue.</p>
<p>Add the <strong>System.net </strong>assembly to add the Socket class to solution.</p>
<p><u>Now to make a connection to the server we can use the Socket class:</u></p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> socket = <span style="color:#0000ff;">new</span> Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> SocketAsyncEventArgs connectEventArgs = <span style="color:#0000ff;">new</span> SocketAsyncEventArgs();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> connectEventArgs.RemoteEndPoint = IP_END_POINT;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> connectEventArgs.Completed += <span style="color:#0000ff;">new</span> EventHandler&lt;SocketAsyncEventArgs&gt;(connectEventArgs_Completed);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> socket.ConnectAsync(connectEventArgs);</pre>
</div>
</div>
<p>Here <strong>IP_END_POINT </strong>is an object of DnsEndPoint class and basically points to the server. I will be showing the code for it later for simplicity.</p>
<p>In the above code I created an instances of <strong>Socket </strong>class and <strong>SocketAsyncEventArgs </strong>class. The latter does the event handling for the former. So when the connection is complete it will just ca a event handler and then we can start sending/receiving data to server.</p>
<p><u>Code for sending data to the server:</u></p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> sendEventArgs = <span style="color:#0000ff;">new</span> SocketAsyncEventArgs();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> sendEventArgs.RemoteEndPoint = IP_END_POINT;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> List&lt;ArraySegment&lt;<span style="color:#0000ff;">byte</span>&gt;&gt; l = <span style="color:#0000ff;">new</span> List&lt;ArraySegment&lt;<span style="color:#0000ff;">byte</span>&gt;&gt;();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> l.Add(<span style="color:#0000ff;">new</span> ArraySegment&lt;<span style="color:#0000ff;">byte</span>&gt;(Encoding.UTF8.GetBytes(sendBox.Text + <span style="color:#006080;">"\n"</span>)));</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> sendEventArgs.BufferList = l;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span> socket.SendAsync(sendEventArgs);</pre>
</div>
</div>
<p>&nbsp;</p>
<p><strong>Socket </strong>uses a <strong>List</strong> to fill the data buffer. I have appended a <strong>&#8220;\n&#8221;</strong> in the data so that it is handled properly at the server.</p>
<p><u>Code to receive data from the server:</u></p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">void</span> connectEventArgs_Completed(<span style="color:#0000ff;">object</span> sender, SocketAsyncEventArgs e)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     e.Completed -= <span style="color:#0000ff;">new</span> EventHandler&lt;SocketAsyncEventArgs&gt;(connectEventArgs_Completed);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     e.Completed += <span style="color:#0000ff;">new</span> EventHandler&lt;SocketAsyncEventArgs&gt;(receiveEventArgs_Completed);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     transferBuffer = <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[BUFFER_SIZE];</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>     e.SetBuffer(transferBuffer, 0, transferBuffer.Length);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>     socket.ReceiveAsync(e);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span> }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span> <span style="color:#0000ff;">void</span> receiveEventArgs_Completed(<span style="color:#0000ff;">object</span> sender, SocketAsyncEventArgs e)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>     String a = Encoding.UTF8.GetString(transferBuffer, e.Offset, e.BytesTransferred);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>     SetText(a);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>     socket.ReceiveAsync(e);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span> }</pre>
</div>
</div>
<p>&nbsp;</p>
<p>When the connection to the server is complete the client begins to wait for the server to send data. This is done by removing the old event handler an creating a new one to receive the data. Also we need to initialise buffer so that incoming data can be received in it.</p>
<p>That is the basic structure you need to make for Asynchronous data transfer using <strong>Socket</strong> class.</p>
<h5><u>The full code:</u></h5>
<p><u>Page.xaml.cs</u></p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">using</span> System;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#0000ff;">using</span> System.Collections.Generic;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> <span style="color:#0000ff;">using</span> System.Linq;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> <span style="color:#0000ff;">using</span> System.Windows;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> <span style="color:#0000ff;">using</span> System.Windows.Controls;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span> <span style="color:#0000ff;">using</span> System.Windows.Documents;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> <span style="color:#0000ff;">using</span> System.Windows.Input;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span> <span style="color:#0000ff;">using</span> System.Windows.Media;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span> <span style="color:#0000ff;">using</span> System.Windows.Media.Animation;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span> <span style="color:#0000ff;">using</span> System.Windows.Shapes;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span> <span style="color:#0000ff;">using</span> System.Net.Sockets;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span> <span style="color:#0000ff;">using</span> System.Net;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span> <span style="color:#0000ff;">using</span> System.Text;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span> <span style="color:#0000ff;">namespace</span> DeleetThis</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">partial</span> <span style="color:#0000ff;">class</span> Page : UserControl</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  18:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  19:</span>         <span style="color:#0000ff;">private</span> Socket socket;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  20:</span>         <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">byte</span>[] transferBuffer;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  21:</span>         <span style="color:#0000ff;">private</span> SocketAsyncEventArgs sendEventArgs;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  22:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  23:</span>         <span style="color:#008000;">// contants</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  24:</span>         <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">int</span> BUFFER_SIZE = 500;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  25:</span>         <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">int</span> PORT = 4505;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courie
r New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  26:</span>         <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> DnsEndPoint IP_END_POINT = <span style="color:#0000ff;">new</span> DnsEndPoint(Application.Current.Host.Source.DnsSafeHost, PORT);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  27:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  28:</span>         <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">delegate</span> <span style="color:#0000ff;">void</span> SetTextCallback(String _text);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  29:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  30:</span>         <span style="color:#008000;">// initialize stuff</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  31:</span>         <span style="color:#0000ff;">public</span> Page()</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  32:</span>         {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  33:</span>             InitializeComponent();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  34:</span>             socket = <span style="color:#0000ff;">new</span> Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  35:</span>             SocketAsyncEventArgs connectEventArgs = <span style="color:#0000ff;">new</span> SocketAsyncEventArgs();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  36:</span>             connectEventArgs.RemoteEndPoint = IP_END_POINT;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  37:</span>             connectEventArgs.Completed += <span style="color:#0000ff;">new</span> EventHandler&lt;SocketAsyncEventArgs&gt;(connectEventArgs_Completed);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  38:</span>             socket.ConnectAsync(connectEventArgs);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  39:</span>         }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  40:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  41:</span>         <span style="color:#008000;">// connection complete</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  42:</span>         <span style="color:#0000ff;">void</span> connectEventArgs_Completed(<span style="color:#0000ff;">object</span> sender, SocketAsyncEventArgs e)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  43:</span>         {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  44:</span>             sendEventArgs = <span style="color:#0000ff;">new</span> SocketAsyncEventArgs();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  45:</span>             sendEventArgs.RemoteEndPoint = IP_END_POINT;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  46:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  47:</span>             e.Completed -= <span style="color:#0000ff;">new</span> EventHandler&lt;SocketAsyncEventArgs&gt;(connectEventArgs_Completed);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  48:</span>             e.Completed += <span style="color:#0000ff;">new</span> EventHandler&lt;SocketAsyncEventArgs&gt;(receiveEventArgs_Completed);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  49:</span>             transferBuffer = <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">byte</span>[BUFFER_SIZE];</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  50:</span>             e.SetBuffer(transferBuffer, 0, transferBuffer.Length);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', cou
rier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  51:</span>             socket.ReceiveAsync(e);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  52:</span>         }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  53:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  54:</span>         <span style="color:#008000;">// incoming data</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  55:</span>         <span style="color:#0000ff;">void</span> receiveEventArgs_Completed(<span style="color:#0000ff;">object</span> sender, SocketAsyncEventArgs e)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  56:</span>         {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  57:</span>             String a = Encoding.UTF8.GetString(transferBuffer, e.Offset, e.BytesTransferred);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  58:</span>             SetText(a);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  59:</span>             socket.ReceiveAsync(e);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  60:</span>         }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  61:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  62:</span>         <span style="color:#008000;">// this process is asynchronous so we need to invoke the ui thread to set the received text</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  63:</span>         <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> SetText(String text)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  64:</span>         {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  65:</span>             <span style="color:#0000ff;">if</span> (<span style="color:#0000ff;">this</span>.Dispatcher.CheckAccess())</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  66:</span>                 receiveBox.Text += text;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  67:</span>             <span style="color:#0000ff;">else</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  68:</span>                 <span style="color:#0000ff;">this</span>.Dispatcher.BeginInvoke(<span style="color:#0000ff;">new</span> SetTextCallback(SetText), text);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  69:</span>         }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  70:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  71:</span>         <span style="color:#008000;">// data sending</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  72:</span>         <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> sendButton_Click(<span style="color:#0000ff;">object</span> sender, RoutedEventArgs e)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  73:</span>         {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  74:</span>             List&lt;ArraySegment&lt;<span style="color:#0000ff;">byte</span>&gt;&gt; l = <span style="color:#0000ff;">new</span> List&lt;ArraySegment&lt;<span style="color:#0000ff;">byte</span>&gt;&gt;();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  75:</span>             l.Add(<span style="color:#0000ff;">new</span> ArraySegment&lt;<span style="color:#0000ff;">byte</span>&gt;(Encoding.UTF8.GetBytes(sendBox.Text + <span style="color:#006080;">"\n"</span>)));</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  76:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font
-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  77:</span>             sendEventArgs.BufferList = l;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  78:</span>             socket.SendAsync(sendEventArgs);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  79:</span>         }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  80:</span>     }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  81:</span> }</pre>
</div>
</div>
<p><a href="http://pastie.caboo.se/182586">Here is the pastie.</a></p>
<p><u>Page.xaml:</u></p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">UserControl</span> <span style="color:#ff0000;">x:Class</span><span style="color:#0000ff;">="ChatClient.Page"</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>     <span style="color:#ff0000;">xmlns</span><span style="color:#0000ff;">="http://schemas.microsoft.com/client/2007"</span> </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#ff0000;">xmlns:x</span><span style="color:#0000ff;">="http://schemas.microsoft.com/winfx/2006/xaml"</span> </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     <span style="color:#ff0000;">Width</span><span style="color:#0000ff;">="400"</span> <span style="color:#ff0000;">Height</span><span style="color:#0000ff;">="230"</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Grid</span> <span style="color:#ff0000;">x:Name</span><span style="color:#0000ff;">="LayoutRoot"</span> <span style="color:#ff0000;">Background</span><span style="color:#0000ff;">="White"</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>         <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">StackPanel</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>             <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">ScrollViewer</span> <span style="color:#ff0000;">Height</span><span style="color:#0000ff;">="200"</span> <span style="color:#0000ff;">&gt;</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>                 <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">TextBlock</span> <span style="color:#ff0000;">x:Name</span><span style="color:#0000ff;">="receiveBox"</span><span style="color:#0000ff;">/&gt;</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>             <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">ScrollViewer</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>             <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">StackPanel</span> <span style="color:#ff0000;">Orientation</span><span style="color:#0000ff;">="Horizontal"</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>                 <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">TextBox</span> <span style="color:#ff0000;">x:Name</span><span style="color:#0000ff;">="sendBox"</span> <span style="color:#ff0000;">Height</span><span style="color:#0000ff;">="30"</span> <span style="color:#ff0000;">Width</span><span style="color:#0000ff;">="365"</span><span style="color:#0000ff;">/&gt;</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>                 <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Button</span> <span style="color:#ff0000;">x:Name</span><span style="color:#0000ff;">="sendButton"</span> <span style="color:#ff0000;">Content</span><span style="color:#0000ff;">="Send"</span> <span style="color:#ff0000;">Click</span><span style="color:#0000ff;">="sendButton_Click"</span><span style="color:#0000ff;">/&gt;</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>             <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">StackPanel</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>         <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">StackPanel</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span>     <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Grid</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span> <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">UserControl</span><span style="color:#0000ff;">&gt;</span></pre>
</div>
</div>
<p><a href="http://pastie.caboo.se/182585">Here is the pastie.</a></p>
<h5><u>Running the application:</u></h5>
<p>Now run this application in two different browser windows and whenever you type in one window the content of both windows should be updated :)</p>
<p><a href="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/04/image2.png"><img style="border-right:0;border-top:0;border-left:0;border-bottom:0;" height="441" alt="image" src="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/04/image-thumb2.png" width="587" border="0"></a>&nbsp;</p>
<p>Download the source code:</p>
<ul>
<li><a href="http://cid-49208a72e4eb818c.skydrive.live.com/self.aspx/world/code/chat_server.rb">Ruby server</a></li>
<li><a href="http://cid-49208a72e4eb818c.skydrive.live.com/self.aspx/world/code/ChatClient.rar">Silverlight client</a></li>
</ul>
<div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:60c01296-59d8-4395-a333-8305d44473bf" style="display:inline;float:none;margin:0;padding:0;">del.icio.us Tags: <a href="http://del.icio.us/popular/C#" rel="tag">C#</a>,<a href="http://del.icio.us/popular/ruby" rel="tag">ruby</a>,<a href="http://del.icio.us/popular/silverlight" rel="tag">silverlight</a>,<a href="http://del.icio.us/popular/socket" rel="tag">socket</a>,<a href="http://del.icio.us/popular/visual%20studio%202008" rel="tag">visual studio 2008</a>,<a href="http://del.icio.us/popular/chat%20application" rel="tag">chat application</a></div>
]]></content:encoded>
			<wfw:commentRss>http://allyourcodearebelongto.me/blog/2008/04/17/creating-a-chat-application-using-socket-in-silverlight/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Using Flex with Ruby on Rails</title>
		<link>http://allyourcodearebelongto.me/blog/2008/03/26/using-flex-with-rubyonrails/</link>
		<comments>http://allyourcodearebelongto.me/blog/2008/03/26/using-flex-with-rubyonrails/#comments</comments>
		<pubDate>Wed, 26 Mar 2008 19:08:39 +0000</pubDate>
		<dc:creator>Gaurav</dc:creator>
				<category><![CDATA[adobe]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[mxml]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[xaml]]></category>

		<guid isPermaLink="false">http://techblogging.wordpress.com/2008/03/26/using-flex-with-rubyonrails/</guid>
		<description><![CDATA[This is the second blog post in a series of three posts comparing Flex with Silverlight. In my previous post I demonstrated how to integrate a scaffolded Rails application with Silverlight. Here I will be showing the same with Flex. The steps needed to create the Rails application are also mentioned here.
&#160;
Creating a new Flex [...]]]></description>
			<content:encoded><![CDATA[<p>This is the second blog post in a series of three posts comparing Flex with Silverlight. In my <a href="http://techblogging.wordpress.com/2008/03/26/using-silverlight-with-rubyonrails/">previous</a> post I demonstrated how to integrate a scaffolded Rails application with Silverlight. Here I will be showing the same with Flex. The steps needed to create the Rails application are also mentioned <a href="http://techblogging.wordpress.com/2008/03/26/using-silverlight-with-rubyonrails/">here</a>.</p>
<p>&nbsp;</p>
<h5><u>Creating a new Flex project:</u></h5>
<p>Open Flex builder and create a new Flex project.</p>
<p><a href="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/03/image8.png"><img style="border-width:0;" height="484" alt="image" src="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/03/image-thumb8.png" width="517" border="0"></a></p>
<p>Leave all the options to default and click Finish. A blank project is now created for you.</p>
</p>
<p><span id="more-74"></span>
</p>
<p>Add a List control to the default mxml file. It should look like:</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">&lt;?</span><span style="color:#800000;">xml</span> <span style="color:#ff0000;">version</span><span style="color:#0000ff;">="1.0"</span> <span style="color:#ff0000;">encoding</span><span style="color:#0000ff;">="utf-8"</span>?<span style="color:#0000ff;">&gt;</span>
<span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:Application</span> <span style="color:#ff0000;">xmlns:mx</span><span style="color:#0000ff;">="http://www.adobe.com/2006/mxml"</span> <span style="color:#ff0000;">layout</span><span style="color:#0000ff;">="vertical"</span><span style="color:#0000ff;">&gt;</span>
    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:List</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="userList"</span> <span style="color:#ff0000;">width</span><span style="color:#0000ff;">="200"</span> <span style="color:#ff0000;">height</span><span style="color:#0000ff;">="200"</span><span style="color:#0000ff;">/&gt;</span>
<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">mx:Application</span><span style="color:#0000ff;">&gt;</span></pre>
</div>
<p>&nbsp;</p>
<p>Now create a script tag inside the source and add a Bindable element to it:</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">[Bindable]
<span style="color:#0000ff;">private</span> <span style="color:#0000ff;">var</span> users:XMLListCollection = <span style="color:#0000ff;">new</span> XMLListCollection();</pre>
</div>
<p>I am using an XMLListCollection because I can easily add/remove elements from it.</p>
<p>&nbsp;</p>
<h5><u>Now send request to the server and bind the result to the list box:</u></h5>
<p>First create a new HTTPService element:</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">&lt;mx:HTTPService id=<span style="color:#006080;">"userIndexService"</span> resultFormat=<span style="color:#006080;">"e4x"</span> result=<span style="color:#006080;">"userIndexServiceResult(event)"</span> url=<span style="color:#006080;">"http://localhost:3000/users.xml"</span>/&gt;</pre>
</div>
<p>&nbsp;</p>
<p>and handle its result:</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">function</span> userIndexServiceResult(<span style="color:#0000ff;">event</span>:ResultEvent):<span style="color:#0000ff;">void</span>{
    <span style="color:#0000ff;">for</span> each(<span style="color:#0000ff;">var</span> xml:XML <span style="color:#0000ff;">in</span> <span style="color:#0000ff;">event</span>.result.user <span style="color:#0000ff;">as</span> XMLList){
        users.addItem(xml);
    }
}</pre>
</div>
<p>&nbsp;</p>
<p>To invoke this service we need to call its &#8220;send()&#8221; method with appropriate paramater, which in this case are none.</p>
<p>So the code now looks like:</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">&lt;?</span><span style="color:#800000;">xml</span> <span style="color:#ff0000;">version</span><span style="color:#0000ff;">="1.0"</span> <span style="color:#ff0000;">encoding</span><span style="color:#0000ff;">="utf-8"</span>?<span style="color:#0000ff;">&gt;</span>
<span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:Application</span> <span style="color:#ff0000;">xmlns:mx</span><span style="color:#0000ff;">="http://www.adobe.com/2006/mxml"</span> <span style="color:#ff0000;">layout</span><span style="color:#0000ff;">="vertical"</span> <span style="color:#ff0000;">initialize</span><span style="color:#0000ff;">="sendServiceRequest(event)"</span><span style="color:#0000ff;">&gt;</span>
    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:Script</span><span style="color:#0000ff;">&gt;</span>
        <span style="color:#0000ff;">&lt;!</span>[CDATA[
            import mx.rpc.events.ResultEvent;
            import mx.collections.XMLListCollection;
            [Bindable]
            private var users:XMLListCollection = new XMLListCollection();

            private function sendServiceRequest(event:Event):void{
                userIndexService.send();
            }
            private function userIndexServiceResult(event:ResultEvent):void{
                for each(var xml:XML in event.result.user as XMLList){
                    users.addItem(xml);
                }
            }
        ]]<span style="color:#0000ff;">&gt;</span>
    <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">mx:Script</span><span style="color:#0000ff;">&gt;</span>
    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:HTTPService</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="userIndexService"</span> <span style="color:#ff0000;">resultFormat</span><span style="color:#0000ff;">="e4x"</span> <span style="color:#ff0000;">result</span><span style="color:#0000ff;">="userIndexServiceResult(event)"</span> <span style="color:#ff0000;">url</span><span style="color:#0000ff;">="http://localhost:3000/users.xml"</span><span style="color:#0000ff;">/&gt;</span>
    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:List</span> <span style="color:#ff0000;">dataProvider</span><span style="color:#0000ff;">="{users}"</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="userList"</span> <span style="color:#ff0000;">width</span><span style="color:#0000ff;">="200"</span> <span style="color:#ff0000;">height</span><span style="color:#0000ff;">="200"</span> <span style="color:#ff0000;">labelField</span><span style="color:#0000ff;">="name"</span><span style="color:#0000ff;">/&gt;</span>
<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">mx:Application</span><span style="color:#0000ff;">&gt;</span></pre>
</div>
<p>&nbsp;</p>
<p>And the output looks like:</p>
<p><a href="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/03/image9.png"><img style="border-width:0;" height="411" alt="image" src="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/03/image-thumb9.png" width="454" border="0"></a></p>
<p>Notice that we are sending the index request in the initialize method of the application.</p>
<p>Now to add controls which show the currently selected element. Its really easy. Just add the folowwing to the code and DataBinding will do the rest:</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:HBox</span><span style="color:#0000ff;">&gt;</span>
    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:Label</span> <span style="color:#ff0000;">text</span><span style="color:#0000ff;">="Name"</span> <span style="color:#ff0000;">width</span><span style="color:#0000ff;">="50"</span><span style="color:#0000ff;">/&gt;</span>
    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:TextInput</span> <span style="color:#ff0000;">text</span><span style="color:#0000ff;">="{userList.selectedItem.name}"</span> <span style="color:#ff0000;">width</span><span style="color:#0000ff;">="150"</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="userName"</span><span style="color:#0000ff;">/&gt;</span>
<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">mx:HBox</span><span style="color:#0000ff;">&gt;</span>
<span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:HBox</span><span style="color:#0000ff;">&gt;</span>
    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:Label</span> <span style="color:#ff0000;">text</span><span style="color:#0000ff;">="Address"</span> <span style="color:#ff0000;">width</span><span style="color:#0000ff;">="50"</span><span style="color:#0000ff;">/&gt;</span>
    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:TextInput</span> <span style="color:#ff0000;">text</span><span style="color:#0000ff;">="{userList.selectedItem.address}"</span> <span style="color:#ff0000;">width</span><span style="color:#0000ff;">="150"</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="userAddress"</span><span style="color:#0000ff;">/&gt;</span>
<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">mx:HBox</span><span style="color:#0000ff;">&gt;</span></pre>
</div>
<h5><u></u>&nbsp;</h5>
<p>Now it loooks like:</p>
<p><a href="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/03/image10.png"><img style="border-width:0;" height="442" alt="image" src="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/03/image-thumb10.png" width="442" border="0"></a></p>
<h5><u></u>&nbsp;</h5>
<h5><u>Implementing the full CRUD:</u></h5>
<p>Now we need to add the other three HTTPServices.</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:HTTPService</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="userIndexService"</span> <span style="color:#ff0000;">resultFormat</span><span style="color:#0000ff;">="e4x"</span> <span style="color:#ff0000;">result</span><span style="color:#0000ff;">="userIndexServiceResult(event)"</span> <span style="color:#ff0000;">url</span><span style="color:#0000ff;">="http://localhost:3000/users.xml"</span><span style="color:#0000ff;">/&gt;</span>
<span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:HTTPService</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="userCreateService"</span> <span style="color:#ff0000;">resultFormat</span><span style="color:#0000ff;">="e4x"</span> <span style="color:#ff0000;">result</span><span style="color:#0000ff;">="userCreateServiceResult(event)"</span> <span style="color:#ff0000;">method</span><span style="color:#0000ff;">="POST"</span> <span style="color:#ff0000;">url</span><span style="color:#0000ff;">="http://localhost:3000/users.xml"</span><span style="color:#0000ff;">/&gt;</span>
<span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:HTTPService</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="userUpdateService"</span> <span style="color:#ff0000;">resultFormat</span><span style="color:#0000ff;">="e4x"</span> <span style="color:#ff0000;">method</span><span style="color:#0000ff;">="POST"</span><span style="color:#0000ff;">/&gt;</span>
<span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:HTTPService</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="userDeleteService"</span> <span style="color:#ff0000;">resultFormat</span><span style="color:#0000ff;">="e4x"</span> <span style="color:#ff0000;">method</span><span style="color:#0000ff;">="POST"</span><span style="color:#0000ff;">/&gt;</span></pre>
</div>
<p>&nbsp;</p>
<p>And their corresponding callbacks.</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">function</span> userCreateServiceResult(<span style="color:#0000ff;">event</span>:ResultEvent):<span style="color:#0000ff;">void</span>{
    users.addItem(<span style="color:#0000ff;">event</span>.result <span style="color:#0000ff;">as</span> XML);
}</pre>
</div>
<p>&nbsp;</p>
<p>Some controls to invoke these services:</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:HBox</span><span style="color:#0000ff;">&gt;</span>
    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:Button</span> <span style="color:#ff0000;">label</span><span style="color:#0000ff;">="Create"</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="createUser"</span> <span style="color:#ff0000;">click</span><span style="color:#0000ff;">="sendServiceRequest(event)"</span><span style="color:#0000ff;">/&gt;</span>
    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:Button</span> <span style="color:#ff0000;">label</span><span style="color:#0000ff;">="Update"</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="updateUser"</span> <span style="color:#ff0000;">click</span><span style="color:#0000ff;">="sendServiceRequest(event)"</span><span style="color:#0000ff;">/&gt;</span>
    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:Button</span> <span style="color:#ff0000;">label</span><span style="color:#0000ff;">="Delete"</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="deleteUser"</span> <span style="color:#ff0000;">click</span><span style="color:#0000ff;">="sendServiceRequest(event)"</span><span style="color:#0000ff;">/&gt;</span>
<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">mx:HBox</span><span style="color:#0000ff;">&gt;</span></pre>
</div>
<p>&nbsp;</p>
<p>And their event handlers (just one in this case):</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">function</span> sendServiceRequest(<span style="color:#0000ff;">event</span>:Event):<span style="color:#0000ff;">void</span>{
    <span style="color:#0000ff;">if</span>(<span style="color:#0000ff;">event</span>.target.id == <span style="color:#006080;">'createUser'</span>){
        userCreateService.send({<span style="color:#006080;">'user[name]'</span>: userName.text, <span style="color:#006080;">'user[address]'</span>: userAddress.text});
    }<span style="color:#0000ff;">else</span> <span style="color:#0000ff;">if</span>(<span style="color:#0000ff;">event</span>.target.id == <span style="color:#006080;">'updateUser'</span>){
        userUpdateService.url = <span style="color:#006080;">'http://localhost:3000/users/'</span>+ userList.selectedItem.id +<span style="color:#006080;">'.xml'</span>;
        userUpdateService.send({<span style="color:#006080;">'user[name]'</span>: userName.text, <span style="color:#006080;">'user[address]'</span>: userAddress.text, _method: <span style="color:#006080;">'put'</span>});
        users[userList.selectedIndex].name = userName.text;
        users[userList.selectedIndex].address = userAddress.text;
    }<span style="color:#0000ff;">else</span> <span style="color:#0000ff;">if</span>(<span style="color:#0000ff;">event</span>.target.id == <span style="color:#006080;">'deleteUser'</span>){
        userDeleteService.url = <span style="color:#006080;">'http://localhost:3000/users/'</span>+ userList.selectedItem.id +<span style="color:#006080;">'.xml'</span>;
        userDeleteService.send({_method: <span style="color:#006080;">'delete'</span>});
        users.removeItemAt(userList.selectedIndex);
    }<span style="color:#0000ff;">else</span>{
        userIndexService.send();
    }
}</pre>
</div>
<p>&nbsp;</p>
<p>Little bit of&nbsp; knowledge of how REST is implemented in Rails is required to understand the parameters to the&nbsp;&nbsp;&nbsp;&nbsp; &#8220;send()&#8221; method of HTTPService class is required, which is out of scope of this blog post.</p>
<p>The TestFrontEnd.mxml should now have the folowwing code:</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">&lt;?</span><span style="color:#800000;">xml</span> <span style="color:#ff0000;">version</span><span style="color:#0000ff;">="1.0"</span> <span style="color:#ff0000;">encoding</span><span style="color:#0000ff;">="utf-8"</span>?<span style="color:#0000ff;">&gt;</span>
<span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:Application</span> <span style="color:#ff0000;">xmlns:mx</span><span style="color:#0000ff;">="http://www.adobe.com/2006/mxml"</span> <span style="color:#ff0000;">layout</span><span style="color:#0000ff;">="vertical"</span> <span style="color:#ff0000;">initialize</span><span style="color:#0000ff;">="sendServiceRequest(event)"</span><span style="color:#0000ff;">&gt;</span>
    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:Script</span><span style="color:#0000ff;">&gt;</span>
        <span style="color:#0000ff;">&lt;!</span>[CDATA[
            import mx.collections.XMLListCollection;
            import mx.controls.Alert;
            import mx.rpc.events.ResultEvent;
            import mx.collections.ArrayCollection;
            [Bindable]
            private var users:XMLListCollection = new XMLListCollection();

            private function sendServiceRequest(event:Event):void{
                if(event.target.id == 'createUser'){
                    userCreateService.send({'user[name]': userName.text, 'user[address]': userAddress.text});
                }else if(event.target.id == 'updateUser'){
                    userUpdateService.url = 'http://localhost:3000/users/'+ userList.selectedItem.id +'.xml';
                    userUpdateService.send({'user[name]': userName.text, 'user[address]': userAddress.text, _method: 'put'});
                    users[userList.selectedIndex].name = userName.text;
                    users[userList.selectedIndex].address = userAddress.text;
                }else if(event.target.id == 'deleteUser'){
                    userDeleteService.url = 'http://localhost:3000/users/'+ userList.selectedItem.id +'.xml';
                    userDeleteService.send({_method: 'delete'});
                    users.removeItemAt(userList.selectedIndex);
                }else{
                    userIndexService.send();
                }
            }

            private function userIndexServiceResult(event:ResultEvent):void{
                for each(var xml:XML in event.result.user as XMLList){
                    users.addItem(xml);
                }
            }
            private function userCreateServiceResult(event:ResultEvent):void{
                users.addItem(event.result as XML);
            }
        ]]<span style="color:#0000ff;">&gt;</span>
    <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">mx:Script</span><span style="color:#0000ff;">&gt;</span>

    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:HTTPService</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="userIndexService"</span> <span style="color:#ff0000;">resultFormat</span><span style="color:#0000ff;">="e4x"</span> <span style="color:#ff0000;">result</span><span style="color:#0000ff;">="userIndexServiceResult(event)"</span> <span style="color:#ff0000;">url</span><span style="color:#0000ff;">="http://localhost:3000/users.xml"</span><span style="color:#0000ff;">/&gt;</span>
    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:HTTPService</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="userCreateService"</span> <span style="color:#ff0000;">resultFormat</span><span style="color:#0000ff;">="e4x"</span> <span style="color:#ff0000;">result</span><span style="color:#0000ff;">="userCreateServiceResult(event)"</span> <span style="color:#ff0000;">method</span><span style="color:#0000ff;">="POST"</span> <span style="color:#ff0000;">url</span><span style="color:#0000ff;">="http://localhost:3000/users.xml"</span><span style="color:#0000ff;">/&gt;</span>
    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:HTTPService</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="userUpdateService"</span> <span style="color:#ff0000;">resultFormat</span><span style="color:#0000ff;">="e4x"</span> <span style="color:#ff0000;">method</span><span style="color:#0000ff;">="POST"</span><span style="color:#0000ff;">/&gt;</span>
    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:HTTPService</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="userDeleteService"</span> <span style="color:#ff0000;">resultFormat</span><span style="color:#0000ff;">="e4x"</span> <span style="color:#ff0000;">method</span><span style="color:#0000ff;">="POST"</span><span style="color:#0000ff;">/&gt;</span>

    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:List</span> <span style="color:#ff0000;">dataProvider</span><span style="color:#0000ff;">="{users}"</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="userList"</span> <span style="color:#ff0000;">width</span><span style="color:#0000ff;">="200"</span> <span style="color:#ff0000;">height</span><span style="color:#0000ff;">="200"</span> <span style="color:#ff0000;">labelField</span><span style="color:#0000ff;">="name"</span><span style="color:#0000ff;">/&gt;</span>

    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:HBox</span><span style="color:#0000ff;">&gt;</span>
        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:Label</span> <span style="color:#ff0000;">text</span><span style="color:#0000ff;">="Name"</span> <span style="color:#ff0000;">width</span><span style="color:#0000ff;">="50"</span><span style="color:#0000ff;">/&gt;</span>
        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:TextInput</span> <span style="color:#ff0000;">text</span><span style="color:#0000ff;">="{userList.selectedItem.name}"</span> <span style="color:#ff0000;">width</span><span style="color:#0000ff;">="150"</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="userName"</span><span style="color:#0000ff;">/&gt;</span>
    <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">mx:HBox</span><span style="color:#0000ff;">&gt;</span>
    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:HBox</span><span style="color:#0000ff;">&gt;</span>
        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:Label</span> <span style="color:#ff0000;">text</span><span style="color:#0000ff;">="Address"</span> <span style="color:#ff0000;">width</span><span style="color:#0000ff;">="50"</span><span style="color:#0000ff;">/&gt;</span>
        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:TextInput</span> <span style="color:#ff0000;">text</span><span style="color:#0000ff;">="{userList.selectedItem.address}"</span> <span style="color:#ff0000;">width</span><span style="color:#0000ff;">="150"</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="userAddress"</span><span style="color:#0000ff;">/&gt;</span>
    <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">mx:HBox</span><span style="color:#0000ff;">&gt;</span>
    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:HBox</span><span style="color:#0000ff;">&gt;</span>
        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:Button</span> <span style="color:#ff0000;">label</span><span style="color:#0000ff;">="Create"</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="createUser"</span> <span style="color:#ff0000;">click</span><span style="color:#0000ff;">="sendServiceRequest(event)"</span><span style="color:#0000ff;">/&gt;</span>
        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:Button</span> <span style="color:#ff0000;">label</span><span style="color:#0000ff;">="Update"</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="updateUser"</span> <span style="color:#ff0000;">click</span><span style="color:#0000ff;">="sendServiceRequest(event)"</span><span style="color:#0000ff;">/&gt;</span>
        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">mx:Button</span> <span style="color:#ff0000;">label</span><span style="color:#0000ff;">="Delete"</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="deleteUser"</span> <span style="color:#ff0000;">click</span><span style="color:#0000ff;">="sendServiceRequest(event)"</span><span style="color:#0000ff;">/&gt;</span>
    <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">mx:HBox</span><span style="color:#0000ff;">&gt;</span>

<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">mx:Application</span><span style="color:#0000ff;">&gt;</span></pre>
</div>
<p>&nbsp;</p>
<p>This is the end result:</p>
<p><a href="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/03/image11.png"><img style="border-width:0;" height="483" alt="image" src="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/03/image-thumb11.png" width="441" border="0"></a></p>
<p>As I mentioned in my last blog post it is not the best way to make this application as there is no error handling. But I think this is the simplest way to make this application.</p>
<p>You also might have noticed Flex requires a lot less lines of code as compared to Silverlight. But that is also dependent on my coding approach.</p>
<p>Full code for this application can be downloaded from:</p>
<ul>
<li><a href="http://cid-49208a72e4eb818c.skydrive.live.com/self.aspx/world/code/test.rar">Rails code</a>
<li><a href="http://cid-49208a72e4eb818c.skydrive.live.com/self.aspx/world/code/TestFrontEndFlex.rar">Flex code</a> </li>
</ul>
<div class="wlWriterSmartContent" id="9cde01ca-f4d1-4e36-94d6-1cd81702baf9" style="display:inline;margin:0;padding:0;">del.icio.us Tags: <a href="http://del.icio.us/popular/adobe" rel="tag">adobe</a>,<a href="http://del.icio.us/popular/flex" rel="tag">flex</a>,<a href="http://del.icio.us/popular/mxmx" rel="tag">mxmx</a>,<a href="http://del.icio.us/popular/rails" rel="tag">rails</a>,<a href="http://del.icio.us/popular/ruby" rel="tag">ruby</a>,<a href="http://del.icio.us/popular/ruby%20on%20rails" rel="tag">ruby on rails</a>,<a href="http://del.icio.us/popular/silverlight" rel="tag">silverlight</a>,<a href="http://del.icio.us/popular/xaml" rel="tag">xaml</a></div>
]]></content:encoded>
			<wfw:commentRss>http://allyourcodearebelongto.me/blog/2008/03/26/using-flex-with-rubyonrails/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Using Silverlight with Ruby on Rails</title>
		<link>http://allyourcodearebelongto.me/blog/2008/03/26/using-silverlight-with-rubyonrails/</link>
		<comments>http://allyourcodearebelongto.me/blog/2008/03/26/using-silverlight-with-rubyonrails/#comments</comments>
		<pubDate>Wed, 26 Mar 2008 12:55:12 +0000</pubDate>
		<dc:creator>Gaurav</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[adobe]]></category>
		<category><![CDATA[crud]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[mxml]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[xaml]]></category>

		<guid isPermaLink="false">http://techblogging.wordpress.com/2008/03/26/using-silverlight-with-rubyonrails/</guid>
		<description><![CDATA[In this post I will be showing a really simple example of creating a Silverlight frontend for a Rails backend. This is what I think will be a three part series comparing Silverlight with Flex.
The steps will be:

Creating a Rails application.
Creating a frontend for it in silverlight.
Creating a frontend for it in Flex.
Comparing the approaches [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I will be showing a really simple example of creating a Silverlight frontend for a Rails backend. This is what I think will be a three part series comparing Silverlight with Flex.</p>
<p>The steps will be:</p>
<ol>
<li>Creating a Rails application.
<li>Creating a frontend for it in silverlight.
<li>Creating a frontend for it in Flex.
<li>Comparing the approaches taken in both the frontends. </li>
</ol>
<p> I will try to keep changes in the backend Rails application to a minimum.</p>
</p>
<p><span id="more-55"></span>
</p>
<h5><u>Creating a Rails Application</u></h5>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">F:\<span style="color:#0000ff;">&gt;</span>rails test
F:\<span style="color:#0000ff;">&gt;</span>cd test
F:\test<span style="color:#0000ff;">&gt;</span>ruby script\generate scaffold user name:string address:text
F:\test<span style="color:#0000ff;">&gt;</span>rake db:create
F:\test<span style="color:#0000ff;">&gt;</span>rake db:migrate
F:\test<span style="color:#0000ff;">&gt;</span>ruby script\server</pre>
</div>
<p>Before running the migrations add the following code in the first migration file (./db/migrate/001_create_users.rb) to create some initial data.&nbsp; So it should look like:</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">class</span> CreateUsers &lt; ActiveRecord::Migration
  def self.up
    create_table :users <span style="color:#0000ff;">do</span> |t|
      t.<span style="color:#0000ff;">string</span> :name
      t.text :address
      t.timestamps
    end

    User.create(:name=&gt; <span style="color:#006080;">"Gaurav"</span>, :address=&gt; <span style="color:#006080;">"Gaurav's address"</span>)
    User.create(:name=&gt; <span style="color:#006080;">"Rishav"</span>, :address=&gt; <span style="color:#006080;">"Rishav's address"</span>)
    User.create(:name=&gt; <span style="color:#006080;">"Bhavesh"</span>, :address=&gt; <span style="color:#006080;">"Bhavesh's address"</span>)
  end

  def self.down
    drop_table :users
  end
end</pre>
</div>
<p>&nbsp;</p>
<h5><u>Creating a new C# based Silverlight project</u></h5>
<p>&nbsp;</p>
<p>Open up Visual Studio 2008 (with Silverlight <a href="http://techblogging.wordpress.com/2008/03/05/silverlight-20-beta-1-now-available-and-10-version-ported-to-nokia-mobiles/">tools</a> installed. You can also download a trial version of Visual Studio 2008 from <a href="http://msdn2.microsoft.com/en-us/vstudio/products/aa700831.aspx">here</a>). And create a new project:</p>
<p><a href="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/03/vs-step1.png"><img style="border-width:0;" height="415" alt="vs_step1" src="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/03/vs-step1-thumb.png" width="609" border="0"></a></p>
<p><a href="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/03/vs-step2.png"><img style="border-width:0;" height="368" alt="vs_step2" src="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/03/vs-step2-thumb.png" width="439" border="0"></a></p>
<p>&nbsp;</p>
<p>Open the page.xaml file and add a ListBox to it. It should look like:</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">&lt;</span><span style="color:#800000;">UserControl</span> <span style="color:#ff0000;">x:Class</span><span style="color:#0000ff;">="TestFrontEnd.Page"</span>
    <span style="color:#ff0000;">xmlns</span><span style="color:#0000ff;">="http://schemas.microsoft.com/client/2007"</span>
    <span style="color:#ff0000;">xmlns:x</span><span style="color:#0000ff;">="http://schemas.microsoft.com/winfx/2006/xaml"</span>
    <span style="color:#ff0000;">Width</span><span style="color:#0000ff;">="400"</span> <span style="color:#ff0000;">Height</span><span style="color:#0000ff;">="300"</span><span style="color:#0000ff;">&gt;</span>
    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Grid</span> <span style="color:#ff0000;">x:Name</span><span style="color:#0000ff;">="LayoutRoot"</span> <span style="color:#ff0000;">Background</span><span style="color:#0000ff;">="White"</span><span style="color:#0000ff;">&gt;</span>
        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">ListBox</span> <span style="color:#ff0000;">x:Name</span><span style="color:#0000ff;">="userList"</span> <span style="color:#ff0000;">Height</span><span style="color:#0000ff;">="200"</span><span style="color:#0000ff;">/&gt;</span>
    <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Grid</span><span style="color:#0000ff;">&gt;</span>
<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">UserControl</span><span style="color:#0000ff;">&gt;</span></pre>
</div>
<p>&nbsp;</p>
<p>Now when the server responds with xml, we will parse that using <a href="http://msdn2.microsoft.com/en-us/netframework/aa904594.aspx">Linq</a>. For that we need a User class that is like the user model on the Rails side. So it should have 3 fields:</p>
<ol>
<li>id
<li>name
<li>address </li>
</ol>
<p>It should look like:</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">namespace</span> TestFrontEnd {
    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> User {
        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">int</span> id { get; set; }
        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">string</span> name { get; set; }
        <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">string</span> address { get; set; }
    }
}</pre>
</div>
<p>&nbsp;</p>
<p>To make things simpler I have created a HTTPService class that behaves something like the Flex based HTTPService class. <a href="http://pastie.caboo.se/170803">Here is the source</a> (too big to paste inline). Add it to your project. You will need to include System.Net assembly your project to to make it work. Its usage is quite simple.</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">HTTPService userIndexService = <span style="color:#0000ff;">new</span> HTTPService();
userIndexService.Url = <span style="color:#006080;">"http://localhost:3000/users.xml"</span>;
userIndexService.ServiceComplete += <span style="color:#0000ff;">new</span> HttpServiceCompleteEventHandler(userIndexService_ServiceComplete);
userIndexService.send();</pre>
</div>
<p>&nbsp;</p>
<p>You can also give the request method like:</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">userIndexService.Method = <span style="color:#006080;">"POST"</span>;</pre>
</div>
<p>&nbsp;</p>
<p>Now add an ObservableCollection to the Page class:</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">private</span> ObservableCollection&lt;User&gt; userListItems = <span style="color:#0000ff;">new</span> ObservableCollection&lt;User&gt;();</pre>
</div>
<p>&nbsp;</p>
<p>We will be using this for <a href="http://msdn2.microsoft.com/en-us/library/ms752347.aspx">databinding</a> with the ListBox to display all the users.</p>
<p>Add the System.Xml.Linq to your project. In the userIndexService_ServiceComplete event handler write:</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">void</span> userIndexService_ServiceComplete(HttpServiceCompleteEventArgs e) {
    XDocument users = XDocument.Parse(e.Response);
    User[] usersList = (from x <span style="color:#0000ff;">in</span> users.Descendants(<span style="color:#006080;">"user"</span>)
            select <span style="color:#0000ff;">new</span> User
            {
                id = <span style="color:#0000ff;">int</span>.Parse(x.Descendants(<span style="color:#006080;">"id"</span>).First().Value),
                name = x.Descendants(<span style="color:#006080;">"name"</span>).First().Value,
                address = x.Descendants(<span style="color:#006080;">"address"</span>).First().Value,
            }).ToArray();
    <span style="color:#0000ff;">foreach</span> (User user <span style="color:#0000ff;">in</span> usersList)
        userListItems.Add(user);
}</pre>
</div>
<p>&nbsp;</p>
<p>So when the service completes all the users will be filled in the ObserveableCollection. Now to bind it to the userList ListBox. In the constructor of the Page class write:</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;">userList.DataContext = userListItems;</pre>
</div>
<p>&nbsp;</p>
<p>And modify the code for the userList ListBox to look like:</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">&lt;</span><span style="color:#800000;">ListBox</span> <span style="color:#ff0000;">x:Name</span><span style="color:#0000ff;">="userList"</span> <span style="color:#ff0000;">Height</span><span style="color:#0000ff;">="200"</span> <span style="color:#ff0000;">ItemsSource</span><span style="color:#0000ff;">="{Binding}"</span> <span style="color:#ff0000;">DisplayMemberPath</span><span style="color:#0000ff;">="name"</span> <span style="color:#0000ff;">/&gt;</span></pre>
</div>
<p>&nbsp;</p>
<p>Add a crossdomain.xml file to the rails project&#8217;s public folder:</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">&lt;?</span><span style="color:#800000;">xml</span> <span style="color:#ff0000;">version</span><span style="color:#0000ff;">="1.0"</span>?<span style="color:#0000ff;">&gt;</span>
<span style="color:#0000ff;">&lt;!</span><span style="color:#800000;">DOCTYPE</span> <span style="color:#ff0000;">cross-domain-policy</span> <span style="color:#ff0000;">SYSTEM</span> <span style="color:#0000ff;">"http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"</span><span style="color:#0000ff;">&gt;</span>
<span style="color:#0000ff;">&lt;</span><span style="color:#800000;">cross-domain-policy</span><span style="color:#0000ff;">&gt;</span>
  <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">allow-access-from</span> <span style="color:#ff0000;">domain</span><span style="color:#0000ff;">="*"</span> <span style="color:#0000ff;">/&gt;</span>
<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">cross-domain-policy</span><span style="color:#0000ff;">&gt;</span></pre>
</div>
<p>We are basically allowing our Silverlight app to access the rails server. You won&#8217;t be requiring this file if the Rails server is serving the Silverlight application, but currently it is not the case.</p>
<p>Now when you run the code you should see:</p>
<p><a href="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/03/vs-step3.png"><img style="border-width:0;" height="416" alt="vs_step3" src="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/03/vs-step3-thumb.png" width="480" border="0"></a></p>
<p>Now I will add a few more lines of code to show the selected item in TextBox. Add/Update the following to page.xaml:</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">&lt;</span><span style="color:#800000;">ListBox</span> <span style="color:#ff0000;">x:Name</span><span style="color:#0000ff;">="userList"</span> <span style="color:#ff0000;">Height</span><span style="color:#0000ff;">="200"</span> <span style="color:#ff0000;">ItemsSource</span><span style="color:#0000ff;">="{Binding}"</span> <span style="color:#ff0000;">DisplayMemberPath</span><span style="color:#0000ff;">="name"</span> <span style="color:#ff0000;">SelectionChanged</span><span style="color:#0000ff;">="userList_SelectionChanged"</span><span style="color:#0000ff;">/&gt;</span>
<span style="color:#0000ff;">&lt;</span><span style="color:#800000;">StackPanel</span> <span style="color:#ff0000;">x:Name</span><span style="color:#0000ff;">="controls"</span><span style="color:#0000ff;">&gt;</span>
    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">TextBox</span> <span style="color:#ff0000;">Text</span><span style="color:#0000ff;">="{Binding name, Mode=Twoway}"</span> <span style="color:#ff0000;">x:Name</span><span style="color:#0000ff;">="userName"</span><span style="color:#0000ff;">/&gt;</span>
    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">TextBox</span> <span style="color:#ff0000;">Text</span><span style="color:#0000ff;">="{Binding address, Mode=Twoway}"</span> <span style="color:#ff0000;">x:Name</span><span style="color:#0000ff;">="userAddress"</span><span style="color:#0000ff;">/&gt;</span>
<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">StackPanel</span><span style="color:#0000ff;">&gt;</span></pre>
</div>
<p>The first TextBox is showing name and second is showing the password. The binding mode is set to TwoWay so that the changes in TextBox are automatically to the userListItems.</p>
<p>Add the corresponding event handler to Page class:</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> userList_SelectionChanged(<span style="color:#0000ff;">object</span> sender, SelectionChangedEventArgs e) {
    controls.DataContext = userList.SelectedItem <span style="color:#0000ff;">as</span> User;
}</pre>
</div>
<p>&nbsp;</p>
<p>Now when you run the code you should see:</p>
<p><a href="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/03/image6.png"><img style="border-width:0;" height="423" alt="image" src="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/03/image-thumb6.png" width="481" border="0"></a></p>
<p>Download the code upto this point:</p>
<ul>
<li><a href="http://cid-49208a72e4eb818c.skydrive.live.com/self.aspx/world/code/test.rar">Rails code</a>
<li><a href="http://cid-49208a72e4eb818c.skydrive.live.com/self.aspx/world/code/TestFrontEnd.rar">Silverlight code</a> </li>
</ul>
<p>&nbsp;</p>
<h5>&nbsp;<u>Implementing the full <a href="http://en.wikipedia.org/wiki/Create,_read,_update_and_delete">CRUD</a> :</u></h5>
<p>&nbsp;</p>
<p>Firstly I willl be doing a bit of refactoring. Move the xml parsing code using Linq to the User class. So it should now look like:</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> User {
    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">int</span> id { get; set; }
    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">string</span> name { get; set; }
    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">string</span> address { get; set; }

    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> User[] users_from_xml(String xml_string){
        XDocument users = XDocument.Parse(xml_string);
        <span style="color:#0000ff;">return</span> (from x <span style="color:#0000ff;">in</span> users.Descendants(<span style="color:#006080;">"user"</span>)
            select <span style="color:#0000ff;">new</span> User
            {
                id = <span style="color:#0000ff;">int</span>.Parse(x.Descendants(<span style="color:#006080;">"id"</span>).First().Value),
                name = x.Descendants(<span style="color:#006080;">"name"</span>).First().Value,
                address = x.Descendants(<span style="color:#006080;">"address"</span>).First().Value,
            }).ToArray();
    }

    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> User user_from_xml(String xml_string) {
        <span style="color:#0000ff;">return</span> users_from_xml(xml_string).First();
    }
}</pre>
</div>
<p>&nbsp;</p>
<p>Change the Page class to:</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">using</span> System;
<span style="color:#0000ff;">using</span> System.Collections.Generic;
<span style="color:#0000ff;">using</span> System.Linq;
<span style="color:#0000ff;">using</span> System.Windows;
<span style="color:#0000ff;">using</span> System.Windows.Controls;
<span style="color:#0000ff;">using</span> System.Windows.Documents;
<span style="color:#0000ff;">using</span> System.Windows.Input;
<span style="color:#0000ff;">using</span> System.Windows.Media;
<span style="color:#0000ff;">using</span> System.Windows.Media.Animation;
<span style="color:#0000ff;">using</span> System.Windows.Shapes;
<span style="color:#0000ff;">using</span> System.Collections.ObjectModel;
<span style="color:#0000ff;">using</span> System.Xml.Linq;

<span style="color:#0000ff;">namespace</span> TestFrontEnd {
    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">partial</span> <span style="color:#0000ff;">class</span> Page : UserControl {
        <span style="color:#0000ff;">private</span> ObservableCollection&lt;User&gt; userListItems = <span style="color:#0000ff;">new</span> ObservableCollection&lt;User&gt;();

        <span style="color:#0000ff;">private</span> HTTPService userIndexService;
        <span style="color:#0000ff;">private</span> HTTPService userCreateService;
        <span style="color:#0000ff;">private</span> HTTPService userUpdateService;
        <span style="color:#0000ff;">private</span> HTTPService userDeleteService;

        <span style="color:#0000ff;">public</span> Page() {
            InitializeComponent();
            <span style="color:#008000;">// setting the databinding</span>
            userList.DataContext = userListItems;
            controls.DataContext = <span style="color:#0000ff;">new</span> User();

            <span style="color:#008000;">// service to get all te users form the server</span>
            userIndexService = <span style="color:#0000ff;">new</span> HTTPService();
            userIndexService.Url = <span style="color:#006080;">"http://localhost:3000/users.xml"</span>;
            userIndexService.ServiceComplete += <span style="color:#0000ff;">new</span> HttpServiceCompleteEventHandler(userIndexService_ServiceComplete);
            userIndexService.send();

            <span style="color:#008000;">// create a new user</span>
            userCreateService = <span style="color:#0000ff;">new</span> HTTPService();
            userCreateService.Url = <span style="color:#006080;">"http://localhost:3000/users.xml"</span>;
            userCreateService.Method = <span style="color:#006080;">"POST"</span>;
            userCreateService.ServiceComplete += <span style="color:#0000ff;">new</span> HttpServiceCompleteEventHandler(userCreateService_ServiceComplete);

            <span style="color:#008000;">// update a user, we will not be trapping the results</span>
            userUpdateService = <span style="color:#0000ff;">new</span> HTTPService();
            userUpdateService.Method = <span style="color:#006080;">"POST"</span>;

            <span style="color:#008000;">// delete a user</span>
            userDeleteService = <span style="color:#0000ff;">new</span> HTTPService();
            userDeleteService.Method = <span style="color:#006080;">"POST"</span>;
        }

        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> _userList_SelectionChanged(<span style="color:#0000ff;">object</span> sender, SelectionChangedEventArgs e) {
            controls.DataContext = userList.SelectedItem <span style="color:#0000ff;">as</span> User;
        }

        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> userIndexService_ServiceComplete(HttpServiceCompleteEventArgs e) {
            <span style="color:#0000ff;">foreach</span> (User user <span style="color:#0000ff;">in</span> User.users_from_xml(e.Response))
                userListItems.Add(user);
        }
        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> userCreateService_ServiceComplete(HttpServiceCompleteEventArgs e) {
            userListItems.Add(User.user_from_xml(e.Response));
        }

        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> createButton_Click(<span style="color:#0000ff;">object</span> sender, RoutedEventArgs e) {
            userCreateService.send(String.Format(<span style="color:#006080;">"{0}={1}&amp;{2}={3}"</span>, <span style="color:#006080;">"user[name]"</span>, userName.Text, <span style="color:#006080;">"user[address]"</span>, userAddress.Text));
        }

        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> updateButton_Click(<span style="color:#0000ff;">object</span> sender, RoutedEventArgs e) {
            User _selectedUser = userList.SelectedItem <span style="color:#0000ff;">as</span> User;
            userUpdateService.Url = <span style="color:#006080;">"http://localhost:3000/users/"</span> + _selectedUser.id.ToString() + <span style="color:#006080;">".xml"</span>;
            userUpdateService.send(String.Format(<span style="color:#006080;">"{0}={1}&amp;{2}={3}&amp;{4}={5}"</span>, <span style="color:#006080;">"user[name]"</span>, userName.Text, <span style="color:#006080;">"user[address]"</span>, userAddress.Text, <span style="color:#006080;">"_method"</span>, <span style="color:#006080;">"PUT"</span>));
            userListItems[userList.SelectedIndex] = _selectedUser;
        }

        <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> deleteButton_Click(<span style="color:#0000ff;">object</span> sender, RoutedEventArgs e) {
            User _selectedUser = userList.SelectedItem <span style="color:#0000ff;">as</span> User;
            userDeleteService.Url = <span style="color:#006080;">"http://localhost:3000/users/"</span> + _selectedUser.id.ToString() + <span style="color:#006080;">".xml"</span>;
            userDeleteService.send(String.Format(<span style="color:#006080;">"{0}={1}"</span>, <span style="color:#006080;">"_method"</span>, <span style="color:#006080;">"DELETE"</span>));
            userListItems.Remove(_selectedUser);
        }

    }
}
</pre>
</div>
<p>&nbsp;</p>
<p>To understand the specific parameters to the &#8220;send()&#8221; method of the HTTPService class you will need a little bit of understanding of how REST is implemented in Rails and how parameters are passed to a url, which is out of scope of this blog post.</p>
<p>&nbsp;</p>
<p>Change Page.xaml to:</p>
<div>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#0000ff;">&lt;</span><span style="color:#800000;">UserControl</span> <span style="color:#ff0000;">x:Class</span><span style="color:#0000ff;">="TestFrontEnd.Page"</span>
    <span style="color:#ff0000;">xmlns</span><span style="color:#0000ff;">="http://schemas.microsoft.com/client/2007"</span>
    <span style="color:#ff0000;">xmlns:x</span><span style="color:#0000ff;">="http://schemas.microsoft.com/winfx/2006/xaml"</span>
    <span style="color:#ff0000;">Width</span><span style="color:#0000ff;">="400"</span> <span style="color:#ff0000;">Height</span><span style="color:#0000ff;">="300"</span><span style="color:#0000ff;">&gt;</span>
    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Grid</span> <span style="color:#ff0000;">x:Name</span><span style="color:#0000ff;">="LayoutRoot"</span> <span style="color:#ff0000;">Background</span><span style="color:#0000ff;">="White"</span><span style="color:#0000ff;">&gt;</span>
        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">StackPanel</span> <span style="color:#ff0000;">Orientation</span><span style="color:#0000ff;">="Vertical"</span><span style="color:#0000ff;">&gt;</span>
            <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">ListBox</span> <span style="color:#ff0000;">x:Name</span><span style="color:#0000ff;">="userList"</span> <span style="color:#ff0000;">ItemsSource</span><span style="color:#0000ff;">="{Binding}"</span> <span style="color:#ff0000;">DisplayMemberPath</span><span style="color:#0000ff;">="name"</span> <span style="color:#ff0000;">SelectionChanged</span><span style="color:#0000ff;">="_userList_SelectionChanged"</span> <span style="color:#ff0000;">Height</span><span style="color:#0000ff;">="200"</span><span style="color:#0000ff;">/&gt;</span>
            <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">StackPanel</span> <span style="color:#ff0000;">x:Name</span><span style="color:#0000ff;">="controls"</span><span style="color:#0000ff;">&gt;</span>
                <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">TextBox</span> <span style="color:#ff0000;">Text</span><span style="color:#0000ff;">="{Binding name, Mode=Twoway}"</span> <span style="color:#ff0000;">x:Name</span><span style="color:#0000ff;">="userName"</span><span style="color:#0000ff;">/&gt;</span>
                <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">TextBox</span> <span style="color:#ff0000;">Text</span><span style="color:#0000ff;">="{Binding address, Mode=Twoway}"</span> <span style="color:#ff0000;">x:Name</span><span style="color:#0000ff;">="userAddress"</span><span style="color:#0000ff;">/&gt;</span>
            <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">StackPanel</span><span style="color:#0000ff;">&gt;</span>
            <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">StackPanel</span> <span style="color:#ff0000;">Orientation</span><span style="color:#0000ff;">="Horizontal"</span><span style="color:#0000ff;">&gt;</span>
                <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Button</span> <span style="color:#ff0000;">Content</span><span style="color:#0000ff;">="Create"</span> <span style="color:#ff0000;">x:Name</span><span style="color:#0000ff;">="createButton"</span> <span style="color:#ff0000;">Click</span><span style="color:#0000ff;">="createButton_Click"</span><span style="color:#0000ff;">/&gt;</span>
                <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Button</span> <span style="color:#ff0000;">Content</span><span style="color:#0000ff;">="Update"</span> <span style="color:#ff0000;">x:Name</span><span style="color:#0000ff;">="updateButton"</span> <span style="color:#ff0000;">Click</span><span style="color:#0000ff;">="updateButton_Click"</span><span style="color:#0000ff;">/&gt;</span>
                <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Button</span> <span style="color:#ff0000;">Content</span><span style="color:#0000ff;">="Delete"</span> <span style="color:#ff0000;">x:Name</span><span style="color:#0000ff;">="deleteButton"</span> <span style="color:#ff0000;">Click</span><span style="color:#0000ff;">="deleteButton_Click"</span><span style="color:#0000ff;">/&gt;</span>
            <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">StackPanel</span><span style="color:#0000ff;">&gt;</span>
            <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">TextBlock</span> <span style="color:#ff0000;">x:Name</span><span style="color:#0000ff;">="trace"</span> <span style="color:#0000ff;">/&gt;</span>
        <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">StackPanel</span><span style="color:#0000ff;">&gt;</span>
    <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Grid</span><span style="color:#0000ff;">&gt;</span>
<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">UserControl</span><span style="color:#0000ff;">&gt;</span>
</pre>
</div>
<p>&nbsp;</p>
<p>This is how it looks like now:</p>
<p><a href="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/03/image7.png"><img style="border-width:0;" height="423" alt="image" src="http://allyourcodearebelongto.me/blog/wp-content/uploads/2008/03/image-thumb7.png" width="481" border="0"></a></p>
<p>So this is full CRUD implementation using Silverlight as frontend.</p>
<p>This is not the most optimal way to implement CRUD it is very easy to understand. It also should be noted that I haven&#8217;t added error checking of any type in the code and there is a lot of code repetition. But I guess that could be improved upon further once the most simple scenario is implemented. My style of coding is &#8220;First make it work and then optimize it&#8221;, and as I have mentioned it before it is certainly not the most optimized way to code but it sure works :)</p>
<p>One microsoft releases better databinding capabilities in controls and some simpler classes for HTTP based communication, this code would be much more simpler.</p>
<p>Full code for this aplication can be downloaded from:</p>
<li><a href="http://cid-49208a72e4eb818c.skydrive.live.com/self.aspx/world/code/test.rar">Rails code</a>
<li><a href="http://cid-49208a72e4eb818c.skydrive.live.com/self.aspx/world/code/TestFrontEnd2.rar">Silverlight code</a></li>
<p>&nbsp;</p>
<div class="wlWriterSmartContent" id="5ce5ff6c-9474-4531-909c-9f79bda9013e" style="display:inline;margin:0;padding:0;">del.icio.us Tags: <a href="http://del.icio.us/popular/adobe" rel="tag">adobe</a>,<a href="http://del.icio.us/popular/C#" rel="tag">C#</a>,<a href="http://del.icio.us/popular/crud" rel="tag">crud</a>,<a href="http://del.icio.us/popular/flex" rel="tag">flex</a>,<a href="http://del.icio.us/popular/microsoft" rel="tag">microsoft</a>,<a href="http://del.icio.us/popular/rails" rel="tag">rails</a>,<a href="http://del.icio.us/popular/ruby%20on%20rails" rel="tag">ruby on rails</a>,<a href="http://del.icio.us/popular/silverlight" rel="tag">silverlight</a>,<a href="http://del.icio.us/popular/xaml" rel="tag">xaml</a></div>
]]></content:encoded>
			<wfw:commentRss>http://allyourcodearebelongto.me/blog/2008/03/26/using-silverlight-with-rubyonrails/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Sudo for Vista</title>
		<link>http://allyourcodearebelongto.me/blog/2007/11/28/sudo-for-vista/</link>
		<comments>http://allyourcodearebelongto.me/blog/2007/11/28/sudo-for-vista/#comments</comments>
		<pubDate>Wed, 28 Nov 2007 05:16:40 +0000</pubDate>
		<dc:creator>Gaurav</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[vista]]></category>

		<guid isPermaLink="false">http://techblogging.wordpress.com/2007/11/28/sudo-for-vista/</guid>
		<description><![CDATA[The following ruby script when given any executable file path as the argument runs it in administrative mode.


   1: require 'Win32API'
   2:&#160; 
   3: def shell_execute(process_name)
   4:     process = ''
   5:     process.replace(process_name)
   6:   [...]]]></description>
			<content:encoded><![CDATA[<p>The following ruby script when given any executable file path as the argument runs it in administrative mode.</p>
<div>
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> require <span style="color:#008000;">'Win32API'</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> def shell_execute(process_name)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     process = <span style="color:#008000;">''</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     process.replace(process_name)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>     se = Win32API.<span style="color:#0000ff;">new</span>(<span style="color:#006080;">"shell32"</span>, <span style="color:#006080;">"ShellExecute"</span>, [<span style="color:#008000;">'P','P','P','P','P','I'], 'I')</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>     se.<span style="color:#0000ff;">Call</span>(nil,<span style="color:#006080;">"runas"</span>,process,nil,nil,5)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span> <span style="color:#0000ff;">end</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span> shell_execute(ARGV[0])</pre>
</div>
</div>
<p>&nbsp;</p>
<p>In Vista you can do the same by right clicking on the file and selecting &#8220;Run as Administrator&#8221; but this script allows you to run a process in admin mode using the command line.</p>
<p>The ShellExecute function resides in the shell32.dll and is documented <a href="http://msdn2.microsoft.com/en-us/library/ms647732.aspx">here</a>.</p>
<p>By just giving &#8220;runas&#8221; as the second parameter to the function the process is executed in the administrative mode.</p>
]]></content:encoded>
			<wfw:commentRss>http://allyourcodearebelongto.me/blog/2007/11/28/sudo-for-vista/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Periodically changing desktop wallpaper using Ruby</title>
		<link>http://allyourcodearebelongto.me/blog/2007/11/27/changing-desktop-wallpaper-using-ruby/</link>
		<comments>http://allyourcodearebelongto.me/blog/2007/11/27/changing-desktop-wallpaper-using-ruby/#comments</comments>
		<pubDate>Tue, 27 Nov 2007 16:46:15 +0000</pubDate>
		<dc:creator>Gaurav</dc:creator>
				<category><![CDATA[XP]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[vista]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://techblogging.wordpress.com/2007/11/27/changing-desktop-wallpaper-using-ruby/</guid>
		<description><![CDATA[Here is small ruby script that picks up a random image from a given folder and sets it as the desktop wallpaper.


   1: require 'Win32API'
   2:&#160; 
   3: SPI_SETDESKWALLPAPER = 20
   4: SPIF_SENDCHANGE = 0x2
   5: SOURCE_FOLDER = "C:\\Users\\Gaurav\\Pictures\\Best\\"
   6:&#160; 
   [...]]]></description>
			<content:encoded><![CDATA[<p>Here is small ruby script that picks up a random image from a given folder and sets it as the desktop wallpaper.</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> require <span style="color:#008000;">'Win32API'</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> SPI_SETDESKWALLPAPER = 20</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> SPIF_SENDCHANGE = 0x2</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> SOURCE_FOLDER = <span style="color:#006080;">"C:\\Users\\Gaurav\\Pictures\\Best\\"</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> files = Dir.entries(SOURCE_FOLDER) - [<span style="color:#006080;">"."</span>, <span style="color:#006080;">".."</span>]</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span> file = files[rand(files.length)]</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span> systemParametersInfo = Win32API.<span style="color:#0000ff;">new</span>(<span style="color:#006080;">"user32"</span>,<span style="color:#006080;">"SystemParametersInfo"</span>,[<span style="color:#006080;">"I"</span>, <span style="color:#006080;">"I"</span>, <span style="color:#006080;">"P"</span>, <span style="color:#006080;">"I"</span>],<span style="color:#006080;">"I"</span>)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span> p systemParametersInfo.<span style="color:#0000ff;">call</span>(SPI_SETDESKWALLPAPER, 1, SOURCE_FOLDER + file, SPIF_SENDCHANGE)</pre>
</div>
</div>
<div>&nbsp;</div>
<p>The constant SOURCE_FOLDER can be changed to point at the folder which contain all your wallpaper and the script then randomly chooses a wallpaper from the collection.</p>
<p>You can also set your task scheduler to run this script automatically after a fixed interval so that your desktop wallpaper keeps changing.</p>
<p>Here is the <a href="http://pastie.caboo.se/182290">pastie</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://allyourcodearebelongto.me/blog/2007/11/27/changing-desktop-wallpaper-using-ruby/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Renaming MP3 files based on ID3 tag information</title>
		<link>http://allyourcodearebelongto.me/blog/2007/11/27/renaming-mp3-files-with-id3-tag-title-using-ruby/</link>
		<comments>http://allyourcodearebelongto.me/blog/2007/11/27/renaming-mp3-files-with-id3-tag-title-using-ruby/#comments</comments>
		<pubDate>Tue, 27 Nov 2007 16:35:59 +0000</pubDate>
		<dc:creator>Gaurav</dc:creator>
				<category><![CDATA[ID3]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://techblogging.wordpress.com/2007/11/27/renaming-mp3-files-with-id3-tag-title-using-ruby/</guid>
		<description><![CDATA[Here is a neat little trick that I often find useful for renaming mp3 files with wrong names and correct tag titles. Just run this ruby script in the directory where you have stored your songs and it will read the tag title and rename the current file using it.


   1: require "rubygems"
 [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a neat little trick that I often find useful for renaming mp3 files with wrong names and correct tag titles. Just run this ruby script in the directory where you have stored your songs and it will read the tag title and rename the current file using it.</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> require <span style="color:#006080;">"rubygems"</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> require <span style="color:#006080;">"id3lib"</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> require <span style="color:#006080;">"fileutils"</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> files = `dir /s /b *.mp3`</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> files.split(<span style="color:#006080;">"\n"</span>).<span style="color:#0000ff;">each</span> <span style="color:#0000ff;">do</span> |file|</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>   dir = File.dirname(file)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>   tag = ID3Lib::Tag.<span style="color:#0000ff;">new</span>(file)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>   new_name = tag.title.gsub(<span style="color:#006080;">"00"</span>,<span style="color:#008000;">'') if tag.title</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>   FileUtils.move(file, File.join(dir, new_name + <span style="color:#006080;">".mp3"</span>)) <span style="color:#0000ff;">if</span> new_name <span style="color:#0000ff;">and</span> !new_name.empty? rescue nil</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span> end</pre>
</div>
</div>
<p>This script requires id3lib gem for ruby which can be installed simply by running the command:</p>
<p>gem install id3lib-ruby</p>
<p>One thing to notice in the script is that currently it works only on windows, but only with a little modification it can be made to work on other OSes as well (The only change I guess will be in the 5th line).</p>
<p><a href="http://pastie.caboo.se/173944">Here</a> is the pastie link for this code.</p>
<div class="wlWriterSmartContent" id="f0b1a552-8a56-4ff5-a150-53d6990b5aa8" style="display:inline;margin:0;padding:0;">del.icio.us Tags: <a href="http://del.icio.us/popular/id3" rel="tag">id3</a>,<a href="http://del.icio.us/popular/mp3" rel="tag">mp3</a>,<a href="http://del.icio.us/popular/winamp" rel="tag">winamp</a></div>
]]></content:encoded>
			<wfw:commentRss>http://allyourcodearebelongto.me/blog/2007/11/27/renaming-mp3-files-with-id3-tag-title-using-ruby/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Creating a User Interface in Ruby using WPF</title>
		<link>http://allyourcodearebelongto.me/blog/2007/10/09/creating-a-user-interface-in-ruby-using-wpf/</link>
		<comments>http://allyourcodearebelongto.me/blog/2007/10/09/creating-a-user-interface-in-ruby-using-wpf/#comments</comments>
		<pubDate>Tue, 09 Oct 2007 11:41:23 +0000</pubDate>
		<dc:creator>Gaurav</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[user interface]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[wpf]]></category>
		<category><![CDATA[xaml]]></category>
		<category><![CDATA[ui]]></category>
		<category><![CDATA[win32ole]]></category>

		<guid isPermaLink="false">http://techblogging.wordpress.com/2007/10/09/creating-a-user-interface-in-ruby-using-wpf/</guid>
		<description><![CDATA[Let me first explain what is WPF?
WPF is the graphical subsystem if the .NET 3.0 framework and it is there to take the place of window forms which we used with earlier versions of .NET
So what&#8217;s so great about WPF?
Actually WPF is has its roots in DirectX API so you can easily create 2D and [...]]]></description>
			<content:encoded><![CDATA[<p>Let me first explain what is WPF?</p>
<p>WPF is the graphical subsystem if the .NET 3.0 framework and it is there to take the place of window forms which we used with earlier versions of .NET</p>
<h5>So what&#8217;s so great about WPF?</h5>
<p>Actually WPF is has its roots in DirectX API so you can easily create 2D and 3D interfaces in it without putting ant load on the processor. Also it is vector based and stores the information for the generating the UI in separate XAML files. So we have logical separation of a control from its appearance.</p>
<p>Further details: <a title="http://en.wikipedia.org/wiki/Windows_Presentation_Foundation" href="http://en.wikipedia.org/wiki/Windows_Presentation_Foundation" target="_blank">http://en.wikipedia.org/wiki/Windows_Presentation_Foundation</a></p>
<p>The following are the requirements for running this example:</p>
<ul>
<li>rubyclr gem. Use <strong>gem install rubyclr -y</strong>
<li>.NET framework 3.0 </li>
</ul>
<p>In this example I will creating a creating a textarea along with a button, trapping the events on the button and logging it inside the textarea .</p>
<p>Here is the code for the xaml file. Store this file with filename ui.xaml. This file contains the UI for the application.</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Window</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#ff0000;">xmlns</span><span style="color:#0000ff;">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> <span style="color:#ff0000;">xmlns:x</span><span style="color:#0000ff;">="http://schemas.microsoft.com/winfx/2006/xaml"</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Grid</span> <span style="color:#ff0000;">x:Name</span><span style="color:#0000ff;">="LayoutRoot"</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Button</span> <span style="color:#ff0000;">Margin</span><span style="color:#0000ff;">="8,0,8,5.723"</span> <span style="color:#ff0000;">VerticalAlignment</span><span style="color:#0000ff;">="Bottom"</span> <span style="color:#ff0000;">Content</span><span style="color:#0000ff;">="Button"</span> <span style="color:#ff0000;">Name</span><span style="color:#0000ff;">="the_button"</span><span style="color:#0000ff;">/&gt;</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">TextBox</span> <span style="color:#ff0000;">Margin</span><span style="color:#0000ff;">="8,8,8,38"</span> <span style="color:#ff0000;">Name</span><span style="color:#0000ff;">="the_text_box"</span><span style="color:#0000ff;">/&gt;</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Grid</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span> <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Window</span><span style="color:#0000ff;">&gt;</span></pre>
</div>
</div>
<div>&nbsp;</div>
<p>Next I will be creating the codebehind file in ruby. Here in this file we have all the application logic. Save this file as logic.rb.</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> # load the libraries</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> require <span style="color:#008000;">'rubygems'</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> require <span style="color:#008000;">'wpf'</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> # load the xaml file</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> window = XamlReader.Load(System::IO::File.open_read(<span style="color:#008000;">'ui.xaml'))</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> # <span style="color:#0000ff;">get</span> the controls</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span> button = window.find_name(<span style="color:#008000;">'the_button')</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span> txt_box = window.find_name(<span style="color:#008000;">'the_text_box')</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span> # trap the mouse enter <span style="color:#0000ff;">event</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span> button.mouse_enter <span style="color:#0000ff;">do</span> |sender, args|</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span> txt_box.text += <span style="color:#006080;">"MOUSE ENTERED\n"</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span> <span style="color:#0000ff;">end</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span> # trap the mouse leave <span style="color:#0000ff;">event</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span> button.mouse_leave <span style="color:#0000ff;">do</span> |sender, args|</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span> txt_box.text += <span style="color:#006080;">"MOUSE LEFT\n"</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span> <span style="color:#0000ff;">end</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  18:</span> # trap the mouse click <span style="color:#0000ff;">event</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  19:</span> button.click <span style="color:#0000ff;">do</span> |sender, args|</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  20:</span> txt_box.text += <span style="color:#006080;">"MOUSE CLICKED\n"</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  21:</span> <span style="color:#0000ff;">end</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  22:</span> # run the application (most important)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  23:</span> Application.<span style="color:#0000ff;">new</span>.run(window)</pre>
</div>
</div>
<p>The code given above explains itself.</p>
<p>We are first loading the ui.xaml file and then finding the two controls in the lines 8, 9.</p>
<p>Then the three event handlers are declared in lines 11, 14, 19. So whenever the mouse events are fired the corresponding event if traced into the textbox.</p>
<h2>And here is the output.</h2>
<p><a href="http://allyourcodearebelongto.me/blog/wp-content/uploads/2007/10/image.png"><img style="border-width:0;" height="349" alt="image" src="http://allyourcodearebelongto.me/blog/wp-content/uploads/2007/10/image-thumb.png" width="540" border="0"></a></p>
<div class="wlWriterSmartContent" id="38ca0925-19ba-445d-831b-fb8a30569fe5" style="display:inline;float:none;margin:0;padding:0;">del.icio.us Tags: <a href="http://del.icio.us/popular/ruby" rel="tag">ruby</a>,<a href="http://del.icio.us/popular/user%20interface" rel="tag">user interface</a>,<a href="http://del.icio.us/popular/windows" rel="tag">windows</a>,<a href="http://del.icio.us/popular/wpf" rel="tag">wpf</a>,<a href="http://del.icio.us/popular/xaml" rel="tag">xaml</a></div>
]]></content:encoded>
			<wfw:commentRss>http://allyourcodearebelongto.me/blog/2007/10/09/creating-a-user-interface-in-ruby-using-wpf/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Automating Powerpoint with ruby</title>
		<link>http://allyourcodearebelongto.me/blog/2007/10/09/automating-powerpoint-with-ruby/</link>
		<comments>http://allyourcodearebelongto.me/blog/2007/10/09/automating-powerpoint-with-ruby/#comments</comments>
		<pubDate>Tue, 09 Oct 2007 10:50:27 +0000</pubDate>
		<dc:creator>Gaurav</dc:creator>
				<category><![CDATA[powerpoint]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[win32ole]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[automation]]></category>

		<guid isPermaLink="false">http://techblogging.wordpress.com/2007/10/09/automating-powerpoint-with-ruby/</guid>
		<description><![CDATA[We know that ruby is a language of few words. We can express a lot of things in a relatively easy manner.
I was trying to export few slides in my powerpoint presentation to an image format using ruby.I could not find any documentation for it anywhere except for a few basic things. But when I [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">We know that ruby is a language of few words. We can express a lot of things in a relatively easy manner.</p>
<p align="justify">I was trying to export few slides in my powerpoint presentation to an image format using ruby.I could not find any documentation for it anywhere except for a few basic things. But when I tried it by hit and trial I found out it to be surprisingly easy. It required just a few lines.</p>
<p>Here is what you have to do:</p>
<div>
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> require ‘win32ole’</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> # open powerpoint</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> ppt = WIN32OLE.<span style="color:#0000ff;">new</span>(‘Powerpoint.Application’)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> # make sure it <span style="color:#0000ff;">is</span> visible</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> ppt.Visible = <span style="color:#0000ff;">true</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span> # open the presentation <span style="color:#0000ff;">to</span> be exported</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> pre = ppt.Presentations.Open(“d:\\imp_file.pptx”)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span> # export the file</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span> pre.Slides(1).Export(“d:\\exp_file.png”,“png”)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span> # close powerpoint, will close all the currently open files</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span> ppt.Quit()</pre>
</div>
</div>
<p>You can always use RMagick to further process this image.</p>
<p>The <tt>Win32OLE</tt> extension library (actually spelled in lowercase, <tt>win32ole</tt>) provides an interface to Windows OLE automation. Your Ruby code can act as a client for any OLE automation server such as Microsoft Word, Outlook, and Internet Explorer and many other softwares.</p>
<p>So in the above code we are making a object is Powerpoint application, then opening a file and saving the first slide in the &#8220;png&#8221; format.</p>
]]></content:encoded>
			<wfw:commentRss>http://allyourcodearebelongto.me/blog/2007/10/09/automating-powerpoint-with-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
