<?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>Tamir Khason - Just code &#187; WPF/E</title>
	<atom:link href="http://khason.net/tag/wpfe/feed/" rel="self" type="application/rss+xml" />
	<link>http://khason.net</link>
	<description>Take care of the sense, and the sounds will take care of themselves.</description>
	<lastBuildDate>Thu, 15 Apr 2010 13:25:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Quick Silverlight tip: Why my ActualWidth and ActualHeight equal 0?</title>
		<link>http://khason.net/blog/quick-silverlight-tip-why-my-actualwidth-and-actualheight-equal-0/</link>
		<comments>http://khason.net/blog/quick-silverlight-tip-why-my-actualwidth-and-actualheight-equal-0/#comments</comments>
		<pubDate>Sat, 12 Jul 2008 11:21:29 +0000</pubDate>
		<dc:creator>Tamir</dc:creator>
				<category><![CDATA[BLOG]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[WPF/E]]></category>

		<guid isPermaLink="false">http://khason.net/blog/quick-silverlight-tip-why-my-actualwidth-and-actualheight-equal-0/</guid>
		<description><![CDATA[If you’ll try to use ActualWidth and ActualHight of controls with explicitly set Width and Height, you’ll not see any problem, however, if your control sits inside other control, ActualWidth and ActualHeight properties will be equal to 0. Why this and how to fix it? Actually, measurement and layout pass in Silverlight run asynchronously, thus [...]


Related posts:<ol><li><a href='http://khason.net/itpro/quick-it-tip-how-to-build-bootable-usb-stick/' rel='bookmark' title='Permanent Link: Quick IT tip: How to build bootable USB stick'>Quick IT tip: How to build bootable USB stick</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>If you’ll try to use ActualWidth and ActualHight of controls <u>with</u> explicitly set Width and Height, you’ll not see any problem, however, if your control sits inside other control, ActualWidth and ActualHeight properties will be equal to 0. Why this and how to fix it?</p>
<p>Actually, measurement and layout pass in Silverlight run asynchronously, thus it executed with or, even after, your code. So in order to fix it, you <a href="http://silverlight.net/blogs/msnow/archive/2008/06/30/tip-of-the-day-13-getting-an-images-width-and-height.aspx">should measure</a> ActualWidth and Actual Height asynchronously. So, instead of:</p>
<blockquote><p>//get and use ActualWidth/ActualHeight   </p></blockquote>
<p>Use:</p>
<blockquote><p>Dispatcher.BeginInvoke(delegate     <br /> {      <br />&#160; //get and use ActualWidth/ActualHeight      <br /> });</p>
</blockquote>
<p>It will allows you to detect actual size of your control.</p>
<p>Have a nice day and be good people.</p>


<p>Related posts:<ol><li><a href='http://khason.net/itpro/quick-it-tip-how-to-build-bootable-usb-stick/' rel='bookmark' title='Permanent Link: Quick IT tip: How to build bootable USB stick'>Quick IT tip: How to build bootable USB stick</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://khason.net/blog/quick-silverlight-tip-why-my-actualwidth-and-actualheight-equal-0/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to handle thickness</title>
		<link>http://khason.net/blog/how-to-handle-thickness/</link>
		<comments>http://khason.net/blog/how-to-handle-thickness/#comments</comments>
		<pubDate>Tue, 08 Jul 2008 22:07:42 +0000</pubDate>
		<dc:creator>Tamir</dc:creator>
				<category><![CDATA[BLOG]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[source]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Work process]]></category>
		<category><![CDATA[WPF/E]]></category>

		<guid isPermaLink="false">http://khason.net/blog/how-to-handle-thickness/</guid>
		<description><![CDATA[Yesterday, we spoke about type converters. We even, built simple generic enum converter. Today, we’ll create more complicated converter, that very missing in Silverlight – ThicknessConverter. During the post, I also explain about tokenizing values in Silverlight What is Thickness ? What is thickness in Silverlight? It’s &#60;Border BorderThickness=”6”/&#62; or &#60;Border BorderThickness=”6,4,3,2”/&#62; or &#60;Border BorderThickness=”6, [...]


Related posts:<ol><li><a href='http://khason.net/dev/inotifypropertychanged-auto-wiring-or-how-to-get-rid-of-redundant-code/' rel='bookmark' title='Permanent Link: INotifyPropertyChanged auto wiring or how to get rid of redundant code'>INotifyPropertyChanged auto wiring or how to get rid of redundant code</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[</p>
<p>Yesterday, we <a href="http://khason.net/blog/work-process-how-to-use-and-build-type-converters/">spoke about type converters</a>. We even, built simple generic enum converter. Today, we’ll create more complicated converter, that very missing in Silverlight – ThicknessConverter. During the post, I also explain about tokenizing values in Silverlight</p>
<p><img title="image" border="0" alt="image" src="http://khason.net/images/2008/12/image-7aa2208a-269e-487e-8ef4-7713a245679d.png" width="178" height="119" /> </p>
<h3>What is Thickness ?</h3>
<p>What is thickness in Silverlight? It’s </p>
<blockquote><p>&lt;Border BorderThickness=”6”/&gt;</p>
</blockquote>
<p>or</p>
<blockquote><p>&lt;Border BorderThickness=”6,4,3,2”/&gt; or &lt;Border BorderThickness=”6, 4, 3,2”/&gt; or, even &lt;Border BorderThickness=”6; 4; 3 ;2”/&gt;</p>
</blockquote>
<p>How to handle it?</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
<h3>Tokenizing strings</h3>
<p>What you seen here is tokenized strings. We have to split them by known token (one and own for each string) and then we can parse it for converter. How to do this? Complicated – too much cases. But, basically, you have to get string, quote character and separator. Don’t forget to check for empty spaces</p>
<blockquote><p>private void Initialize(string str, char quoteChar, char separator)     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this._str = str;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this._strLen = (str == null) ? 0 : str.Length;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this._currentTokenIndex = -1;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this._quoteChar = quoteChar;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this._argSeparator = separator;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; while (this._charIndex &lt; this._strLen)      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (!char.IsWhiteSpace(this._str, this._charIndex))      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this._charIndex++;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</blockquote>
<p>Then we have to scan string to find tokens</p>
<blockquote><p>private void ScanToNextToken(char separator)     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (this._charIndex &lt; this._strLen)      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; char c = this._str[this._charIndex];      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if ((c != separator) &amp;&amp; !char.IsWhiteSpace(c))      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Exceptions.ThrowInvalidOperationException(&quot;No Separator Found&quot;);      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; int i = 0;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; while (this._charIndex &lt; this._strLen)      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; c = this._str[this._charIndex];      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (c == separator)      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this._foundSeparator = true;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; i++;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this._charIndex++;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (i &gt; 1)      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Exceptions.ThrowInvalidOperationException(&quot;Empty Token Found&quot;);      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; else      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (!char.IsWhiteSpace(c))      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; break;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; this._charIndex++;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if ((i &gt; 0) &amp;&amp; (this._charIndex &gt;= this._strLen))      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Exceptions.ThrowInvalidOperationException(&quot;Emply Token Found&quot;);      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</blockquote>
<p>why not just split? Because it is not generic solution for strings with empty tokens, which is absolutely invalid. Another reason of using such helper is performance. String operation are not very fast things, thus we’ll check only the number of tokens required for future operations. </p>
<p>Also, we should make sure, that all tokens are required and get rid of unnecessary parts of the string, such as leading spaces, control characters etc. Now, when we have tokenized string, we can start building converter</p>
<h3>Building thickness converter</h3>
<p>Actually, the most significant part of this converter is tokenization , thus the most important override method for such converter is ConvertFromString</p>
<p>public override object ConvertFromString(string text)   <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Thickness res = new Thickness();    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; TokenizerHelper helper = new TokenizerHelper(text);    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; double[] numArray = new double[4];    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; int index = 0;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; while (helper.NextToken())    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (index &gt;= 4)    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; index = 5;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; break;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; LengthConverter lc = new LengthConverter();    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; numArray[index] = (double)lc.ConvertFromString(helper.GetCurrentToken());    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; index++;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; switch (index)    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; case 1:    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; res = new Thickness(numArray[0]); break;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; case 2:    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; res = new Thickness(numArray[0], numArray[1], numArray[0], numArray[1]); break; </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; case 4:   <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; res = new Thickness(numArray[0], numArray[1], numArray[2], numArray[3]); break;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; default:    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; typeof(Thickness).ThrowConvertFromException(text); break;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return res;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p>The only thing to remember is to check whither we can convert from the type received from XAML</p>
<p>public override bool CanConvertFrom(Type sourceType)   <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; switch (Type.GetTypeCode(sourceType))    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; case TypeCode.Int16:    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; case TypeCode.UInt16:    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; case TypeCode.Int32:    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; case TypeCode.UInt32:    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; case TypeCode.Int64:    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; case TypeCode.UInt64:    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; case TypeCode.Single:    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; case TypeCode.Double:    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; case TypeCode.Decimal:    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; case TypeCode.String:    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return true;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return false;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p>Have a nice day and be good people. Stay tuned for future <a href="http://blogs.microsoft.co.il/blogs/tamir/archive/tags/Work+process/default.aspx">work process</a> items.</p>


<p>Related posts:<ol><li><a href='http://khason.net/dev/inotifypropertychanged-auto-wiring-or-how-to-get-rid-of-redundant-code/' rel='bookmark' title='Permanent Link: INotifyPropertyChanged auto wiring or how to get rid of redundant code'>INotifyPropertyChanged auto wiring or how to get rid of redundant code</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://khason.net/blog/how-to-handle-thickness/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Work process: How to use and build type converters</title>
		<link>http://khason.net/blog/work-process-how-to-use-and-build-type-converters/</link>
		<comments>http://khason.net/blog/work-process-how-to-use-and-build-type-converters/#comments</comments>
		<pubDate>Mon, 07 Jul 2008 20:58:15 +0000</pubDate>
		<dc:creator>Tamir</dc:creator>
				<category><![CDATA[BLOG]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[source]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Work process]]></category>
		<category><![CDATA[WPF/E]]></category>

		<guid isPermaLink="false">http://khason.net/blog/work-process-how-to-use-and-build-type-converters/</guid>
		<description><![CDATA[Today, I want to start new tag &#8211; “Work process”. Here I’m going to publish partial classes, fixes, small utilities, I’m building for myself or clients to help in work process. I’ll give an example: currently, I’m working on BiDi support for Silverlight 2.0 (beta 2 to RTM). During the work process, I need to [...]


Related posts:<ol><li><a href='http://khason.net/dev/inotifypropertychanged-auto-wiring-or-how-to-get-rid-of-redundant-code/' rel='bookmark' title='Permanent Link: INotifyPropertyChanged auto wiring or how to get rid of redundant code'>INotifyPropertyChanged auto wiring or how to get rid of redundant code</a></li>
<li><a href='http://khason.net/itpro/quick-it-tip-how-to-build-bootable-usb-stick/' rel='bookmark' title='Permanent Link: Quick IT tip: How to build bootable USB stick'>Quick IT tip: How to build bootable USB stick</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Today, I want to start new tag &#8211; “<a href="http://blogs.microsoft.co.il/blogs/tamir/archive/tags/Work+process/default.aspx">Work process</a>”. Here I’m going to publish partial classes, fixes, small utilities, I’m building for myself or clients to help in work process. <em>I’ll give an example</em>: currently, I’m working on <a href="http://www.codeplex.com/SilverlightRTL">BiDi support for Silverlight 2.0</a> (beta 2 to RTM). During the work process, I need to write different classes, such as converters, string utilities, exception helpers etc. </p>
<p><img title="ZK4Y7556" border="0" alt="ZK4Y7556" src="http://khason.net/images/2008/12/200453681-001-3086d77d-7abf-49e4-8fd9-35f091441969.jpg" width="337" height="506" />&#160; <br /><font size="1"><a href="http://www.hendricksonphoto.com/">© Imaginary copyright by&#160; Noel Hendrickson</a></font><font size="1"></font></p>
<p>So, today I’ll public general <strong>type converter for generic classes</strong>.</p>
<h3>What is type converter?</h3>
<p><a href="http://msdn.microsoft.com/en-us/library/cc645047(VS.95).aspx">TypeConverter</a> is a service attribute, used to help rendering engine to convert XAML string (usually it strings) to the type, you require in your class implementation. Here an example:</p>
<p>In your custom class you have dependency property of type TextAlignment</p>
<blockquote><p>public TextAlignment TextAlignment     <br />&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; get { return (TextAlignment)GetValue(TextAlignmentProperty); }      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; set { SetValue(TextAlignmentProperty, value); }      <br />&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</blockquote>
<p>In XAML code it will be used as:</p>
<blockquote><p>&lt;MyControl TextAlignment=”Left”/&gt;</p>
</blockquote>
<p>But, what you’re actually transfer to your class is string “Left”, when you need enum TextAlignment.Left, how to convert it? This for we’re using type converters</p>
<h3>Attribute usage</h3>
<p>In order to “tell” framework to use your type converter, you should mark target property with special attribute TypeConverterAttribute. Also, you can provide default value to your property by using another attribute DefaultValueAttribute. This will looks as following:</p>
<blockquote><p><strong>[TypeConverter(typeof(MyTextAlignmentTypeConverter))]       <br />[DefaultValue(TextAlignment.Left)]        <br /></strong>public TextAlignment TextAlignment      <br /> {      <br />&#160; get { return (TextAlignment)GetValue(TextAlignmentProperty); }      <br />&#160; set { SetValue(TextAlignmentProperty, value); }      <br /> }</p>
</blockquote>
<h3>How to build type converter</h3>
<p>In order to build type converter, all you have to do is to build your own class, derived from TypeConverter. It also can be generic class. Then, implement necessary methods, that incorporate your business logic. Like this one, converter any enum value back and forward for TypeConverterAttribute</p>
<blockquote><p>public class EnumValueConverter&lt;T&gt; : TypeConverter where T:struct     <br />&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; static EnumValueConverter()      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (!typeof(T).IsEnum) { Exceptions.ThrowInvalidOperationException(&quot;Cannot use this type for conversion&quot;); }      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; } </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; public override bool CanConvertFrom(Type sourceType)     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return Type.GetTypeCode(sourceType) == TypeCode.String;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; } </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; public override object ConvertFrom(object value)     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (value == null)      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; typeof(T).ThrowConvertFromException(value);      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (value is string)      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return ConvertFromString(value as string);      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return (T)value;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; } </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; public override object ConvertFromString(string text)     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return (T)Enum.Parse(typeof(T), text, true);      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; } </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; public override string ConvertToString(object value)     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return Enum.GetName(typeof(T), value);      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br />&#160;&#160;&#160; }</p>
</blockquote>
<h3>Final result</h3>
<p>Now, when we built converter and know how to use it, we can easily set it to any DependencyProperty requires conversion from / to enum</p>
<p>[TypeConverter(typeof(EnumValueConverter&lt;LineStackingStrategy&gt;))]   <br />public LineStackingStrategy LineStackingStrategy    <br /> {    <br />&#160; get { return (LineStackingStrategy)GetValue(LineStackingStrategyProperty); }    <br />&#160; set { SetValue(LineStackingStrategyProperty, value); }    <br /> }</p>
<p>[TypeConverter(typeof(EnumValueConverter&lt;TextAlignment&gt;))]   <br />[DefaultValue(TextAlignment.Right)]    <br /> public TextAlignment TextAlignment    <br />&#160; {    <br />&#160;&#160; get { return (TextAlignment)GetValue(TextAlignmentProperty); }    <br />&#160;&#160; set { SetValue(TextAlignmentProperty, value); }    <br />&#160; }</p>
<p>[TypeConverter(typeof(EnumValueConverter&lt;TextWrapping&gt;))]   <br />public TextWrapping TextWrapping    <br /> {    <br />&#160; get { return (TextWrapping)GetValue(TextWrappingProperty); }    <br />&#160; set { SetValue(TextWrappingProperty, value); }    <br /> }</p>
<p>We done, have a nice day and be good people.</p>
<p>Stay tuned for release of BiDi support for Silverlight 2.0, sponsored by Development Platform Evangelism unit of Microsoft Israel</p>


<p>Related posts:<ol><li><a href='http://khason.net/dev/inotifypropertychanged-auto-wiring-or-how-to-get-rid-of-redundant-code/' rel='bookmark' title='Permanent Link: INotifyPropertyChanged auto wiring or how to get rid of redundant code'>INotifyPropertyChanged auto wiring or how to get rid of redundant code</a></li>
<li><a href='http://khason.net/itpro/quick-it-tip-how-to-build-bootable-usb-stick/' rel='bookmark' title='Permanent Link: Quick IT tip: How to build bootable USB stick'>Quick IT tip: How to build bootable USB stick</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://khason.net/blog/work-process-how-to-use-and-build-type-converters/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Silverlight Visual Tree Investigation</title>
		<link>http://khason.net/blog/silverlight-visual-tree-investigation/</link>
		<comments>http://khason.net/blog/silverlight-visual-tree-investigation/#comments</comments>
		<pubDate>Mon, 07 Jul 2008 07:44:31 +0000</pubDate>
		<dc:creator>Tamir</dc:creator>
				<category><![CDATA[BLOG]]></category>
		<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[source]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[WPF/E]]></category>

		<guid isPermaLink="false">http://khason.net/blog/silverlight-visual-tree-investigation/</guid>
		<description><![CDATA[Wait a moment. Silverlight has Visual and Logical Trees as well as WPF? Not exactly. The design more similar to how it was in WinForms (Parent-Child relationship). But dev team made all possible to make it syntactically similar to WPF. Let’s start Challenge: Find and hold references to all element, contains text. Solution: Recursive VisualTreeHelper [...]


Related posts:<ol><li><a href='http://khason.net/dev/read-singleton-approach-in-wpf-application/' rel='bookmark' title='Permanent Link: Real singleton approach in WPF application'>Real singleton approach in WPF application</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Wait a moment. Silverlight has Visual and Logical Trees as well as WPF? Not exactly. The design more similar to how it was in WinForms (Parent-Child relationship). But dev team made all possible to make it syntactically similar to WPF. Let’s start</p>
<p><strong>Challenge</strong>: Find and hold references to all element, contains text.</p>
<p><strong>Solution</strong>: Recursive VisualTreeHelper (introduced in SL2.0 b2)</p>
<p>First of all we should know, that Silverlight is not ready for manufacture, thus direct references might produce memory leaks, thus we’ll use WeakReference to hold pointers to SL objects.</p>
<p><strong>First step</strong>: We know for sure, that TextBox and TextBlock controls has Text property, that holds only text. Also TextBlock can hold Inlines collection with Runs and LineBreaks. Let’s find them all</p>
<blockquote><p>internal static List&lt;WeakReference&gt; GetTextElements(this DependencyObject root)     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />List&lt;WeakReference&gt; res = new List&lt;WeakReference&gt;();      <br />if (root is TextBlock | root is TextBox)      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; res.Add(new WeakReference(root));      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</blockquote>
<p>Next, we know, that ContentControl can hold text directly inside Content property. We’ll find them too, but should check if it holds text inside…</p>
<blockquote><p>else if (root is ContentControl)     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; (root as ContentControl).CheckAndAddStringContentControl(ref res);      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</blockquote>
<p>Now ItemsControl. It can hold inside either ContentElement, TextBox, TextBlock or other. So we should check inside it too.</p>
<blockquote><p>else if (root is ItemsControl)     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ItemsControl ic = root as ItemsControl;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; for (int i = 0; i &lt; ic.Items.Count; i++)      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (ic.Items[i] is ContentControl)      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; (ic.Items[i] as ContentControl).CheckAndAddStringContentControl(ref res);      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; else      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; List&lt;WeakReference&gt; tmp = (ic.Items[i] as DependencyObject).GetTextElements();      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (tmp != null &amp;&amp; tmp.Count &gt; 0)      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; res.AddRange(tmp);      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</blockquote>
<p>And last, but not least is to dig into all child of each control.</p>
<blockquote><p>else     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; int cnt = VisualTreeHelper.GetChildrenCount(root); </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; for (int i = 0; i &lt; cnt; i++)     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; DependencyObject d = VisualTreeHelper.GetChild(root, i);      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; List&lt;WeakReference&gt; tmp = d.GetTextElements();      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (tmp != null &amp;&amp; tmp.Count &gt; 0)      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; res.AddRange(tmp);      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; } </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return res;</p>
</blockquote>
<p><strong>Step two</strong>: Check whether object contains text inside. This one is simple. If not, we’ll call the main method to inter inside the control and check deeper.</p>
<blockquote><p>internal static void CheckAndAddStringContentControl(this ContentControl cc, ref List&lt;WeakReference&gt; res)     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (cc.Content is string)      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; res.Add(new WeakReference(cc));      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; else      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; res.AddRange(cc.GetTextElements());      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</blockquote>
<p>We done. Now we have WeakReferences collection of all items, which contains text in our page.</p>
<p>Have a good day and be nice one to another.</p>


<p>Related posts:<ol><li><a href='http://khason.net/dev/read-singleton-approach-in-wpf-application/' rel='bookmark' title='Permanent Link: Real singleton approach in WPF application'>Real singleton approach in WPF application</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://khason.net/blog/silverlight-visual-tree-investigation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Quick Silverlight Tip: How to access HTML DOM from Silverlight and Silverlight managed objects from HTML?</title>
		<link>http://khason.net/blog/quick-silverlight-tip-how-to-access-html-dom-from-silverlight-and-silverlight-managed-objects-from-html/</link>
		<comments>http://khason.net/blog/quick-silverlight-tip-how-to-access-html-dom-from-silverlight-and-silverlight-managed-objects-from-html/#comments</comments>
		<pubDate>Tue, 01 Jul 2008 12:56:33 +0000</pubDate>
		<dc:creator>Tamir</dc:creator>
				<category><![CDATA[BLOG]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[help]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[source]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[WPF/E]]></category>

		<guid isPermaLink="false">http://khason.net/blog/quick-silverlight-tip-how-to-access-html-dom-from-silverlight-and-silverlight-managed-objects-from-html/</guid>
		<description><![CDATA[After my session yesterday, I got number of email from people, who have a problem to access HTML DOM from Silverlight and vice verse. I want to answer to all of you by this post. Access HTML from Silverlight You have not do anything special in Silverlight to access it.&#160; Your friend is HtmlPage.Window object. [...]


Related posts:<ol><li><a href='http://khason.net/itpro/quick-it-tip-how-to-build-bootable-usb-stick/' rel='bookmark' title='Permanent Link: Quick IT tip: How to build bootable USB stick'>Quick IT tip: How to build bootable USB stick</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>After <a href="http://khason.net/blog/printouts-of-the-slides-presented-on-silverlight-20-open-house-today/">my session yesterday</a>, I got number of email from people, who have a problem to access HTML DOM from Silverlight and vice verse. I want to answer to all of you by this post.</p>
<h3>Access HTML from Silverlight</h3>
<p>You have not do anything special in Silverlight to access it.&#160; Your friend is <strong>HtmlPage.Window</strong> object. For example, if I want to execute <em>window.open</em> JavaScript method from Silverlight, all I have to do is to call:</p>
<blockquote><p>public void OpenWindow()</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p>HtmlPage.Window.Invoke(&quot;open&quot;, new object[] { &quot;<a href="http://blogs.microsoft.co.il/blogs/tamir">http://blogs.microsoft.co.il/blogs/tamir</a>&quot;, &quot;login&quot;, &quot;resizable=1,width=646,height=436&quot; });</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
</blockquote>
<p>HtmlPage.Window.Invoke also returns Object – this is the response of function executed.</p>
<h3>Access Silverlight Managed Objects from HTML</h3>
<p>Here you should remember two things:</p>
<ul>
<li>You should mark <u>both</u><strong> class</strong> and <strong>member</strong> you want to access with special attributes <strong>ScriptableType </strong>(for class) and <strong>ScriptableMember</strong> (for member)</li>
<li>Register Scriptable object anywhere (for example in Constructor).</li>
</ul>
<p>So, if you want OpenWindow method to be exposed from the Page class to HTML, you should use following code:</p>
<blockquote><p>&#160;&#160;&#160; [ScriptableType]     <br />public partial class Page : UserControl&#160;&#160;&#160; {      <br /> public Page() {      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; InitializeComponent();      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; HtmlPage.RegisterScriptableObject(&quot;Page&quot;, this);      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p> [ScriptableMember]     <br />public void OpenWindow() {      <br />object o = HtmlPage.Window.Invoke(&quot;open&quot;, new object[] { &quot;<a href="http://blogs.microsoft.co.il/blogs/tamir">http://blogs.microsoft.co.il/blogs/tamir</a>&quot;, &quot;login&quot;, &quot;resizable=1,width=646,height=436&quot; });      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }      <br /> }</p>
</blockquote>
<p>That’s all, folks. Be good people and have a nice day. </p>
<p>Here the script of test application:</p>
<blockquote><p><b>Page.xaml</b></p>
<p>&lt;UserControl x:Class=&quot;DomAccessDemo.Page&quot;</p>
<p> xmlns=&quot;<a href="http://schemas.microsoft.com/winfx/2006/xaml/presentation">http://schemas.microsoft.com/winfx/2006/xaml/presentation</a>&quot;</p>
<p> xmlns:x=&quot;<a href="http://schemas.microsoft.com/winfx/2006/xaml">http://schemas.microsoft.com/winfx/2006/xaml</a>&quot;</p>
<p> Height=&quot;20&quot;&gt;</p>
<p>&lt;Grid x:Name=&quot;LayoutRoot&quot; Background=&quot;White&quot;&gt;</p>
<p>&lt;Button Click=&quot;Button_Click&quot; Content=&quot;Fun!&quot;/&gt;</p>
<p>&lt;/Grid&gt;</p>
<p>&lt;/UserControl&gt;</p>
<p><b>Page.xaml.cs</b></p>
<p>using System.Windows;</p>
<p>using System.Windows.Controls;</p>
<p>using System.Windows.Documents;</p>
<p>using System.Windows.Input;</p>
<p>using System.Windows.Media;</p>
<p>using System.Windows.Media.Animation;</p>
<p>using System.Windows.Shapes;</p>
<p>using System.Windows.Browser;</p>
<p>using System.ComponentModel;</p>
<p>namespace DomAccessDemo</p>
<p>{</p>
<p>&#160;&#160;&#160; [ScriptableType]</p>
<p>public partial class Page : UserControl</p>
<p>&#160;&#160;&#160; {</p>
<p>public Page()</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; InitializeComponent();</p>
<p>HtmlPage.RegisterScriptableObject(&quot;Page&quot;, this);</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p>private void Button_Click(object sender, RoutedEventArgs e)</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; OpenWindow();</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; [ScriptableMember]</p>
<p>public void OpenWindow()</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p>object o = HtmlPage.Window.Invoke(&quot;open&quot;, new object[] { &quot;<a href="http://blogs.microsoft.co.il/blogs/tamir">http://blogs.microsoft.co.il/blogs/tamir</a>&quot;, &quot;login&quot;, &quot;resizable=1,width=646,height=436&quot; });</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p>&#160;&#160;&#160; }</p>
<p>}</p>
<p><b>DomAccessDemoTestPage.aspx</b></p>
<p>&lt;html xmlns=&quot;<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>&quot; style=&quot;height:100%;&quot;&gt;</p>
<p>&lt;head runat=&quot;server&quot;&gt;</p>
<p>&lt;title&gt;Test Page For DomAccessDemo&lt;/title&gt;</p>
<p>&lt;script language=&quot;javascript&quot;&gt;</p>
<p>function testSL()</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p>
<p>var xaml = document.getElementById(&quot;Xaml1&quot;); </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; xaml.Content.Page.OpenWindow(); </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p>
<p>&lt;/script&gt;</p>
<p>&lt;/head&gt;</p>
<p>&lt;body style=&quot;height:100%;margin:0;&quot;&gt;</p>
<p>&lt;form id=&quot;form1&quot; runat=&quot;server&quot; style=&quot;height:100%;&quot;&gt;</p>
<p>&lt;asp:ScriptManager ID=&quot;ScriptManager1&quot; runat=&quot;server&quot;&gt;&lt;/asp:ScriptManager&gt;</p>
<p>&lt;div style=&quot;height:100%;&quot;&gt;</p>
<p>&lt;asp:Silverlight ID=&quot;Xaml1&quot; runat=&quot;server&quot; Source=&quot;~/ClientBin/DomAccessDemo.xap&quot; MinimumVersion=&quot;2.0.30523&quot; Width=&quot;100%&quot; Height=&quot;100%&quot; /&gt;</p>
<p>&lt;/div&gt;</p>
<p>&lt;/form&gt;</p>
<p>&lt;button onmouseup=&quot;testSL();&quot;&gt;Test&lt;/button&gt;</p>
<p>&lt;/body&gt;</p>
<p>&lt;/html&gt;</p>
</blockquote>


<p>Related posts:<ol><li><a href='http://khason.net/itpro/quick-it-tip-how-to-build-bootable-usb-stick/' rel='bookmark' title='Permanent Link: Quick IT tip: How to build bootable USB stick'>Quick IT tip: How to build bootable USB stick</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://khason.net/blog/quick-silverlight-tip-how-to-access-html-dom-from-silverlight-and-silverlight-managed-objects-from-html/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Printouts of the slides, presented on Silverlight 2.0 open house today</title>
		<link>http://khason.net/blog/printouts-of-the-slides-presented-on-silverlight-20-open-house-today/</link>
		<comments>http://khason.net/blog/printouts-of-the-slides-presented-on-silverlight-20-open-house-today/#comments</comments>
		<pubDate>Mon, 30 Jun 2008 16:26:45 +0000</pubDate>
		<dc:creator>Tamir</dc:creator>
				<category><![CDATA[BLOG]]></category>
		<category><![CDATA[blogging general]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[promo]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[WPF/E]]></category>

		<guid isPermaLink="false">http://khason.net/blog/printouts-of-the-slides-presented-on-silverlight-20-open-house-today/</guid>
		<description><![CDATA[Thank you all, who participated today in Silverlight 2.0 for building Rich Internet Applications event. I uploaded printouts of the slides, presented during the session here. So, you can download it for reference. I’m really interesting within your feedback (leave comments) in order for me to be able to enhance it for future events. Also, [...]

<p/>]]></description>
			<content:encoded><![CDATA[<p>Thank you all, who participated today in <a href="http://khason.net/blog/silverlight-20-for-building-rich-internet-applications-local-event/">Silverlight 2.0 for building Rich Internet Applications event</a>. I uploaded printouts of the slides, presented during the session here. So, you can download it for reference.</p>
<p>I’m really interesting within your feedback (<em>leave comments</em>) in order for me to be able to enhance it for future events.</p>
<p>Also, I want to remind you, that due to lack of space <a href="http://khason.net/blog/please-do-not-ask-me-we-cannot-handle-more%e2%80%a6/">we were unable to handle all those who want to attend</a>, thus we decided <a href="http://khason.net/blog/silverlight-20-for-building-rich-internet-applications-local-event-%e2%80%93-take-2/">to make another session within two weeks</a>, so can <a href="https://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032382418&amp;culture=he-IL">register here</a>. Remember, only registered attendees, who with confirmation received will allow to enter. So, hurry up.</p>
<p>If you want to learn more about Silverlight, you can register to attend Expert Days “<strong><a href="http://www.e4d.co.il/events/expertdays/Course.aspx?CourseID=020&amp;Course=Mastering%20Microsoft%20Silverlight%202.0">Mastering Microsoft Silverlight 2.0 – 020</a>”</strong> full day course, <strong>13-August</strong>, were we’ll have Silverlight 2.0 deep dive training day. You can also review another two courses, I’ll have there: </p>
<ul>
<li>12-Aug “<strong><a href="http://www.e4d.co.il/events/expertdays/Course.aspx?CourseID=027&amp;Course=Windows%20Presentation%20Foundation%20for%20fellow%20developers">Windows Presentation Foundation for fellow developers – 027</a></strong>”</li>
<li>14-Aug “<strong><a href="http://www.e4d.co.il/events/expertdays/Course.aspx?CourseID=011&amp;Course=Advanced%20.NET%203.5%20SP1%20%E2%80%93%20Windows%20Presentation%20Foundation">Advanced .NET 3.5 SP1 – Windows Presentation Foundation – 011</a></strong>”</li>
</ul>
<p><a href="http://blogs.microsoft.co.il/files/folders/110009/download.aspx"><strong>Download slides from today session &gt;&gt;</strong></a></p>
<p><a href="https://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032382418&amp;culture=he-IL"><strong>Register to attend next event at 23 July &gt;&gt;</strong></a></p>
<p><img title="image" border="0" alt="image" src="http://khason.net/images/2008/12/image-2977810c-d3f4-451a-bb97-26b45857a156.png" width="422" height="215" /> </p>
<p>Thank you and leave a comment…</p>


<p/>]]></content:encoded>
			<wfw:commentRss>http://khason.net/blog/printouts-of-the-slides-presented-on-silverlight-20-open-house-today/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Please do not ask me. We cannot handle more…</title>
		<link>http://khason.net/blog/please-do-not-ask-me-we-cannot-handle-more%e2%80%a6/</link>
		<comments>http://khason.net/blog/please-do-not-ask-me-we-cannot-handle-more%e2%80%a6/#comments</comments>
		<pubDate>Thu, 26 Jun 2008 13:34:00 +0000</pubDate>
		<dc:creator>Tamir</dc:creator>
				<category><![CDATA[BLOG]]></category>
		<category><![CDATA[blogging general]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[promo]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[WPF/E]]></category>

		<guid isPermaLink="false">http://khason.net/blog/please-do-not-ask-me-we-cannot-handle-more%e2%80%a6/</guid>
		<description><![CDATA[Monday next week, I’m making half day session about building rich internet applications with Silverlight 2.0 in ILDC. The registration is over a while ago, but during this week, I got very large amount of email, IMs and phones calls with appeal to attend. Sorry, we’re fully booked (currently about twice of available seats and [...]

<p/>]]></description>
			<content:encoded><![CDATA[<p>
<p>Monday next week, I’m <a href="http://khason.net/blog/silverlight-20-for-building-rich-internet-applications-local-event/" mce_href="http://khason.net/blog/silverlight-20-for-building-rich-internet-applications-local-event/">making half day session about building rich internet applications with Silverlight 2.0</a> in ILDC. The <u>registration is over a while ago</u>, but during this week, I got very large amount of email, IMs and phones calls with appeal to attend. </p>
<p><b>Sorry, we’re fully booked (currently about twice of available seats and it&#8217;s about 200) and cannot handle any more. <u>Only those who registered and got confirmations will allow to enter</u>. It is for your own convenience!</b></p>
<p>I promise, that within next two weeks <u>I’ll make the same session again</u> for all those, who want to come, but unable to register and attend (after a small arrangement, I’ll publish here next date).</p>
<p>Thank you again for understanding and see you soon.</p>
<p><img src="http://khason.net/images/2008/12/image-c66f0029-e65a-4b72-bada-0315ca465f6d1.png" mce_src="http://khason.net/images/2008/12/image-c66f0029-e65a-4b72-bada-0315ca465f6d1.png"/></p>


<p/>]]></content:encoded>
			<wfw:commentRss>http://khason.net/blog/please-do-not-ask-me-we-cannot-handle-more%e2%80%a6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Silverlight 2.0 for building Rich Internet Applications (local event)</title>
		<link>http://khason.net/blog/silverlight-20-for-building-rich-internet-applications-local-event/</link>
		<comments>http://khason.net/blog/silverlight-20-for-building-rich-internet-applications-local-event/#comments</comments>
		<pubDate>Tue, 20 May 2008 15:23:00 +0000</pubDate>
		<dc:creator>Tamir</dc:creator>
				<category><![CDATA[BLOG]]></category>
		<category><![CDATA[Accessibility]]></category>
		<category><![CDATA[Blogging rules]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[promo]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[WPF/E]]></category>

		<guid isPermaLink="false">http://khason.net/blog/silverlight-20-for-building-rich-internet-applications-local-event/</guid>
		<description><![CDATA[If you are developing rich internet applications, web sites or just want to learn what’s new in Silverlight 2.0, you are invited to attend&#160; local (Israel) Microsoft Event 30-June 8:30 AM- 12:30 PM in ILDC (new Microsoft R&#38;D offices in Herzliya). I’ll speak about what is Silverlight 2.0 and what are differences between SL 1.0 [...]

<p/>]]></description>
			<content:encoded><![CDATA[<p>If you are developing rich internet applications, web sites or just want to learn <strong><a href="https://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032379874&amp;culture=he-IL" target="_blank">what’s new in Silverlight 2.0</a></strong>, you are invited to attend&#160; local (Israel) Microsoft Event <strong>30-June 8:30 AM- 12:30 PM </strong>in ILDC (new Microsoft R&amp;D offices in Herzliya).</p>
<p><img title="image" border="0" alt="image" src="http://khason.net/images/2008/12/image-c66f0029-e65a-4b72-bada-0315ca465f6d2.png" width="299" height="221" /> </p>
<p>I’ll speak about what is Silverlight 2.0 and what are differences between SL 1.0 and SL 2.0. How to build RIA with Silverlight 2.0. What is DeepZoom and some cool parts of this new internet technology. How to use DataBinding, LINQ and Microsoft Media Encoder. As small bonus for this part of the event, we’ll compare different technologies with Silverlight such as Flex, Flash 10, Thermo and SVG to make you understand differences, “pros and cons” of using each one of those technologies for specific and identifiable <b></b>needs. </p>
<p>Then we’ll deep dive into DataBinding and learn about DataTemplates, Styles, Accessibility, Authoring and Usability (including differences between Silverlight and WPF approaches). We’ll also take a look into LINQ-to-SQL and SEO of Silverlight applications.</p>
<p>Third part of this event will be dedicated to networking and interoperability. We’ll speak about internet protocols such as ATOM, RSS, REST, JSON, XMLWS, etc and usage of those protocols by Silverlight applications. We’ll understand how WCF can assist us to make our live easier. We’ll also touch security issues of Silverlight deployment and networking such as Cross Domain Policy, progressive download and deploy-on-demand.</p>
<p>I can make you sure, that if ”you are in” new web technologies and want to start using (or if you already using) Silverlight 2.0 for development of your applications – come and see me. Will be fun!</p>
<p><em>Note</em>: Only a limited number of places available, so do not wait for MS newsletter (by the end of this month). <a href="https://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032379874&amp;culture=he-IL" target="_blank">Book now</a> and come to attend this event – it’s free! (there is parking lot in floor -2)</p>
<blockquote><p><a href="https://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032379874&amp;culture=he-IL" target="_blank">I want to attend “Silverlight 2.0 introduction for building rich internet applications” event</a> (30-June 8:30 AM – 12:30 PM, ILDC Herzelia, 5, Gav-Yam str, floor L2 13,Shenkar str)</p>
</blockquote>
<p>See you there…</p>
<p><b>UPD 29-Jun:</b> Next session will take place at <b>23 July, 8:30 AM-12:30 PM</b>. Register by visiting Microsoft events web application at <a href="https://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032382418&#038;culture=he-IL" target="_blank">https://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032382418&#038;culture=he-IL</a> and sign up.</p>


<p/>]]></content:encoded>
			<wfw:commentRss>http://khason.net/blog/silverlight-20-for-building-rich-internet-applications-local-event/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>FindResource replacement and how to change control style more then once in Silverlight 2.0 application</title>
		<link>http://khason.net/blog/findresource-replacement-and-how-to-change-control-style-more-then-once-in-silverlight-20-application/</link>
		<comments>http://khason.net/blog/findresource-replacement-and-how-to-change-control-style-more-then-once-in-silverlight-20-application/#comments</comments>
		<pubDate>Tue, 25 Mar 2008 20:24:00 +0000</pubDate>
		<dc:creator>Tamir</dc:creator>
				<category><![CDATA[BLOG]]></category>
		<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[source]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[WPF/E]]></category>

		<guid isPermaLink="false">http://khason.net/blog/findresource-replacement-and-how-to-change-control-style-more-then-once-in-silverlight-20-application/</guid>
		<description><![CDATA[As deeper we’re digging in Silverlight 2.0, we finding more and more WPF things and we’re really missing in Silverlight. One of such things is FindResources. In WPF I had Visual and Logical tree, so I was able to travel the tree to find resource I need. Let’s see an example of the application. We [...]


Related posts:<ol><li><a href='http://khason.net/dev/read-singleton-approach-in-wpf-application/' rel='bookmark' title='Permanent Link: Real singleton approach in WPF application'>Real singleton approach in WPF application</a></li>
<li><a href='http://khason.net/dev/inotifypropertychanged-auto-wiring-or-how-to-get-rid-of-redundant-code/' rel='bookmark' title='Permanent Link: INotifyPropertyChanged auto wiring or how to get rid of redundant code'>INotifyPropertyChanged auto wiring or how to get rid of redundant code</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>As deeper we’re digging in Silverlight 2.0, we finding more and more WPF things and we’re really missing in Silverlight. One of such things is FindResources.</p>
<p>In WPF I had Visual and Logical tree, so I was able to travel the tree to find resource I need. Let’s see an example of the application. We have one resource defined in App level</p>
<blockquote>
<p>&lt;Application.Resources&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Style TargetType=&#8221;Button&#8221; x:Key=&#8221;green&#8221;&gt;       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Setter Property=&#8221;Background&#8221; Value=&#8221;Green&#8221;/&gt;       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Style&gt;       <br />&lt;/Application.Resources&gt;</p>
</blockquote>
<p>Another resources are defined in different levels of Page</p>
<blockquote>
<p>&lt;UserControl.Resources&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Style TargetType=&#8221;Button&#8221; x:Name=&#8221;red&#8221;&gt;       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Setter Property=&#8221;Background&#8221; Value=&#8221;Red&#8221;/&gt;       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Style&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; &lt;/UserControl.Resources&gt;       <br />&nbsp;&nbsp;&nbsp; &lt;Grid x:Name=&#8221;LayoutRoot&#8221; Background=&#8221;White&#8221;&gt;       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Grid.Resources&gt;       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Style TargetType=&#8221;Button&#8221; x:Name=&#8221;blue&#8221;&gt;       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Setter Property=&#8221;Background&#8221; Value=&#8221;Blue&#8221;/&gt;       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Style&gt;       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Grid.Resources&gt;       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Button Content=&#8221;Click me&#8221; Click=&#8221;Button_Click&#8221;&gt;       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Button.Resources&gt;       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Style TargetType=&#8221;Button&#8221; x:Name=&#8221;yellow&#8221;&gt;       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Setter Property=&#8221;Background&#8221; Value=&#8221;#FFFFFF00&#8243;/&gt;       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Style&gt;       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Button.Resources&gt;       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Button&gt;       <br />&nbsp;&nbsp;&nbsp; &lt;/Grid&gt;</p>
</blockquote>
<p>Now I want to call FindResource(“red”) and have my style ready for apply. I should not thing a lot about where the resource exists. There is no such method in Silverlight. If so, let’s see what we have. Looking in debugger I can find <u>all my resources</u> as members of the page. </p>
<p><img src="http://khason.net/images/2008/12/image-e3915132-8f7a-40da-a43f-2a6c9971b5a8.png" alt="image" mce_src="http://khason.net/images/2008/12/image-e3915132-8f7a-40da-a43f-2a6c9971b5a8.png" border="0" height="144" width="378"/> </p>
<p>But how to get them out? In Silverlight <b>FrameworkElement</b>, we have handy method named <b>FindName</b>. That’s exactly what we need. But how to get Application resources? Simple. Just look into it’s collection. Now, I can write small method, that help me to find resources in any level of Silverlight application. </p>
<blockquote>
<p>public static object FindResource(string name)      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (App.Current.Resources.Contains(name))       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return App.Current.Resources[name];       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FrameworkElement root = App.Current.RootVisual as FrameworkElement;       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return root.FindResource(name);       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; internal static object FindResource(this FrameworkElement root, string name)       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (root != null &amp;&amp; root.Resources.Contains(name))       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return root.Resources[name];       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return root.FindName(name);       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch { }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return null;</p>
</blockquote>
<p>Well now we can find all our resources. Let’s apply it to elements</p>
<blockquote>
<p>Style s = (Style)Helper.FindResource(&#8220;red&#8221;);      <br />b.Style=s;</p>
</blockquote>
<p>It works perfect. Let’s take another one</p>
<blockquote>
<p>Style s = (Style)Helper.FindResource(&#8220;blue&#8221;);      <br />b.Style = s;</p>
</blockquote>
<p><img src="http://khason.net/images/2008/12/image-a2789cba-bc68-4aa2-9ec0-1b5105413516.png" alt="image" mce_src="http://khason.net/images/2008/12/image-a2789cba-bc68-4aa2-9ec0-1b5105413516.png" border="0" height="232" width="528"/> </p>
<p>What is it? “Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))”. Why this happens only after applying <u>second</u> style? Let’s look <a href="http://msdn2.microsoft.com/en-us/library/cc189093%28VS.95%29.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/cc189093(VS.95).aspx" target="_blank">into MSDN</a>: “<i>Styles are write-once in Silverlight. You can set a style to override a built-in default style, but attempting to set the same style again will result in an exception.”</i></p>
<p>What to do? We just have to write our own multi-use styling engine. First of all, we should get all setters of the style</p>
<blockquote>
<p>foreach (Setter setter in value.Setters)</p>
</blockquote>
<p>Then check target type and set values of setters to appropriate properties.</p>
<blockquote>
<p>Type targetType = parent.GetValue(setter.Property).GetType();      <br />parent.SetValue(setter.Property, setter.Value);</p>
</blockquote>
<p>Another exception. That’s the mess? All values are strings. I need real values and I have no parsers. Fortunately, we have handy .NET method <b>Convert.ChangeType</b>. Let’s use it. Exception. Let’s check if the type is IConvertible (such as int, double, etc)</p>
<blockquote>
<p>if(targetType is IConvertible)      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return Convert.ChangeType(source, targetType, CultureInfo.InvariantCulture);       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</blockquote>
<p>No exception, but also no result. We need SolidColorBrush and we have only Color name, which is string. How to convert such string into Color instance and then into SolidColorBrush? The answer is reflection. We should write our own FromString converter. First of all let’s capitalize the string</p>
<blockquote>
<p>static string Capitalize(this string str)      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (str.Length &gt; 0)       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return string.Concat(str.Substring(0, 1).ToUpper(), str.Substring(1, str.Length &#8211; 1));       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return str;       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</blockquote>
<p>Then check whether I have static member with the same name in Colors class (not by hand of cause)</p>
<blockquote>
<p>MemberInfo[] infos = typeof(Colors).GetMember(color.Capitalize());</p>
</blockquote>
<p>If I have, let’s invoke it</p>
<blockquote>
<p>if (infos.Length &gt; 0)      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (Color)typeof(Colors).InvokeMember(color.Capitalize(), BindingFlags.Static | BindingFlags.Public | BindingFlags.GetProperty, null, null, null);       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</blockquote>
<p>Well done. Now we can use strings which have names of colors and convert them into real color. But what to do with not “well known colors”? Parse it</p>
<blockquote>
<p>else if (color.IndexOf(&#8216;#&#8217;) == 0)      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return Color.FromArgb(       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte.Parse(color.Substring(1, 2), NumberStyles.HexNumber),       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte.Parse(color.Substring(3, 2), NumberStyles.HexNumber),       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte.Parse(color.Substring(5, 2), NumberStyles.HexNumber),       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte.Parse(color.Substring(7, 2), NumberStyles.HexNumber));       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</blockquote>
<p>We done. Just write another attached method and use in in our converter</p>
<blockquote>
<p>internal static Color ParseKnownColor(string color)      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MemberInfo[] infos = typeof(Colors).GetMember(color.Capitalize());       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (infos.Length &gt; 0)       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (Color)typeof(Colors).InvokeMember(color.Capitalize(), BindingFlags.Static | BindingFlags.Public | BindingFlags.GetProperty, null, null, null);       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else if (color.IndexOf(&#8216;#&#8217;) == 0)       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return Color.FromArgb(       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte.Parse(color.Substring(1, 2), NumberStyles.HexNumber),       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte.Parse(color.Substring(3, 2), NumberStyles.HexNumber),       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte.Parse(color.Substring(5, 2), NumberStyles.HexNumber),       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte.Parse(color.Substring(7, 2), NumberStyles.HexNumber));       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return Colors.White;       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }       <br />……</p>
<p>else if(targetType == typeof(SolidColorBrush))      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return new SolidColorBrush(ParseKnownColor(source));       <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</blockquote>
<p>Now you turn to continue with string to object convention for your own need. Starting today you know how to do it.</p>
<p>Here the result</p>
<p><iframe src="http://0x15.net/play/SLFindResource/SLFindResource.html" style="width: 350px; height: 300px;" frameborder="0" scrolling="no">&amp;amp;lt;noframes&amp;amp;gt;Here the working application and you&#8217;re reading it from RSS reader. Visit the original article to see it&amp;amp;lt;/noframes&amp;amp;gt;</iframe></p>
<p>Have a nice day and be good people</p>
<p><a href="/blogs/tamir/attachment/69587.ashx" mce_href="/blogs/tamir/attachment/69587.ashx">Source code for this article&gt;&gt;</a></p>


<p>Related posts:<ol><li><a href='http://khason.net/dev/read-singleton-approach-in-wpf-application/' rel='bookmark' title='Permanent Link: Real singleton approach in WPF application'>Real singleton approach in WPF application</a></li>
<li><a href='http://khason.net/dev/inotifypropertychanged-auto-wiring-or-how-to-get-rid-of-redundant-code/' rel='bookmark' title='Permanent Link: INotifyPropertyChanged auto wiring or how to get rid of redundant code'>INotifyPropertyChanged auto wiring or how to get rid of redundant code</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://khason.net/blog/findresource-replacement-and-how-to-change-control-style-more-then-once-in-silverlight-20-application/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Networking (raw sockets) in Silverlight and WPF &#8211; messaging in general</title>
		<link>http://khason.net/blog/networking-raw-sockets-in-silverlight-and-wpf-messaging-in-general/</link>
		<comments>http://khason.net/blog/networking-raw-sockets-in-silverlight-and-wpf-messaging-in-general/#comments</comments>
		<pubDate>Sat, 22 Mar 2008 14:30:00 +0000</pubDate>
		<dc:creator>Tamir</dc:creator>
				<category><![CDATA[BLOG]]></category>
		<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[demos]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[source]]></category>
		<category><![CDATA[teched]]></category>
		<category><![CDATA[TechedIsrael2008]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[WPF/E]]></category>

		<guid isPermaLink="false">http://khason.net/blog/networking-raw-sockets-in-silverlight-and-wpf-messaging-in-general/</guid>
		<description><![CDATA[You, probably, know how to use raw sockets in WinForms. It&#8217;s pretty the same in WPF, however it is very different (and limited) in Silverlight. Today, we&#8217;ll create sample application in Silverlight, WPF and WinForm that sending and receiving updates via TCP as well as broadcasting it via UDP (singlecast and multicast). So let&#8217;s start. [...]


Related posts:<ol><li><a href='http://khason.net/dev/read-singleton-approach-in-wpf-application/' rel='bookmark' title='Permanent Link: Real singleton approach in WPF application'>Real singleton approach in WPF application</a></li>
<li><a href='http://khason.net/dev/inotifypropertychanged-auto-wiring-or-how-to-get-rid-of-redundant-code/' rel='bookmark' title='Permanent Link: INotifyPropertyChanged auto wiring or how to get rid of redundant code'>INotifyPropertyChanged auto wiring or how to get rid of redundant code</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>You, probably, know how to use raw sockets in WinForms. It&#8217;s pretty the same in WPF, however it is very different (and limited) in Silverlight. Today, we&#8217;ll create sample application in Silverlight, WPF and WinForm that sending and receiving updates via TCP as well as broadcasting it via UDP (singlecast and multicast). So let&#8217;s start.</p>
<p><img src="http://khason.net/images/2008/12/image-c64c1456-2b33-479e-a3e2-3e7fe7898532.png" alt="image" mce_src="http://khason.net/images/2008/12/image-c64c1456-2b33-479e-a3e2-3e7fe7898532.png" border="0" height="427" width="685"/> </p>
<p>First of all we&#8217;ll create WinForms server, that should distribute updates. It knows what the time is it now and broadcasting the time message via UDP. Also it has TCP server, that distribute updates to all it&#8217;s clients.</p>
<p>First of all UDP. We should create the working Socket first. It uses all ip addresses to broadcast changes via given port. In order to make the socket to be multicast, we should set appropriate socket options. Let&#8217;s see the code.</p>
<blockquote><p>lock (this)     <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (m_mainSocket == null)      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m_mainSocket = new Socket(AddressFamily.InterNetwork,      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SocketType.Dgram,      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ProtocolType.Udp); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, port); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m_mainSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m_mainSocket.Bind(ipLocal);     <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IPAddress ip = IPAddress.Parse(castGroupIp); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EPCast = new IPEndPoint(ip, port);     <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m_mainSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, IPAddress.Any));      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</blockquote>
<p>Now, the TCP part. It&#8217;s very similar (for server we do not need specific IP address to bind), however, we should not set multicast options there aside with begin listening right after connection was established.</p>
<blockquote><p>lock (this)     <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (m_mainSocket == null)      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m_mainSocket = new Socket(AddressFamily.InterNetwork,      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SocketType.Stream,      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ProtocolType.Tcp);      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, port);      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m_mainSocket.Bind(ipLocal);      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m_mainSocket.Listen(Backlog);      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m_mainSocket.BeginAccept(new AsyncCallback(acceptCallback), null);      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</blockquote>
<p>In order to send data via UDP. All we have to do is to write it into current socket.</p>
<blockquote><p>socket.SendTo(data, EPCast);</p>
</blockquote>
<p>In TCP world, we should first know who we want to sent to, thus we have to enumerate all incoming clients to save the references to their sockets.</p>
<blockquote><p>Socket workerSocket = m_mainSocket.EndAccept(asyn);     <br />m_workerSocketList.Add(workerSocket.GetHashCode(), workerSocket); </p>
<p>m_mainSocket.BeginAccept(new AsyncCallback(acceptCallback), null);</p>
</blockquote>
<p>Then when we know who to send, all we have to do is to send</p>
<blockquote><p>socket.Send(data);</p>
</blockquote>
<p>Now, when we know how to send, we should learn how to receive network messages. It&#8217;s exactly the same within TCP or UDP. First check is there is data to receive, then receive it.</p>
<blockquote><p>if (m_pfnCallBack == null)     <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m_pfnCallBack = new AsyncCallback(dataReceivedCallback);      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SocketPacket theSocPkt = new SocketPacket(socket, bufferSize); </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; socket.BeginReceive(theSocPkt.dataBuffer, 0,     <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; theSocPkt.dataBuffer.Length,      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SocketFlags.None,      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m_pfnCallBack,      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; theSocPkt);</p>
</blockquote>
<p>We done with WinForms and WPF networking. So, we can start with graphics. Since we have no a lot of graphics in WinForms, we&#8217;ll focus on WPF stuff.</p>
<p>We&#8217;ll use ContentControl to present the content with datatemplate of our message. We&#8217;ll create Ellipse for the clock and three rectangles for clock hands. Once data received, we should change RotateTransform value of RenderTransform for each of our rectangles (first set the TransformOrigin to the center of the clock). Bind it together</p>
<blockquote><p>&lt;Ellipse Width=&#8221;250&#8243; Height=&#8221;250&#8243; StrokeThickness=&#8221;2&#8243; Stroke=&#8221;Black&#8221;/&gt;     <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Rectangle Height=&#8221;100&#8243; Width=&#8221;20&#8243; RadiusX=&#8221;10&#8243; RadiusY=&#8221;10&#8243; Fill=&#8221;Black&#8221; RenderTransformOrigin=&#8221;0.5,1&#8243;&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Rectangle.RenderTransform&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;TransformGroup&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;RotateTransform/&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;TranslateTransform Y=&#8221;25&#8243; X=&#8221;115&#8243;/&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/TransformGroup&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Rectangle.RenderTransform&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Rectangle&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Rectangle Height=&#8221;125&#8243; Width=&#8221;10&#8243; RadiusX=&#8221;5&#8243; RadiusY=&#8221;5&#8243; Fill=&#8221;Black&#8221; RenderTransformOrigin=&#8221;0.5,1&#8243;&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Rectangle.RenderTransform&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;TransformGroup&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;RotateTransform/&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;TranslateTransform Y=&#8221;0&#8243; X=&#8221;120&#8243;/&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/TransformGroup&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Rectangle.RenderTransform&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Rectangle&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Rectangle Height=&#8221;125&#8243; Width=&#8221;4&#8243;&nbsp; RadiusX=&#8221;2&#8243; RadiusY=&#8221;2&#8243; Fill=&#8221;Black&#8221; RenderTransformOrigin=&#8221;0.5,1&#8243;&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Rectangle.RenderTransform&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;TransformGroup&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;RotateTransform/&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;TranslateTransform Y=&#8221;0&#8243; X=&#8221;123&#8243;/&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/TransformGroup&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Rectangle.RenderTransform&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Rectangle&gt;</p>
</blockquote>
<p>How we should convert values received to the angle to turn the clock hand. We can use only one converter here and bind to the message, according following formula</p>
<blockquote><p>HourAngle = (Hours * 30)+(12*Minutes/60);     <br />MinuteAngle = Minutes * 6; (it&#8217;s 360/60)      <br />SecondAngle = Seconds * 6;</p>
</blockquote>
<p>Let&#8217;s run it. Nothing happens. Why? The reason is, that even each property of the object changes, it does not trigger binding, due to the fact, that whole object has not been changed. In order to fix it, we should bind to each property. And in case of hours to Hours and Minutes properties both. But how to make my converter to be single and multi value converter? Simple &#8211; all this about interfaces. So, following converter will do the work</p>
<blockquote><p>public class TimeToAngleConverter : IValueConverter, IMultiValueConverter     <br />&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </p>
<p>public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)     <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (double)value * 6;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </p>
<p>public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)     <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return ((double)values[0] * 30) + (12 * (double)values[1] / 60);      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p>public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { throw new NotImplementedException(); } </p>
<p> public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { throw new NotImplementedException(); } </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</blockquote>
<p>Now binding expressions</p>
<blockquote><p>&lt;Rectangle Height=&#8221;100&#8243; Width=&#8221;20&#8243; RadiusX=&#8221;10&#8243; RadiusY=&#8221;10&#8243; Fill=&#8221;Black&#8221; RenderTransformOrigin=&#8221;0.5,1&#8243;&gt;     <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Rectangle.RenderTransform&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;TransformGroup&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;RotateTransform&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;RotateTransform.Angle&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;MultiBinding Converter=&#8221;{StaticResource timeToAngle}&#8221;&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Binding Path=&#8221;Hour&#8221;/&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Binding Path=&#8221;Minute&#8221;/&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/MultiBinding&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/RotateTransform.Angle&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/RotateTransform&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;TranslateTransform Y=&#8221;25&#8243; X=&#8221;115&#8243;/&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/TransformGroup&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Rectangle.RenderTransform&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Rectangle&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Rectangle Height=&#8221;125&#8243; Width=&#8221;10&#8243; RadiusX=&#8221;5&#8243; RadiusY=&#8221;5&#8243; Fill=&#8221;Black&#8221; RenderTransformOrigin=&#8221;0.5,1&#8243;&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Rectangle.RenderTransform&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;TransformGroup&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;RotateTransform Angle=&#8221;{Binding Path=Minute, Converter={StaticResource timeToAngle}}&#8221;/&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;TranslateTransform Y=&#8221;0&#8243; X=&#8221;120&#8243;/&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/TransformGroup&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Rectangle.RenderTransform&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Rectangle&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Rectangle Height=&#8221;125&#8243; Width=&#8221;4&#8243;&nbsp; RadiusX=&#8221;2&#8243; RadiusY=&#8221;2&#8243; Fill=&#8221;Black&#8221; RenderTransformOrigin=&#8221;0.5,1&#8243;&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Rectangle.RenderTransform&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;TransformGroup&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;RotateTransform Angle=&#8221;{Binding Path=Second, Converter={StaticResource timeToAngle}}&#8221;/&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;TranslateTransform Y=&#8221;0&#8243; X=&#8221;123&#8243;/&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/TransformGroup&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Rectangle.RenderTransform&gt;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Rectangle&gt;</p>
</blockquote>
<p>We done with graphics in WPF. Let&#8217;s start it over in Silverlight. We cannot do at least half of what has been done in WPF. MultiBinding is not supported, there is no implicitly templating and Transform class does not support Binding. What to do? Let&#8217;s remember old good world. </p>
<p>Set the control without using templates, then find resources and save references and set values explicitly (after subscribing to OnPropertyChanged event of cause. Other words, make binding with your own hands.</p>
<blockquote><p>void onLoaded(object s, RoutedEventArgs e)     <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mt = ((RotateTransform)FindName(&#8220;mTransform&#8221;));      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ht = ((RotateTransform)FindName(&#8220;hTransform&#8221;));      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; st = ((RotateTransform)FindName(&#8220;sTransform&#8221;));      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((NetTimeProvider)Resources["timeProvider"]).PropertyChanged += new PropertyChangedEventHandler(Page_PropertyChanged);      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RotateTransform mt, ht, st; </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void Page_PropertyChanged(object sender, PropertyChangedEventArgs e)     <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Binding to Transform does not supported (yet??);      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NetTimeProvider s = sender as NetTimeProvider;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dispatcher.BeginInvoke((SendOrPostCallback) delegate (object o)      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NetTimeProvider ntp = o as NetTimeProvider;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (e.PropertyName == &#8220;Hour&#8221;)      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ht.Angle = (ntp.Hour * 30) + (12 * ntp.Minute / 60); ;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else if (e.PropertyName == &#8220;Minute&#8221;)      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mt.Angle = ntp.Minute * 6;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else if (e.PropertyName == &#8220;Second&#8221;)      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; st.Angle = ntp.Second * 6;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }, s);      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</blockquote>
<p>Now, when we have layout for our Silverlight control, we should connect to distribution network server. Reuse the manager, used for Winforms and WPF? We can&#8217;t. Silverlight is subset of .NET framework, and it is not relays on it, so we have to write new network provider for Silverlight. UDP is not supported in Silverlight, thus we&#8217;ll use TCP networking. Let&#8217;s see what we have. WebRequest/WebResponse HttpWebRequest/HttpWebResponse &#8211; to use it &#8211; no. Our server neither HTTP nor Web server. We should use raw sockets in Silverlight. Socket class exists in System.Net dll for Silverlight, however it is very limited. Let&#8217;s make the connection. First of all, we should know what IP to connect. </p>
<p>Due to security restrictions we cannot do DNS queries in Silverlight. From the other hand we do not want to restrict it to hardcoded name or IP address. In application class of Silverlight we have very handy property, named DnsSafeHost (Application.Current.Host.Source.DnsSafeHost). So let&#8217;s use it. </p>
<p>What about ports? Can I use TCP socket for any port I want? No. This is another security restriction. The only port range able available for Silverlight is 4502-5432 (only 30 ports). So with those restrictions we&#8217;ll create the connection as following.</p>
<blockquote><p>socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);     <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DnsEndPoint ep = new DnsEndPoint(Application.Current.Host.Source.DnsSafeHost, 4502);      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SocketAsyncEventArgs args = new SocketAsyncEventArgs()      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RemoteEndPoint = ep      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; args.Completed += onConnected;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; socket.ConnectAsync(args);</p>
</blockquote>
<p>Now we should check if the connection is established successfully. The only place we can do it is in onConnected handler. Here also we&#8217;ll reuse completed event of SocketAsyncArgs to perform read sequence. Upon the end of the handler we&#8217;ll try to read something from inline socket.</p>
<blockquote><p>void onConnected(object sender, SocketAsyncEventArgs e)     <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (e.SocketError == SocketError.Success)      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.Completed -= onConnected;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.Completed += onRead;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Message = &#8220;Connected&#8221;;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; readMoreData(e);     <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</blockquote>
<p>If you remember in regular framework we can wait on socket. We can do it as well in Silverlight. </p>
<blockquote><p>void readMoreData(SocketAsyncEventArgs e)     <br />{      <br />&nbsp;&nbsp;&nbsp; e.SetBuffer(buffer, bytesRead, (buffer.Length &#8211; bytesRead));      <br />&nbsp;&nbsp;&nbsp; if (!socket.ReceiveAsync(e))      <br />&nbsp;&nbsp;&nbsp; {      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; onRead(socket, e);      <br />&nbsp;&nbsp;&nbsp; }      <br />&nbsp;&nbsp;&nbsp; else      <br />&nbsp;&nbsp;&nbsp; {      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Message = &#8220;Disconnected&#8221;;      <br />&nbsp;&nbsp;&nbsp; } </p>
<p>}</p>
</blockquote>
<p>So, if everything is ok and we have data in the socket, let&#8217;s read it. There is some fault proofing should be done in it. First we should check if we go all the message. We know, that the message size is 20 bytes (5 integers &#8211; we check first four). Then we should check, that the message we got is our message. So in the header we&#8217;ll check for Magic number. Then if it&#8217;s ok we&#8217;ll parse it and fill all properties of our class.</p>
<blockquote><p>void onRead(object sender, SocketAsyncEventArgs e)     <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (e.BytesTransferred &gt; 0)      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bytesRead += e.BytesTransferred;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (bytesRead == 20 &amp;&amp; BitConverter.ToUInt32(buffer, 0) == Magic)      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Hour = BitConverter.ToInt32(buffer, 4);      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OnPropertyChanged(&#8220;Hour&#8221;);      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Minute = BitConverter.ToInt32(buffer, 8);      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OnPropertyChanged(&#8220;Minute&#8221;);      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Second = BitConverter.ToInt32(buffer, 12);      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OnPropertyChanged(&#8220;Second&#8221;);      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bytesRead = 0;      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; readMoreData(e);      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }      <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</blockquote>
<p>If everything fine, we&#8217;ll return to wait for next message to arrive. </p>
<p>We done. WinForms, WPF and Silverlight speaks over the network one with another, by using TCP and UDP raw sockets. What can be better? When our message is not just clock information, but something related to gaming world (e.g. current position or state of other network players). What am I speaking about? Come to reveal it on my session <span style="font-size: 10pt; color: rgb(0, 32, 96);">DEV335</span><span style="font-size: 10pt; color: rgb(23, 54, 93);">: </span><span style="font-size: 10pt; color: rgb(0, 32, 96);"><span style="color: rgb(0, 32, 96);"><a href="/blogs/tamir/archive/tags/teched/default.aspx" mce_href="/blogs/tamir/archive/tags/teched/default.aspx">Game Development Using<br />
Microsoft’s Latest Technologies</a> </span></span>in <a href="http://www.microsoft.com/israel/teched2008/index.aspx" target="_blank" mce_href="http://www.microsoft.com/israel/teched2008/index.aspx">TechEd Israel</a>.</p>
<p>Have a nice day and be good people.</p>
<p><a href="/blogs/tamir/attachment/68770.ashx" title="Download full source code for this article" mce_href="/blogs/tamir/attachment/68770.ashx">Source code for this article</a>.</p>


<p>Related posts:<ol><li><a href='http://khason.net/dev/read-singleton-approach-in-wpf-application/' rel='bookmark' title='Permanent Link: Real singleton approach in WPF application'>Real singleton approach in WPF application</a></li>
<li><a href='http://khason.net/dev/inotifypropertychanged-auto-wiring-or-how-to-get-rid-of-redundant-code/' rel='bookmark' title='Permanent Link: INotifyPropertyChanged auto wiring or how to get rid of redundant code'>INotifyPropertyChanged auto wiring or how to get rid of redundant code</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://khason.net/blog/networking-raw-sockets-in-silverlight-and-wpf-messaging-in-general/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
