<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Kiener&#039;s Blog &#187; Silverlight</title>
	<atom:link href="http://blog.thekieners.com/tag/silverlight/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.thekieners.com</link>
	<description>Blog of Beat Kiener, containing my experiences, thoughts, comments and questions</description>
	<lastBuildDate>Thu, 26 Jan 2012 02:01:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.thekieners.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Kiener&#039;s Blog &#187; Silverlight</title>
		<link>http://blog.thekieners.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.thekieners.com/osd.xml" title="Kiener&#039;s Blog" />
	<atom:link rel='hub' href='http://blog.thekieners.com/?pushpress=hub'/>
		<item>
		<title>Video about my first Windows Phone 7 App in the marketplace</title>
		<link>http://blog.thekieners.com/2010/12/01/video-about-my-first-windows-phone-7-app-in-the-marketplace/</link>
		<comments>http://blog.thekieners.com/2010/12/01/video-about-my-first-windows-phone-7-app-in-the-marketplace/#comments</comments>
		<pubDate>Wed, 01 Dec 2010 21:59:40 +0000</pubDate>
		<dc:creator>beatkiener</dc:creator>
				<category><![CDATA[Phone7]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">https://bkiener.wordpress.com/2010/12/01/video-about-my-first-windows-phone-7-app-in-the-marketplace/</guid>
		<description><![CDATA[It demonstrates the access to our backend system called “evidence” and allows to browse online through different data objects. Check out the video (put sound on…)<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.thekieners.com&amp;blog=7436128&amp;post=512&amp;subd=bkiener&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>It demonstrates the access to our backend system called “evidence” and allows to browse online through different data objects.</p>
<p>Check out the video (put sound on…)</p>
<p><a href="http://files.thekieners.com/blogcontent/2010/WindowsPhone7"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="VideoWindowsPhone" border="0" alt="VideoWindowsPhone" src="http://bkiener.files.wordpress.com/2010/12/videowindowsphone.png?w=218&#038;h=360" width="218" height="360" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bkiener.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bkiener.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bkiener.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bkiener.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bkiener.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bkiener.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bkiener.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bkiener.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bkiener.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bkiener.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bkiener.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bkiener.wordpress.com/512/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bkiener.wordpress.com/512/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bkiener.wordpress.com/512/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.thekieners.com&amp;blog=7436128&amp;post=512&amp;subd=bkiener&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.thekieners.com/2010/12/01/video-about-my-first-windows-phone-7-app-in-the-marketplace/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/bbdffd0b655e44e12898217ff1973ad3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">beatkiener</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/12/videowindowsphone.png" medium="image">
			<media:title type="html">VideoWindowsPhone</media:title>
		</media:content>
	</item>
		<item>
		<title>Expression Blend Samples not working with Silverlight 4 and WPF 4</title>
		<link>http://blog.thekieners.com/2010/11/09/expression-blend-samples-not-working-with-silverlight-4-and-wpf-4/</link>
		<comments>http://blog.thekieners.com/2010/11/09/expression-blend-samples-not-working-with-silverlight-4-and-wpf-4/#comments</comments>
		<pubDate>Tue, 09 Nov 2010 16:00:24 +0000</pubDate>
		<dc:creator>beatkiener</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">https://bkiener.wordpress.com/2010/11/09/expression-blend-samples-not-working-with-silverlight-4-and-wpf-4/</guid>
		<description><![CDATA[This blog post is just to document my experience with the Expression Blend Sample source together with Silverlight 4 or WPF 4.0 If you are using the Expression Blend Samples from codeplex (http://expressionblend.codeplex.com) then you might experience some problems when using the included triggers and behavior within SL4 or WPF4 runtime. The problem is that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.thekieners.com&amp;blog=7436128&amp;post=507&amp;subd=bkiener&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This blog post is just to document my experience with the Expression Blend Sample source together with Silverlight 4 or WPF 4.0</p>
<p>If you are using the Expression Blend Samples from codeplex (<a href="http://expressionblend.codeplex.com" target="_blank">http://expressionblend.codeplex.com</a>) then you might experience some problems when using the included triggers and behavior within SL4 or WPF4 runtime. The problem is that a dependency property with the type <em>System.Windows.Data.Binding</em> is not set to due to a change in the <em>MS.Internal.Data.DynamicValueConverter</em> class. Since SL4 / WPF4 the <em>DynamicValueConverter</em> uses the value of the binding instead of the binding instance itself. As example, the Command property of the <em>InvokeDataCommand</em> trigger is declared as <em>System.Windows.Data.Binding</em>, but the <em>DynamicValueConverter</em> will return the value of the binding, an ICommand compatible instance, and not the binding instance itself. So, the command property will always be null, because <em>DynamicValueConverter</em> cannot cast the object. And if the command binding cannot be set, the BindingListener, which is used internally, will not work correctly.</p>
<p><a href="http://bkiener.files.wordpress.com/2010/11/image.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://bkiener.files.wordpress.com/2010/11/image_thumb.png?w=692&#038;h=465" width="692" height="465" /></a></p>
<p>If we change the binding target from a dependency property to a simple CLR property, then the <em>DynamicValueConverter</em> can set the property and <em>InvokeDataCommand</em> works as before.</p>
<p><a href="http://bkiener.files.wordpress.com/2010/11/image1.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://bkiener.files.wordpress.com/2010/11/image_thumb1.png?w=442&#038;h=178" width="442" height="178" /></a></p>
<p>I’ve found that codeplex user <a href="http://www.codeplex.com/site/users/view/AvdMeulen">AvdMeulen</a> changed the whole Expression sample source and submitted a zip package on codeplex. See link below.</p>
<p>&#160;</p>
<p>Let’s go into more details:</p>
<p>Silverlight 3 does not allow using data binding on types which are NOT inherited from class <em>FrameworkElement</em>. That means that the entire Silverlight standard behaviors and triggers do not support data binding, because the Trigger and Behavior classes are directly derived from <em>DepedencyObject</em>. Nevertheless, if you use data-binding with a trigger- or behavior property in Silverlight 3, then the binding system does not execute the binding, but it tries to set the binding instance itself to the property as a value of type <em>System.Windows.Data.Binding</em>. This will be done through the <em>MS.Internal.Data.DynamicValueConverter</em> class. If the target property is not of type <em>System.Windows.Data.Binding</em> it will not work and you get an <em>AG_E_PARSER_BAD_PROPERTY_VALUE</em> exception from the XAML parser. Based on this binding-engine behavior, <a href="http://blois.us/blog/2009/04/datatrigger-bindings-on-non.html" target="_blank">Pete Blois</a> invented the <em>BindingListener</em> class.     <br />His trick was to declare properties on behaviors and triggers as type of <em>System.Windows.Data.Binding</em> in order to get them bindable. The binding is still not executed by the binding system and to bind them he declared a dummy attached dependency property and set the binding instance to the attached dependency property. The result is that the attached dependency property can execute the binding and we can just listen to when the attached dependency property changes. This results in a full functional binding for non-<em>FrameworkElements</em>. All this is encapsulated in the <em>BindingListener</em> class.</p>
<p>But now, why it does not work for SL4 anymore? The answer can be found in the <a href="http://msdn.microsoft.com/en-us/library/cc645049(VS.95).aspx" target="_blank">breaking changes for Silverlight 4</a>.</p>
<p><a href="http://bkiener.files.wordpress.com/2010/11/image2.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="image" border="0" alt="image" src="http://bkiener.files.wordpress.com/2010/11/image_thumb2.png?w=709&#038;h=494" width="709" height="494" /></a></p>
<p>&#160;</p>
<p>With SL4 it&#8217;s now possible to data binding dependency properties on triggers and behavior too, which makes the BindingListener class useless. This is also the reason why the Binding property remains null, because the SL4 binding engine set a BindingExpression and not the Binding itselfs and the BindingExpression tries to set the Command value instead of the Binding instance.</p>
<p>&#160;</p>
<p>Link resources:    <br /><a href="http://expressionblend.codeplex.com/workitem/8148">http://expressionblend.codeplex.com/workitem/8148</a>     <br /><a href="http://www.sztronka.com/2010/01/behaviors-triggers-and-memory-leaks-in.html">http://www.sztronka.com/2010/01/behaviors-triggers-and-memory-leaks-in.html</a>    <br /><a href="http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2010/03/27/silverlight-4-blend-4-mvvm-binding-dependencyobject.aspx">http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2010/03/27/silverlight-4-blend-4-mvvm-binding-dependencyobject.aspx</a>    <br /><a href="http://compiledexperience.com/blog/posts/blendable-mvvm-commands-and-behaviors">http://compiledexperience.com/blog/posts/blendable-mvvm-commands-and-behaviors</a>    <br /><a href="http://blois.us/blog/2009/04/datatrigger-bindings-on-non.html">http://blois.us/blog/2009/04/datatrigger-bindings-on-non.html</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bkiener.wordpress.com/507/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bkiener.wordpress.com/507/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bkiener.wordpress.com/507/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bkiener.wordpress.com/507/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bkiener.wordpress.com/507/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bkiener.wordpress.com/507/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bkiener.wordpress.com/507/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bkiener.wordpress.com/507/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bkiener.wordpress.com/507/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bkiener.wordpress.com/507/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bkiener.wordpress.com/507/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bkiener.wordpress.com/507/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bkiener.wordpress.com/507/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bkiener.wordpress.com/507/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.thekieners.com&amp;blog=7436128&amp;post=507&amp;subd=bkiener&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.thekieners.com/2010/11/09/expression-blend-samples-not-working-with-silverlight-4-and-wpf-4/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/bbdffd0b655e44e12898217ff1973ad3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">beatkiener</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/11/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/11/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/11/image_thumb2.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>RelativeSource Binding with FindAncestor mode in Silverlight</title>
		<link>http://blog.thekieners.com/2010/09/08/relativesource-binding-with-findancestor-mode-in-silverlight/</link>
		<comments>http://blog.thekieners.com/2010/09/08/relativesource-binding-with-findancestor-mode-in-silverlight/#comments</comments>
		<pubDate>Wed, 08 Sep 2010 04:46:27 +0000</pubDate>
		<dc:creator>beatkiener</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[MVVM]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">https://bkiener.wordpress.com/2010/09/08/relativesource-binding-with-findancestor-mode-in-silverlight/</guid>
		<description><![CDATA[Silverlight 3 introduced the RelativeSource Binding that is well known from WPF. Unfortunately Silverlight only supports the two modes Self and TemplatedParent. &#160; I don’t know why the FindAncestor mode is missing in Silverlight’s RelativeSource binding, but there is a real need for that in Silverlight too. I’m finding me quite often in a dead-end [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.thekieners.com&amp;blog=7436128&amp;post=502&amp;subd=bkiener&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Silverlight 3 introduced the <a href="http://msdn.microsoft.com/en-us/library/system.windows.data.binding.relativesource%28VS.95%29.aspx">RelativeSource</a> Binding that is well known from WPF. Unfortunately Silverlight only supports the two modes <em>Self</em> and <em>TemplatedParent</em>.</p>
<p><a href="http://bkiener.files.wordpress.com/2010/09/image.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://bkiener.files.wordpress.com/2010/09/image_thumb.png?w=624&#038;h=96" width="624" height="96" /></a>&#160;</p>
<p>I don’t know why the <em>FindAncestor</em> mode is missing in Silverlight’s RelativeSource binding, but there is a real need for that in Silverlight too. I’m finding me quite often in a dead-end ifI want to set a binding to a command or value in the view model within a list box item template. The problem in the example below is that the list box item has its own data context set to an item in the items-collection.</p>
<p><a href="http://bkiener.files.wordpress.com/2010/09/image1.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://bkiener.files.wordpress.com/2010/09/image_thumb1.png?w=715&#038;h=265" width="715" height="265" /></a> </p>
<p>In the case above I really need a relative binding to set a binding to the RemoveCommand and to the global Picklist collection as comboxbox source. On solution is to create a bidirectional relationship between the view models behind the scene in order to get a reference to the parent view model within the child view model. But this not the solution I was looking for. </p>
<p>After some googling I found a binding helper written by <a href="http://www.scottlogic.co.uk/blog/colin/2009/02/relativesource-binding-in-silverlight/" target="_blank">Colin Eberhardt</a> which enables it to use a relative binding with help of an attached dependency property. </p>
<p><a href="http://bkiener.files.wordpress.com/2010/09/clip_image0011.png"><img style="display:inline;border-width:0;" title="clip_image001" border="0" alt="clip_image001" src="http://bkiener.files.wordpress.com/2010/09/clip_image001_thumb1.png?w=600&#038;h=113" width="600" height="113" /></a> </p>
<p>Explained in brief: Colin uses an attached property to set his own relative source binding configuration for an specific element. When the attached property becomes attached to the target element it adds a handler for the elements loaded event. Within the event handler, he walks up the visual tree to find the specified ancestor and constructs a binding expression between the source and target properties. </p>
<p>Based on Colin’s idea I’ve created a similar implementation which provides some different behaviors: </p>
<ul>
<li>Support for a list of relative binding. This allows binding more than one property of an element. </li>
<li>It does not use a relay object in the middle of the binding. It is a pure binding without any custom code in between. This gain in a better performance, especially when using a storyboard to animate the data bound value. </li>
<li>I have added some special type of relative binding mode which is called as <i>ParentDataContext</i> mode. In that mode it walks up the visual tree until it finds a new data context. This is very helpful when having templates in an ItemsControl, ListBox or DataGrid. </li>
<li>Support for non-dependency property as source, e.g. direct usage of view model properties. </li>
<li>Support for property path syntax for the source property, e.g. <em>DataContext.Person.Name</em> </li>
<li>The AncestorType allows to set a base class as type criteria instead of the concrete type, e.g. if the concrete element is of type Grid, then the AncestorType can be set to Panel. </li>
<li>Allows to set the relative binding for attached properties too, e.g. to bind TooltipService.Tooltip property. </li>
<li>Allows to use <em>OneWay</em> or <em>OneTime</em> binding mode. </li>
<li>Allows to set any other binding parameter such as <em>ValidatesOnNotifyDataErrors</em>, <em>ValidatesOnExceptions</em>, <em>ValidatesOnDataErrors</em>, <em>NotifyOnValidationError</em> and <em>ConverterCulture.</em> </li>
<li>Enables to set the XAML namespace for controls which are not in the core-control assembly. </li>
</ul>
<h2>How to use</h2>
<p>Let’s have a look at some examples.</p>
<p>1. Simply relative binding with ancestor type </p>
<p><strong></strong></p>
</p>
<pre><span style="color:#0000ff;">&lt;</span><span style="color:#800000;">ComboBox</span><span style="color:#0000ff;">&gt;</span>
    <span style="color:#0000ff;">&lt;</span><span style="color:#c71585;">local</span>:<span style="color:#800000;">BindingHelper.Binding</span><span style="color:#0000ff;">&gt;</span>
            <span style="color:#0000ff;">&lt;</span><span style="color:#c71585;">local</span>:<span style="color:#800000;">RelativeSourceBinding</span> <span style="color:#ff0000;">Path</span>=<span style="color:#0000ff;">&quot;DataContext.Picklist&quot;</span>
                    <span style="color:#ff0000;">TargetProperty</span>=<span style="color:#0000ff;">&quot;ItemsSource&quot;</span> <span style="color:#ff0000;">RelativeMode</span>=<span style="color:#0000ff;">&quot;FindAncestor&quot;</span>
                    <span style="color:#ff0000;">AncestorType</span>=<span style="color:#0000ff;">&quot;UserControl&quot;</span> <span style="color:#0000ff;">/&gt;</span>
    <span style="color:#0000ff;">&lt;/</span><span style="color:#c71585;">local</span>:<span style="color:#800000;">BindingHelper.Binding</span><span style="color:#0000ff;">&gt;</span>
<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">ComboBox</span><span style="color:#0000ff;">&gt;</span></pre>
<pre>&#160;</pre>
<p>&#160;</p>
<p>2. Bind two or more properties by adding a list of binding definitions</p>
<pre><span style="color:#0000ff;">&lt;</span><span style="color:#800000;">ComboBox</span><span style="color:#0000ff;">&gt;</span>
    <span style="color:#0000ff;">&lt;</span><span style="color:#c71585;">local</span>:<span style="color:#800000;">BindingHelper.Binding</span><span style="color:#0000ff;">&gt;</span>
        <span style="color:#0000ff;">&lt;</span><span style="color:#c71585;">local</span>:<span style="color:#800000;">BindingList</span><span style="color:#0000ff;">&gt;</span>
            <span style="color:#0000ff;">&lt;</span><span style="color:#c71585;">local</span>:<span style="color:#800000;">RelativeSourceBinding</span> <span style="color:#ff0000;">Path</span>=<span style="color:#0000ff;">&quot;DataContext.Picklist&quot;</span>
                    <span style="color:#ff0000;">TargetProperty</span>=<span style="color:#0000ff;">&quot;ItemsSource&quot;</span> <span style="color:#ff0000;">RelativeMode</span>=<span style="color:#0000ff;">&quot;FindAncestor&quot;</span>
                    <span style="color:#ff0000;">AncestorType</span>=<span style="color:#0000ff;">&quot;UserControl&quot;</span> <span style="color:#0000ff;">/&gt;</span>
            <span style="color:#0000ff;">&lt;</span><span style="color:#c71585;">local</span>:<span style="color:#800000;">RelativeSourceBinding</span> <span style="color:#ff0000;">Path</span>=<span style="color:#0000ff;">&quot;DataContext.Tooltip&quot;</span>
                    <span style="color:#ff0000;">TargetProperty</span>=<span style="color:#0000ff;">&quot;(ToolTipService.ToolTip)&quot;</span> <span style="color:#ff0000;">RelativeMode</span>=<span style="color:#0000ff;">&quot;FindAncestor&quot;</span>
                    <span style="color:#ff0000;">AncestorType</span>=<span style="color:#0000ff;">&quot;UserControl&quot;</span> <span style="color:#0000ff;">/&gt;</span>
        <span style="color:#0000ff;">&lt;/</span><span style="color:#c71585;">local</span>:<span style="color:#800000;">BindingList</span><span style="color:#0000ff;">&gt;</span>
    <span style="color:#0000ff;">&lt;/</span><span style="color:#c71585;">local</span>:<span style="color:#800000;">BindingHelper.Binding</span><span style="color:#0000ff;">&gt;</span>
<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">ComboBox</span><span style="color:#0000ff;">&gt;</span></pre>
<p>&#160;</p>
<p>&#160;</p>
<p>3. Bind any attached dependency property</p>
<pre><span style="color:#0000ff;">&lt;</span><span style="color:#c71585;">local</span>:<span style="color:#800000;">RelativeSourceBinding</span> <span style="color:#ff0000;">Path</span>=<span style="color:#0000ff;">&quot;DataContext.Tooltip&quot;</span>
        <span style="color:#ff0000;">TargetProperty</span>=<span style="color:#0000ff;">&quot;(ToolTipService.ToolTip)&quot;</span> <span style="color:#ff0000;">RelativeMode</span>=<span style="color:#0000ff;">&quot;FindAncestor&quot;</span>
        <span style="color:#ff0000;">AncestorType</span>=<span style="color:#0000ff;">&quot;UserControl&quot;</span> <span style="color:#0000ff;">/&gt;</span></pre>
<p>&#160;</p>
<p>&#160;</p>
<p>4. Bind an attached dependency property from any assembly. Just specify the XAML namespace as in the example below</p>
<pre><span style="color:#0000ff;">&lt;</span><span style="color:#c71585;">local</span>:<span style="color:#800000;">RelativeSourceBinding</span> <span style="color:#ff0000;">Path</span>=<span style="color:#0000ff;">&quot;DataContext.Tooltip&quot;</span>
        <span style="color:#ff0000;">TargetProperty</span>=<span style="color:#0000ff;">&quot;(DemoAttachedElement.Value)&quot;</span>
        <span style="color:#ff0000;">TargetNamespace</span>=<span style="color:#0000ff;">&quot;clr-namespace:RelativeSourceBindingDemo;assembly=RelativeSourceBindingDemo&quot;</span>
        <span style="color:#ff0000;">RelativeMode</span>=<span style="color:#0000ff;">&quot;FindAncestor&quot;</span> <span style="color:#ff0000;">AncestorType</span>=<span style="color:#0000ff;">&quot;UserControl&quot;</span> <span style="color:#0000ff;">/&gt;</span></pre>
<p>&#160;</p>
<p>&#160;</p>
<p>5. Use the parent data context instead an ancestor type</p>
<pre><span style="color:#0000ff;">&lt;</span><span style="color:#c71585;">local</span>:<span style="color:#800000;">RelativeSourceBinding</span> <span style="color:#ff0000;">Path</span>=<span style="color:#0000ff;">&quot;RemoveCommand&quot;</span> <span style="color:#ff0000;">TargetProperty</span>=<span style="color:#0000ff;">&quot;Command&quot;</span>
        <span style="color:#ff0000;">RelativeMode</span>=<span style="color:#0000ff;">&quot;ParentDataContext&quot;</span> <span style="color:#0000ff;">/&gt;</span></pre>
<p>&#160;</p>
<p>&#160;</p>
<p>6. The mode <em>ParentDataContext</em> is set as default behavior. So in most cases you can just write xaml as following: </p>
<pre><span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Button</span> <span style="color:#ff0000;">Content</span>=<span style="color:#0000ff;">&quot;Remove&quot;</span> <span style="color:#ff0000;">CommandParameter</span>=<span style="color:#0000ff;">&quot;{Binding}&quot;</span><span style="color:#0000ff;">&gt;</span>
    <span style="color:#0000ff;">&lt;</span><span style="color:#c71585;">local</span>:<span style="color:#800000;">BindingHelper.Binding</span><span style="color:#0000ff;">&gt;</span>
        <span style="color:#0000ff;">&lt;</span><span style="color:#c71585;">local</span>:<span style="color:#800000;">RelativeSourceBinding</span> <span style="color:#ff0000;">Path</span>=<span style="color:#0000ff;">&quot;RemoveCommand&quot;</span> <span style="color:#ff0000;">TargetProperty</span>=<span style="color:#0000ff;">&quot;Command&quot;</span> <span style="color:#0000ff;">/&gt;</span>
    <span style="color:#0000ff;">&lt;/</span><span style="color:#c71585;">local</span>:<span style="color:#800000;">BindingHelper.Binding</span><span style="color:#0000ff;">&gt;</span>
<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Button</span><span style="color:#0000ff;">&gt;</span>

<span style="color:#0000ff;">&lt;</span><span style="color:#800000;">ComboBox</span> <span style="color:#ff0000;">Grid</span>.<span style="color:#ff0000;">Column</span>=<span style="color:#0000ff;">&quot;1&quot;</span><span style="color:#0000ff;">&gt;</span>
    <span style="color:#0000ff;">&lt;</span><span style="color:#c71585;">local</span>:<span style="color:#800000;">BindingHelper.Binding</span><span style="color:#0000ff;">&gt;</span>
        <span style="color:#0000ff;">&lt;</span><span style="color:#c71585;">local</span>:<span style="color:#800000;">BindingList</span><span style="color:#0000ff;">&gt;</span>
            <span style="color:#0000ff;">&lt;</span><span style="color:#c71585;">local</span>:<span style="color:#800000;">RelativeSourceBinding</span> <span style="color:#ff0000;">Path</span>=<span style="color:#0000ff;">&quot;Picklist&quot;</span> <span style="color:#ff0000;">TargetProperty</span>=<span style="color:#0000ff;">&quot;ItemsSource&quot;</span><span style="color:#0000ff;">/&gt;</span>
            <span style="color:#0000ff;">&lt;</span><span style="color:#c71585;">local</span>:<span style="color:#800000;">RelativeSourceBinding</span> <span style="color:#ff0000;">Path</span>=<span style="color:#0000ff;">&quot;Tooltip&quot;</span> <span style="color:#ff0000;">TargetProperty</span>=<span style="color:#0000ff;">&quot;(ToolTipService.ToolTip)&quot;</span> <span style="color:#0000ff;">/&gt;</span>
        <span style="color:#0000ff;">&lt;/</span><span style="color:#c71585;">local</span>:<span style="color:#800000;">BindingList</span><span style="color:#0000ff;">&gt;</span>
    <span style="color:#0000ff;">&lt;/</span><span style="color:#c71585;">local</span>:<span style="color:#800000;">BindingHelper.Binding</span><span style="color:#0000ff;">&gt;</span>
<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">ComboBox</span><span style="color:#0000ff;">&gt;</span></pre>
<p>&#160;</p>
<p>&#160;</p>
<p>7. Use a converter and other binding settings</p>
<pre><span style="color:#0000ff;">&lt;</span><span style="color:#c71585;">local</span>:<span style="color:#800000;">RelativeSourceBinding</span> <span style="color:#ff0000;">Path</span>=<span style="color:#0000ff;">&quot;Application.IsEnabled&quot;</span>
        <span style="color:#ff0000;">TargetProperty</span>=<span style="color:#0000ff;">&quot;Visibility&quot;</span> <span style="color:#ff0000;">Converter</span>=<span style="color:#0000ff;">&quot;{StaticResource VisibilityConverter}&quot;</span>
        <span style="color:#ff0000;">ValidatesOnNotifyDataErrors</span>=<span style="color:#0000ff;">&quot;True&quot;</span> <span style="color:#ff0000;">ValidatesOnExceptions</span>=<span style="color:#0000ff;">&quot;True&quot;</span>
        <span style="color:#ff0000;">ValidatesOnDataErrors</span>=<span style="color:#0000ff;">&quot;True&quot;</span> <span style="color:#ff0000;">NotifyOnValidationError</span>=<span style="color:#0000ff;">&quot;True&quot;</span> <span style="color:#0000ff;">/&gt;</span></pre>
<p>&#160;</p>
<h2>Source Code &amp; Demo Project</h2>
<p><a href="http://files.thekieners.com/blogcontent/2010/RelativeSourceBinding.zip" target="_blank">Here you can find the full source code within a demo project.</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bkiener.wordpress.com/502/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bkiener.wordpress.com/502/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bkiener.wordpress.com/502/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bkiener.wordpress.com/502/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bkiener.wordpress.com/502/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bkiener.wordpress.com/502/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bkiener.wordpress.com/502/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bkiener.wordpress.com/502/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bkiener.wordpress.com/502/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bkiener.wordpress.com/502/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bkiener.wordpress.com/502/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bkiener.wordpress.com/502/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bkiener.wordpress.com/502/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bkiener.wordpress.com/502/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.thekieners.com&amp;blog=7436128&amp;post=502&amp;subd=bkiener&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.thekieners.com/2010/09/08/relativesource-binding-with-findancestor-mode-in-silverlight/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/bbdffd0b655e44e12898217ff1973ad3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">beatkiener</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/09/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/09/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/09/clip_image001_thumb1.png" medium="image">
			<media:title type="html">clip_image001</media:title>
		</media:content>
	</item>
		<item>
		<title>Type.GetType implementation with help of XamlReader</title>
		<link>http://blog.thekieners.com/2010/09/06/type-gettype-implementation-with-help-of-xamlreader/</link>
		<comments>http://blog.thekieners.com/2010/09/06/type-gettype-implementation-with-help-of-xamlreader/#comments</comments>
		<pubDate>Mon, 06 Sep 2010 15:37:57 +0000</pubDate>
		<dc:creator>beatkiener</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">https://bkiener.wordpress.com/2010/09/06/type-gettype-implementation-with-help-of-xamlreader/</guid>
		<description><![CDATA[The Type.GetType method is different in Silverlight than in the standard .NET runtime. In Silverlight we must provide the fully qualified assembly name to get a type from an assembly. Only built in controls such as Button, Grid, ListBox, etc. or types in the executing assembly are excluded from this rule. Fully qualified assembly name [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.thekieners.com&amp;blog=7436128&amp;post=495&amp;subd=bkiener&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The Type.GetType method is different in Silverlight than in the standard .NET runtime. In Silverlight we must provide the fully qualified assembly name to get a type from an assembly. Only built in controls such as Button, Grid, ListBox, etc. or types in the executing assembly are excluded from this rule. Fully qualified assembly name means you must provide the version, culture and public key token. That means that the following GetType usage works well, until the target assembly version changes from 1.0 to 1.1.</p>
<pre>Type.GetType(&quot;<span style="color:#8b0000;">MyComponent.MyType, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4bec85d7bec6698f</span>&quot;)</pre>
<pre>&#160;</pre>
<p>And do we really want to have such Xaml or code behind files?</p>
<pre>&lt;local:MyCustomElement Grid.Row=&quot;<span style="color:#8b0000;">2</span>&quot; Grid.Column=&quot;<span style="color:#8b0000;">1</span>&quot;
   ItemType=&quot;<span style="color:#8b0000;">MyComponent.MyType, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4bec85d7bec6698f</span>&quot; &gt;
&lt;/local:MyCustomElement&gt;</pre>
<p><font face="Courier New"></font></p>
<p>&#160;</p>
<h2>Iterate over Assembly Parts</h2>
<p>One option to load a type just by its type-name and assembly-name is to iterate over all loaded assemblies. The method <em>AppDomain.CurrentDomain.GetAssemblies()</em> does not exist in Silverlight, but we can iterate over all assembly parts that are included in the deployment, e.g. all assemblies that are in the XAP file. I’ve found a <a href="http://inquisitorjax.blogspot.com/2009/10/gettype-from-referenced-assembly-in.html" target="_blank">blog post from Malcolm Jack</a> in which he describes how to iterate over the parts to find a type by its name.</p>
<p>An this is how it works in brief: loop through each assembly part, getting the assembly, then use assembly.GetType to load the type.</p>
<p>This is the code this short explanation:</p>
<pre><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> Type GetAssemblyType(<span style="color:#0000ff;">string</span> assemblyName, <span style="color:#0000ff;">string</span> className)
{
    StreamResourceInfo info = Application.GetResourceStream(<span style="color:#0000ff;">new</span> Uri(assemblyName, UriKind.Relative));
    Assembly assembly = <span style="color:#0000ff;">new</span> AssemblyPart().Load(info.Stream);
    Type type = assembly.GetType(className);
    <span style="color:#0000ff;">return</span> type;
}

<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> Type GetAssemblyType(<span style="color:#0000ff;">string</span> className)
{
    Type type = <span style="color:#0000ff;">null</span>;
    <span style="color:#0000ff;">foreach</span> (AssemblyPart part <span style="color:#0000ff;">in</span> Deployment.Current.Parts)
    {
        type = GetAssemblyType(part.Source, className);
        <span style="color:#0000ff;">if</span> (type != <span style="color:#0000ff;">null</span>)
            <span style="color:#0000ff;">break</span>;
    }
    <span style="color:#0000ff;">return</span> type;
}</pre>
<p>&#160;</p>
<p>Unfortunately this solution has two drawbacks: </p>
<p>1. Dynamic loaded assemblies downloaded with help of a web client instance are not part of the deployment, e.g. the code cannot find types in those assemblies. You can add an additional part to the collection after each downloaded dll, but you do not have a guarantee this will be done.</p>
<pre>AssemblyPart part = <span style="color:#0000ff;">new</span> AssemblyPart();
Assembly assm = part.Load(e.Result);
Deployment.Current.Parts.Add(part);</pre>
<p>&#160;</p>
<p>2. The second issue is when using application library caching to reduce the XAP size. </p>
<p><a href="http://bkiener.files.wordpress.com/2010/09/clip_image001.png"><img style="display:inline;border-width:0;" title="clip_image001" border="0" alt="clip_image001" src="http://bkiener.files.wordpress.com/2010/09/clip_image001_thumb.png?w=305&#038;h=30" width="305" height="30" /></a> </p>
<p>If this option is enabled, it will change the application manifest and the XAP no longer includes those assemblies which do support assembly caching. See <a href="http://timheuer.com/blog/archive/2009/07/13/silverlight-3-cached-assembly-feature.aspx">Tim Heuer’s posts</a> to get more detail how it works. </p>
<p>The cached assemblies are now declared as external parts as you can see in the manifest file (AppManifest.xml):</p>
<pre>&lt;Deployment ... &gt;
  &lt;Deployment.Parts&gt;
     ...
  &lt;/Deployment.Parts&gt;
  &lt;Deployment.ExternalParts&gt;
    &lt;ExtensionPart Source=&quot;<span style="color:#8b0000;">System.Windows.Controls.Input.Toolkit.zip</span>&quot; /&gt;
    &lt;ExtensionPart Source=&quot;<span style="color:#8b0000;">System.Xml.Linq.zip</span>&quot; /&gt;
    &lt;ExtensionPart Source=&quot;<span style="color:#8b0000;">System.Windows.Controls.zip</span>&quot; /&gt;
    &lt;ExtensionPart Source=&quot;<span style="color:#8b0000;">System.Windows.Controls.Toolkit.zip</span>&quot; /&gt;
  &lt;/Deployment.ExternalParts&gt;
&lt;/Deployment&gt;</pre>
<p>&#160;</p>
<p>In the end when the application starts those external assemblies will still be downloaded before the main form is added to the visual tree.</p>
<p><a href="http://bkiener.files.wordpress.com/2010/09/clip_image002.png"><img style="display:inline;border-width:0;" title="clip_image002" border="0" alt="clip_image002" src="http://bkiener.files.wordpress.com/2010/09/clip_image002_thumb.png?w=572&#038;h=154" width="572" height="154" /></a> </p>
<p>Iterating over all external parts is not that easy as iterating over all assembly parts, because it needs to download the source again and this is an asynchron operation. </p>
<p>&#160; </p>
<h2>The Solution: Using the XamlReader</h2>
<p>I’ve found a solution by using the XamlReader to resolve types during parsing a xaml string. Unfortunately there is no StringToTypeConverter in Silverlight and the Xaml parser uses some hard coded logic to resolve System.Type properties only for certain type like Style and ControlTemplate. However, we can use a Style or ControlTemplate class and use the TargetType property to retrieve the type information. And these lines of code show how it works:</p>
<pre><span style="color:#008000;">// create xaml with a simply Style element and set the TargetType property with the provided type name</span>
<span style="color:#0000ff;">string</span> xaml = &quot;<span style="color:#8b0000;">&lt;Style xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' </span>&quot;;

<span style="color:#008000;">// set the xaml namesapce if provided</span>
<span style="color:#0000ff;">if</span> (!<span style="color:#0000ff;">string</span>.IsNullOrWhiteSpace(xamlNamespace))
{
    xaml += <span style="color:#0000ff;">string</span>.Format(&quot;<span style="color:#8b0000;">xmlns:tmp='{0}' TargetType='tmp:{1}' /&gt;</span>&quot;, xamlNamespace, className);
}
<span style="color:#0000ff;">else</span>
{
    <span style="color:#008000;">// Core controls such as Button, Grid, ListBox, etc do not need a namespace</span>
    xaml += <span style="color:#0000ff;">string</span>.Format(&quot;<span style="color:#8b0000;">TargetType='{0}' /&gt;</span>&quot;, className);
}

<span style="color:#008000;">// let the XamlParser load the type via the TargetType property </span>
Style style = XamlReader.Load(xaml) <span style="color:#0000ff;">as</span> Style;

<span style="color:#0000ff;">if</span> (style != <span style="color:#0000ff;">null</span>)
{
    Type targetType = style.TargetType;
    <span style="color:#0000ff;">return</span> targetType;
}</pre>
<p>&#160;</p>
<p>Xaml parsing is not the fastest way to get a type. Therefore I encapsulated the functionality into a small class which maintains a cache for types which are already parsed. </p>
<p>The class provides four different ways to get a type:</p>
<pre><span style="color:#008000;">// for types in the executing assembly or types in the core-control </span>
<span style="color:#008000;">// assembly such as Button, Grid, etc. Just provide the type name.</span>
TypeLoader.GetType(&quot;<span style="color:#8b0000;">Grid</span>&quot;); 

<span style="color:#008000;">// The type name with its xaml namespace</span>
TypeLoader.GetType(&quot;<span style="color:#8b0000;">Grid</span>&quot;, &quot;<span style="color:#8b0000;">clr-namespace:System.Windows.Controls;assembly=System.Windows</span>&quot;);

<span style="color:#008000;">// Type name, namespace and assembly name as separate parameters</span>
TypeLoader.GetType(&quot;<span style="color:#8b0000;">Grid</span>&quot;, &quot;<span style="color:#8b0000;">System.Windows.Controls</span>&quot;, &quot;<span style="color:#8b0000;">System.Windows</span>&quot;);

<span style="color:#008000;">// Or with the assembly qualified type name, without version, culture and public key token</span>
TypeLoader.GetType(&quot;<span style="color:#8b0000;">System.Windows.Controls.Grid, System.Windows</span>&quot;);</pre>
<p>&#160; </p>
<p>Here are some other examples:</p>
<pre><span style="color:#008000;">// *** toolkit controls ***</span>
<span style="color:#008000;">// returns null, because NumericUpDown is in toolkit and not in core control assembly</span>
<span style="color:#008000;">//TypeLoader.GetType(&quot;NumericUpDown&quot;); </span>
TypeLoader.GetType(&quot;<span style="color:#8b0000;">NumericUpDown</span>&quot;,
               &quot;<span style="color:#8b0000;">clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit</span>&quot;);
TypeLoader.GetType(&quot;<span style="color:#8b0000;">NumericUpDown</span>&quot;,
               &quot;<span style="color:#8b0000;">System.Windows.Controls</span>&quot;, &quot;<span style="color:#8b0000;">System.Windows.Controls.Input.Toolkit</span>&quot;);
TypeLoader.GetType(&quot;<span style="color:#8b0000;">System.Windows.Controls.NumericUpDown, System.Windows.Controls.Input.Toolkit</span>&quot;);

<span style="color:#008000;">// *** main application ***</span>
<span style="color:#008000;">// returns null, because MainPage is not in core-control assembly</span>
<span style="color:#008000;">//Type mainPage1 = TypeLoader.GetType(&quot;MainPage&quot;); </span>
TypeLoader.GetType(&quot;<span style="color:#8b0000;">MainPage</span>&quot;, &quot;<span style="color:#8b0000;">clr-namespace:TypeLoaderDemo;assembly=TypeLoaderDemo</span>&quot;);
TypeLoader.GetType(&quot;<span style="color:#8b0000;">MainPage</span>&quot;, &quot;<span style="color:#8b0000;">TypeLoaderDemo</span>&quot;, &quot;<span style="color:#8b0000;">TypeLoaderDemo</span>&quot;);
TypeLoader.GetType(&quot;<span style="color:#8b0000;">TypeLoaderDemo.MainPage, TypeLoaderDemo</span>&quot;);

<span style="color:#008000;">// *** 3rd party libs ***</span>
TypeLoader.GetType(&quot;<span style="color:#8b0000;">MyButton</span>&quot;, &quot;<span style="color:#8b0000;">clr-namespace:TypeLoaderControlLib;assembly=TypeLoaderControlLib</span>&quot;);
TypeLoader.GetType(&quot;<span style="color:#8b0000;">MyButton</span>&quot;, &quot;<span style="color:#8b0000;">TypeLoaderControlLib</span>&quot;, &quot;<span style="color:#8b0000;">TypeLoaderControlLib</span>&quot;);
TypeLoader.GetType(&quot;<span style="color:#8b0000;">TypeLoaderControlLib.MyButton,TypeLoaderControlLib</span>&quot;);</pre>
<pre>&#160;</pre>
<h2>Demo Project and Source Code</h2>
<pre><font face="Verdana"></font>&#160;</pre>
<p>Here you can find the <a href="http://files.thekieners.com/blogcontent/2010/TypeLoader.cs.txt" target="_blank">TypeLoader implementation</a> or a <a href="http://files.thekieners.com/blogcontent/2010/TypeLoaderDemo.zip" target="_blank">full demo project</a> including TypeLoader source, examples, assembly parts iteration and dynamic assembly loading.</p>
<pre>&#160;</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bkiener.wordpress.com/495/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bkiener.wordpress.com/495/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bkiener.wordpress.com/495/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bkiener.wordpress.com/495/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bkiener.wordpress.com/495/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bkiener.wordpress.com/495/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bkiener.wordpress.com/495/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bkiener.wordpress.com/495/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bkiener.wordpress.com/495/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bkiener.wordpress.com/495/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bkiener.wordpress.com/495/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bkiener.wordpress.com/495/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bkiener.wordpress.com/495/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bkiener.wordpress.com/495/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.thekieners.com&amp;blog=7436128&amp;post=495&amp;subd=bkiener&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.thekieners.com/2010/09/06/type-gettype-implementation-with-help-of-xamlreader/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/bbdffd0b655e44e12898217ff1973ad3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">beatkiener</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/09/clip_image001_thumb.png" medium="image">
			<media:title type="html">clip_image001</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/09/clip_image002_thumb.png" medium="image">
			<media:title type="html">clip_image002</media:title>
		</media:content>
	</item>
		<item>
		<title>Listening to DependencyProperty changes in Silverlight</title>
		<link>http://blog.thekieners.com/2010/08/27/listening-to-dependencyproperty-changes-in-silverlight/</link>
		<comments>http://blog.thekieners.com/2010/08/27/listening-to-dependencyproperty-changes-in-silverlight/#comments</comments>
		<pubDate>Fri, 27 Aug 2010 14:20:59 +0000</pubDate>
		<dc:creator>beatkiener</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">https://bkiener.wordpress.com/2010/08/27/listening-to-dependencyproperty-changes-in-silverlight/</guid>
		<description><![CDATA[The dependency property system is a pretty nice concept. Receiving notifications for dependency property changes on an existing object is a very common scenario in order to update my view model or the UI. This is quite easy in WPF: // get the property descriptor DependencyPropertyDescriptor prop = DependencyPropertyDescriptor.FromProperty(TextBox.TextProperty, myTextBox.GetType()); // add change handler prop.AddValueChanged(myTextBox, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.thekieners.com&amp;blog=7436128&amp;post=486&amp;subd=bkiener&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The dependency property system is a pretty nice concept. Receiving notifications for dependency property changes on an existing object is a very common scenario in order to update my view model or the UI.</p>
<p>This is quite easy in WPF:</p>
<pre><span style="color:#008000;">// get the property descriptor</span>
DependencyPropertyDescriptor prop =
         DependencyPropertyDescriptor.FromProperty(TextBox.TextProperty, myTextBox.GetType());
<span style="color:#008000;">// add change handler</span>
prop.AddValueChanged(myTextBox, (sender, args) =&gt;
{

});</pre>
<p>Unfortunately Silverlight has a limited set of meta-data functionality around the dependency property system, because the <em>DependencyPropertyDescriptor</em> does exist in Silverlight.</p>
<p>In order to get a workaround I found a solution in which I get notified with help of the binding system. I simple use a relay object which value property is bound to the source property I want to get notified. The relay object contains a public event which raises when the value changes.</p>
<p><a href="http://bkiener.files.wordpress.com/2010/08/image2.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://bkiener.files.wordpress.com/2010/08/image_thumb2.png?w=718&#038;h=254" width="718" height="254" /></a> </p>
<p>&#160;</p>
<p>I’ve put the implementation details into a <em>DependencyPropertyChangedListener </em>class to make it as simple as possible and similar to WPF. The lines below show how simple it is to create the listener and attaching the event.</p>
<pre><span style="color:#0000ff;">private</span> DependencyPropertyChangedListener listener;

<span style="color:#0000ff;">public</span> MainPage()
{
    InitializeComponent();

    listener = DependencyPropertyChangedListener.Create(<span style="color:#0000ff;">this</span>.myTextBox, &quot;<span style="color:#8b0000;">Text</span>&quot;);
    listener.ValueChanged += listener_ValueChanged;
}

<span style="color:#0000ff;">void</span> listener_ValueChanged(<span style="color:#0000ff;">object</span> sender, DependencyPropertyValueChangedEventArgs e)
{
    Debug.WriteLine(e.OldValue + &quot;<span style="color:#8b0000;"> </span>&quot; + e.NewValue);
}

<span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> btnSetValue_Click(<span style="color:#0000ff;">object</span> sender, RoutedEventArgs e)
{
    <span style="color:#0000ff;">this</span>.myTextBox.Text = DateTime.Now.Ticks.ToString();
}</pre>
</p>
<p>The listener provides the method Detach to release the binding.</p>
<pre>listener.Detach();</pre>
<h2>&#160;</h2>
<h2>Known Limitations</h2>
<p>It is not possible to listen for dependency property values which are inherited like the FontSize, DataContext and FontFamily properties so far.</p>
<p>&#160;</p>
<h2>Source code</h2>
<p><a href="http://bkiener.files.wordpress.com/2010/08/image4.png"><img style="border-bottom:0;border-left:0;display:inline;margin-left:0;border-top:0;margin-right:0;border-right:0;" title="image" border="0" alt="image" src="http://bkiener.files.wordpress.com/2010/08/image_thumb4.png?w=61&#038;h=64" width="61" height="64" /></a> </p>
<p><a href="http://files.thekieners.com/blogcontent/2010/DependencyPropertyChangeListener.zip" target="_blank">Here you can find a demo project with full source code.</a></p>
</p>
<p>&#160;</p>
<p><font color="#ffffff" size="1">Keywords: Registering to a Dependency Property Change Event Property OverrideMetadata PropertyDescriptor prop TypeDescriptor GetProperties Receive notifications for dependency properties</font></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bkiener.wordpress.com/486/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bkiener.wordpress.com/486/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bkiener.wordpress.com/486/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bkiener.wordpress.com/486/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bkiener.wordpress.com/486/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bkiener.wordpress.com/486/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bkiener.wordpress.com/486/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bkiener.wordpress.com/486/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bkiener.wordpress.com/486/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bkiener.wordpress.com/486/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bkiener.wordpress.com/486/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bkiener.wordpress.com/486/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bkiener.wordpress.com/486/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bkiener.wordpress.com/486/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.thekieners.com&amp;blog=7436128&amp;post=486&amp;subd=bkiener&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.thekieners.com/2010/08/27/listening-to-dependencyproperty-changes-in-silverlight/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/bbdffd0b655e44e12898217ff1973ad3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">beatkiener</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/08/image_thumb2.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/08/image_thumb4.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>.NET Reflector &#8211; Working with different profiles for Silverlight and the Core Framework</title>
		<link>http://blog.thekieners.com/2010/08/27/net-reflector-working-with-different-profiles-for-silverlight-and-the-core-framework/</link>
		<comments>http://blog.thekieners.com/2010/08/27/net-reflector-working-with-different-profiles-for-silverlight-and-the-core-framework/#comments</comments>
		<pubDate>Fri, 27 Aug 2010 04:58:51 +0000</pubDate>
		<dc:creator>beatkiener</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Reflector]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">https://bkiener.wordpress.com/2010/08/27/net-reflector-working-with-different-profiles-for-silverlight-and-the-core-framework/</guid>
		<description><![CDATA[.NET Reflector is a one of the 10 Must-Have utilities for .NET developers. If you need to work with more than one version of the .NET Framework, especially with Silverlight and the Core Framework (3.5 or 4.0) you will find you reconfiguring your Reflector every time you changing the platform. For me this is very [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.thekieners.com&amp;blog=7436128&amp;post=480&amp;subd=bkiener&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.red-gate.com/products/reflector/index.htm" target="_blank">.NET Reflector</a> is a one of the 10 Must-Have utilities for .NET developers. If you need to work with more than one version of the .NET Framework, especially with Silverlight and the Core Framework (3.5 or 4.0) you will find you reconfiguring your Reflector every time you changing the platform. For me this is very annoying tasks for which I was looking for a solution.</p>
<p>&#160;</p>
<h2>Different config files</h2>
<p>.NET Reflector stores it settings into a config structure which includes all assemblies from the left tree in the application.</p>
<p>Recently I noticed the ability to start .NET Reflector with command line arguments. One command line argument allows to specify a config file path:   <br /><em>&quot;C:\Program Files\Reflector\Reflector.exe&quot; /configuration:”Reflector.cfg”</em></p>
<p>Unfortunately there seems to be a bug in version 6.1.0.11. When closing Reflector it reports the following error:</p>
<p><a href="http://bkiener.files.wordpress.com/2010/08/clip_image001.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="clip_image001" border="0" alt="clip_image001" src="http://bkiener.files.wordpress.com/2010/08/clip_image001_thumb.png?w=504&#038;h=182" width="504" height="182" /></a></p>
<p>When changing the config-path to an absolute syntax then it works fine:   <br /><em>&quot;C:\Program Files\Reflector\Reflector.exe&quot; /configuration:”C:\Program Files\Reflector\Reflector.cfg”</em></p>
<p><em></em></p>
<h2>Creating different profiles</h2>
<p>First create different .NET Reflector shortcuts for each profile you want to have. In my case these are Silverlight, .NET 3.5 and .NET 4.0.</p>
<p><a href="http://bkiener.files.wordpress.com/2010/08/clip_image002.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="clip_image002" border="0" alt="clip_image002" src="http://bkiener.files.wordpress.com/2010/08/clip_image002_thumb.png?w=504&#038;h=173" width="504" height="173" /></a></p>
<p>Edit every shortcut and add the <i>/configuration</i> argument to every shortcut with a different name as config file. The config file must not exists at this time.</p>
<p>My shortcuts are as following:</p>
<ul>
<li><em>&quot;C:\Program Files\Reflector\Reflector.exe&quot; /configuration:&quot;C:\Program Files\Reflector\DOTNET40.cfg&quot;</em></li>
<li><em>&quot;C:\Program Files\Reflector\Reflector.exe&quot; /configuration:&quot;C:\Program Files\Reflector\DOTNET35.cfg&quot;</em></li>
<li><em>&quot;C:\Program Files\Reflector\Reflector.exe&quot; /configuration:&quot;C:\Program Files\Reflector\Silverlight.cfg&quot;</em> </li>
</ul>
<p><a href="http://bkiener.files.wordpress.com/2010/08/clip_image003.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="clip_image003" border="0" alt="clip_image003" src="http://bkiener.files.wordpress.com/2010/08/clip_image003_thumb.png?w=404&#038;h=565" width="404" height="565" /></a></p>
<p>Now start each profile with its shortcut. .NET Reflector will not find a config and asks you which default assembly list you want to use. Select the version matching to your shortcut and repeat this for every shortcut you have created.</p>
<p><a href="http://bkiener.files.wordpress.com/2010/08/clip_image004.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="clip_image004" border="0" alt="clip_image004" src="http://bkiener.files.wordpress.com/2010/08/clip_image004_thumb.png?w=504&#038;h=629" width="504" height="629" /></a></p>
<h2>&#160;</h2>
<h2>Existing Add-Ins</h2>
<p>.NET Reflector has a clear and simple config file structure. Therefore you can easily copy and paste your favorite Add-Ins from one config file to another config file.</p>
<p><a href="http://bkiener.files.wordpress.com/2010/08/image.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://bkiener.files.wordpress.com/2010/08/image_thumb.png?w=462&#038;h=117" width="462" height="117" /></a> </p>
<h2>&#160;</h2>
<h2>Improving startup</h2>
<p>Working with Vista/Windows 7 makes it very easy to find programs via the start menu. To use the program search, just put a copy of your shortcuts into your start menu folder (or a subfolder in it).</p>
<p><em>C:\ProgramData\Microsoft\Windows\Start Menu\Programs\MyStartMenuLinks</em></p>
<p><a href="http://bkiener.files.wordpress.com/2010/08/image1.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://bkiener.files.wordpress.com/2010/08/image_thumb1.png?w=716&#038;h=164" width="716" height="164" /></a> </p>
<p>Now you can type in “Reflector” into your start menu search box and you get a selection of different .NET Reflector profiles. </p>
<p><a href="http://bkiener.files.wordpress.com/2010/08/clip_image007.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="clip_image007" border="0" alt="clip_image007" src="http://bkiener.files.wordpress.com/2010/08/clip_image007_thumb.png?w=433&#038;h=263" width="433" height="263" /></a></p>
<p>For me these profiles are very helpful during my daily coding life.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bkiener.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bkiener.wordpress.com/480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bkiener.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bkiener.wordpress.com/480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bkiener.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bkiener.wordpress.com/480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bkiener.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bkiener.wordpress.com/480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bkiener.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bkiener.wordpress.com/480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bkiener.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bkiener.wordpress.com/480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bkiener.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bkiener.wordpress.com/480/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.thekieners.com&amp;blog=7436128&amp;post=480&amp;subd=bkiener&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.thekieners.com/2010/08/27/net-reflector-working-with-different-profiles-for-silverlight-and-the-core-framework/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/bbdffd0b655e44e12898217ff1973ad3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">beatkiener</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/08/clip_image001_thumb.png" medium="image">
			<media:title type="html">clip_image001</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/08/clip_image002_thumb.png" medium="image">
			<media:title type="html">clip_image002</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/08/clip_image003_thumb.png" medium="image">
			<media:title type="html">clip_image003</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/08/clip_image004_thumb.png" medium="image">
			<media:title type="html">clip_image004</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/08/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/08/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/08/clip_image007_thumb.png" medium="image">
			<media:title type="html">clip_image007</media:title>
		</media:content>
	</item>
		<item>
		<title>Optimize Data Contracts for better WCF Performance</title>
		<link>http://blog.thekieners.com/2010/05/04/optimize-data-contracts-for-better-wcf-performance/</link>
		<comments>http://blog.thekieners.com/2010/05/04/optimize-data-contracts-for-better-wcf-performance/#comments</comments>
		<pubDate>Tue, 04 May 2010 06:59:00 +0000</pubDate>
		<dc:creator>beatkiener</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">https://bkiener.wordpress.com/2010/05/04/optimize-data-contracts-for-better-wcf-performance/</guid>
		<description><![CDATA[Out of the box Silverlight provides two message encoding types for sending SOAP messages over the wire. These are TextEncoding and BinaryEncoding. MTOM encoding is still not available within Silverlight. Binary encoded messages produce a payload that is about half the size of a message encoded with standard text encoding. Please note that binary encoding [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.thekieners.com&amp;blog=7436128&amp;post=457&amp;subd=bkiener&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Out of the box Silverlight provides two message encoding types for sending SOAP messages over the wire. These are <i>TextEncoding</i> and <i>BinaryEncoding</i>. MTOM encoding is still not available within Silverlight.</p>
<p>Binary encoded messages produce a payload that is about half the size of a message encoded with standard text encoding. Please note that binary encoding is a Microsoft proprietary implementation and therefore not interoperable outside the .NET framework.</p>
<p>In our Silverlight applications we use a .NET to .NET messaging scenario (like many other SL apps too), which means Silverlight communicates with a WCF backend. Using then binary encoded messages is obvious.    <br />You will find a lot of blog posts out there in the community when you are interested in binary vs. text-encoding comparison.</p>
<h2>What exactly does BinaryEncoding?</h2>
<p><b></b></p>
<p>When the encoding is set to binary, then the <em>DataContractSerializer</em> still produces SOAP messages as XML but after that the messages get transformed into a binary representation of the XML with help of the <em>XmlDictionaryWriter</em> class.</p>
<p>The following code snippet shows the usage of the <em>XmlDictionaryWriter</em> class (not WCF related).</p>
<p><a href="http://bkiener.files.wordpress.com/2010/05/image.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://bkiener.files.wordpress.com/2010/05/image_thumb.png?w=713&#038;h=418" width="713" height="418" /></a> </p>
<p>You should not compare the <em>XmlDictionaryWriter</em> with the well-known <em>BinaryFormatter</em> in the mscorlib. The <em>BinaryFormatter</em> produces a binary representation of an object graph. The <em>XmlDictionaryWriter</em> on the other hand translates a textual XML into a binary representation. Serializing a XML string with the <em>BinaryFormatter</em> will not reduce the size, because the XML is just a single string from this point of view.</p>
<p>The textual output form the example above indicates that it is a kind of an optimized XML structure. </p>
<p><a href="http://bkiener.files.wordpress.com/2010/05/image1.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://bkiener.files.wordpress.com/2010/05/image_thumb1.png?w=828&#038;h=73" width="828" height="73" /></a> </p>
<p><b></b></p>
<h2>Optimize Data Contracts</h2>
<p>Binary XML still includes the names of the elements and attributes as plain text.</p>
<p>When setting the name of my data contracts to a single character then the SOAP message size will be reduced by additional 30-50 percent (depends on the data structure).</p>
<p><a href="http://bkiener.files.wordpress.com/2010/05/image2.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://bkiener.files.wordpress.com/2010/05/image_thumb2.png?w=363&#038;h=288" width="363" height="288" /></a> </p>
<p>&#160;</p>
<p>The SOAP-Envelop then look like this:</p>
<p><a href="http://bkiener.files.wordpress.com/2010/05/image3.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://bkiener.files.wordpress.com/2010/05/image_thumb3.png?w=468&#038;h=248" width="468" height="248" /></a> </p>
<p>Rules for setting the name: </p>
<ul>
<li>Each data contract must have a unique name in the scope of its namespace </li>
<li>Each data member must have a unique name in the scope of its class. </li>
<li>Use character [A-Za-z], you can mix upper and lower case. </li>
</ul>
<p>After these changes the generated proxy on client side is quite unhandy, because the classes and members are named like they are declared in the contracts. Therefore I created a shared lib and reuse the data contracts on client side.</p>
<p><a href="http://bkiener.files.wordpress.com/2010/05/image4.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://bkiener.files.wordpress.com/2010/05/image_thumb4.png?w=523&#038;h=344" width="523" height="344" /></a> </p>
<p>Assemblies built in Silverlight are in general not binary compatible with the .NET Framework, so if you want to share code you need to dual-compile your code. Since Silverlight 4 you will be able to use some Silverlight-based assemblies from within .NET 4. In order to load a Silverlight assembly in .NET, the assembly may only reference the following assemblies: mscorlib.dll, System, System.Core.dll, System.ComponentModel.Composition.dll, Microsoft.VisualBasic.dll. </p>
<p>Well, my shared data contracts dll needs a reference to System.Runtime.Serialization.dll which is currently not binary compatible with the Silverlight runtime. </p>
<p>Therefore I do a dual-compile by adding the same source file with “Add As Link” to my Silverlight library.</p>
<p><a href="http://bkiener.files.wordpress.com/2010/05/clip_image008.png"><img style="display:inline;border-width:0;" title="clip_image008" border="0" alt="clip_image008" src="http://bkiener.files.wordpress.com/2010/05/clip_image008_thumb.png?w=513&#038;h=401" width="513" height="401" /></a></p>
<p>As final task I add a reference to SharedDataContract.SL assembly and update my service reference. Please check that the option “Reuse types in referenced assemblies” is checked.</p>
<p><a href="http://bkiener.files.wordpress.com/2010/05/clip_image009.png"><img style="display:inline;border-width:0;" title="clip_image009" border="0" alt="clip_image009" src="http://bkiener.files.wordpress.com/2010/05/clip_image009_thumb.png?w=334&#038;h=66" width="334" height="66" /></a></p>
<p>&#160;</p>
<p><b></b></p>
<p><b></b></p>
<h2>Comparison of different configurations </h2>
<p><b></b></p>
<p><strong>Text encoding / standard contract</strong></p>
<p><a href="http://bkiener.files.wordpress.com/2010/05/image5.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://bkiener.files.wordpress.com/2010/05/image_thumb5.png?w=294&#038;h=192" width="294" height="192" /></a>&#160;</p>
<p>&#160;</p>
<p><strong>Text encoding / optimized contracts</strong></p>
<p><a href="http://bkiener.files.wordpress.com/2010/05/image6.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://bkiener.files.wordpress.com/2010/05/image_thumb6.png?w=297&#038;h=198" width="297" height="198" /></a>&#160;</p>
<p>&#160;</p>
<p><strong>Binary encoding / standard contracts</strong></p>
<p><a href="http://bkiener.files.wordpress.com/2010/05/image7.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://bkiener.files.wordpress.com/2010/05/image_thumb7.png?w=299&#038;h=173" width="299" height="173" /></a> </p>
<p>&#160;</p>
<p><strong>Binary encoding / optimized contracts</strong></p>
<p><a href="http://bkiener.files.wordpress.com/2010/05/image8.png"><img style="display:inline;border-width:0;" title="image" border="0" alt="image" src="http://bkiener.files.wordpress.com/2010/05/image_thumb8.png?w=300&#038;h=172" width="300" height="172" /></a> </p>
<h2>Summary</h2>
<p>When communicating in a .NET to .NET messaging scenario then optimizing the data contracts is an additional way to reduce the SOAP message size. This together with binary encoding will reduce the message size about 4 times. Especially looking to the Windows Phone 7 development this will help when the traffic goes over a low bandwidth network.</p>
<p>&#160;</p>
<h2>Demo Project</h2>
<p><a href="http://files.thekieners.com/blogcontent/2010/OptimizedWcfPerformance.zip" target="_blank">Download it here</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bkiener.wordpress.com/457/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bkiener.wordpress.com/457/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bkiener.wordpress.com/457/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bkiener.wordpress.com/457/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bkiener.wordpress.com/457/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bkiener.wordpress.com/457/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bkiener.wordpress.com/457/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bkiener.wordpress.com/457/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bkiener.wordpress.com/457/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bkiener.wordpress.com/457/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bkiener.wordpress.com/457/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bkiener.wordpress.com/457/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bkiener.wordpress.com/457/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bkiener.wordpress.com/457/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.thekieners.com&amp;blog=7436128&amp;post=457&amp;subd=bkiener&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.thekieners.com/2010/05/04/optimize-data-contracts-for-better-wcf-performance/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/bbdffd0b655e44e12898217ff1973ad3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">beatkiener</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/05/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/05/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/05/image_thumb2.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/05/image_thumb3.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/05/image_thumb4.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/05/clip_image008_thumb.png" medium="image">
			<media:title type="html">clip_image008</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/05/clip_image009_thumb.png" medium="image">
			<media:title type="html">clip_image009</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/05/image_thumb5.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/05/image_thumb6.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/05/image_thumb7.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/05/image_thumb8.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Opening a Microsoft Office documents with Silverlight</title>
		<link>http://blog.thekieners.com/2010/04/21/opening-an-microsoft-office-documents-with-silverlight/</link>
		<comments>http://blog.thekieners.com/2010/04/21/opening-an-microsoft-office-documents-with-silverlight/#comments</comments>
		<pubDate>Wed, 21 Apr 2010 08:37:09 +0000</pubDate>
		<dc:creator>beatkiener</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Office]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://bkiener.wordpress.com/2010/04/21/opening-an-microsoft-office-documents-with-silverlight/</guid>
		<description><![CDATA[Situation From our Silverlight application we must be able to view documents. Documents are stored in an existing document management system and are accessible through a WCF REST service. It sounds very easy, but it was quite hard to understand all the stuff going on behind the Internet Explorer and the Microsoft Office. Simplest solution [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.thekieners.com&amp;blog=7436128&amp;post=427&amp;subd=bkiener&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h2>Situation</h2>
<p>From our Silverlight application we must be able to view documents. Documents are stored in an existing document management system and are accessible through a WCF REST service. It sounds very easy, but it was quite hard to understand all the stuff going on behind the Internet Explorer and the Microsoft Office.</p>
<h2>Simplest solution</h2>
<p>Download the content via a WebClient instance and save it with help of the Silverlight SaveFileDialog to the users local disk. Unfortunately this simple solution provides not the expected user experience, because Silverlight is not able to set the file name for the SaveFileDialog and after saving the user must manually navigate to the local folder and open the document by hand. The user experience should like be “Click &amp; View” and not “Click, Safe, Search, Open &amp; View”.</p>
<h2>Expected solution</h2>
<p>Open a popup from the Silverlight application with the URL pointing to the document.</p>
<p>Via HtmlPage:</p>
<pre>Uri docUri = <span style="color:#0000ff;">new</span> Uri("<span style="color:#8b0000;">http://mydomain/docs/test.docx</span>");
HtmlPage.PopupWindow(docUri, "<span style="color:#8b0000;">windwow1</span>", <span style="color:#0000ff;">null</span>);</pre>
<p>Or via a HyperlinkButton:</p>
<pre><span style="color:#0000ff;">&lt;</span><span style="color:#800000;">HyperlinkButton</span> <span style="color:#ff0000;">Content</span>=<span style="color:#0000ff;">"open document"</span>
                <span style="color:#ff0000;">NavigateUri</span>=<span style="color:#0000ff;">"http://mydomain/docs/test.docx"</span>
                <span style="color:#ff0000;">TargetName</span>=<span style="color:#0000ff;">"_blank"</span> <span style="color:#0000ff;">/&gt;</span></pre>
<h2>The service part</h2>
<p>We deliver documents through a simple WCF REST service from our middle layer infrastructure. We use a custom authentication system that relies on HTTP session cookies for client identification. When the user isn’t authenticated then the service will redirect the request to the logon page (status code 302). The REST service is a self-hosted windows service (existing middle layer). The service is available with the URI pattern: <em>http://&lt;myhost&gt;/docs/&lt;docname&gt;</em></p>
<p>As example: <em>http://mydomain/docs/test.docx</em></p>
<p>Here the simplified version of our REST service.</p>
<pre><span style="color:#0000ff;">public</span> Stream GetContent(<span style="color:#0000ff;">string</span> docId)
{

    WebOperationContext context = WebOperationContext.Current;

    <span style="color:#008000;">// read session cookie</span>
    <span style="color:#0000ff;">string</span> cookies = context.IncomingRequest.Headers[HttpRequestHeader.Cookie];
    <span style="color:#0000ff;">string</span> sessionId = "";
    <span style="color:#0000ff;">if</span> (cookies != <span style="color:#0000ff;">null</span>)
    {
        <span style="color:#008000;">// parse cookie values</span>
        var cookieItems = from c <span style="color:#0000ff;">in</span> cookies.Split(';')
                            let cc = c.Split('=')
                            where cc.Length == 2
                            select <span style="color:#0000ff;">new</span> { Name = cc[0].Trim().ToLower(), Value = cc[1].Trim() };

        var items = cookieItems.ToDictionary(c =&gt; c.Name);

        <span style="color:#0000ff;">if</span> (items.ContainsKey("<span style="color:#8b0000;">sessionid</span>"))
            sessionId = items["<span style="color:#8b0000;">sessionid</span>"].Value;
    }

    <span style="color:#008000;">// check session is valid</span>
    <span style="color:#0000ff;">bool</span> sessionValid = <span style="color:#0000ff;">false</span>;
    sessionValid = sessionId == "<span style="color:#8b0000;">1234</span>"; <span style="color:#008000;">// todo</span>

    <span style="color:#0000ff;">if</span> (!sessionValid)
    {
        <span style="color:#008000;">// redirect uri</span>
        <span style="color:#0000ff;">string</span> redirectUri = "<span style="color:#8b0000;">http://mydomain/logon.html</span>";
        <span style="color:#008000;">// ...set the location the logon site should naviagate to after the successful logon</span>
        redirectUri += "<span style="color:#8b0000;">?redirect=</span>" + context.IncomingRequest.UriTemplateMatch.RequestUri.ToString();
        <span style="color:#008000;">// redirect to the logon page </span>
        context.OutgoingResponse.Location = redirectUri;
        context.OutgoingResponse.StatusCode = HttpStatusCode.Redirect;
        <span style="color:#008000;">// response has header only</span>
        <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">null</span>;
    }
    <span style="color:#0000ff;">else</span>
    {

        <span style="color:#0000ff;">try</span>
        {

            <span style="color:#008000;">// check if document exists </span>
            <span style="color:#0000ff;">bool</span> docExists = <span style="color:#0000ff;">true</span>; <span style="color:#008000;">// todo</span>

            <span style="color:#0000ff;">if</span> (docExists)
            {
                <span style="color:#008000;">// todo</span>
                FileInfo requestedFile = <span style="color:#0000ff;">new</span> FileInfo("<span style="color:#8b0000;">todo</span>");

                <span style="color:#008000;">// set mime type (firefox, chrome and safari requires them)</span>
                context.OutgoingResponse.ContentType = GetMimeType(requestedFile.Extension);
                <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">new</span> FileStream(requestedFile.FullName, FileMode.Open);
            }
            <span style="color:#0000ff;">else</span>
            {
                <span style="color:#008000;">// doc not found</span>
                context.OutgoingResponse.StatusCode = HttpStatusCode.NotFound;
                context.OutgoingResponse.StatusDescription = "<span style="color:#8b0000;">Document not found</span>";
                <span style="color:#008000;">// response has header only</span>
                <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">null</span>;
            }

        }
        <span style="color:#0000ff;">catch</span> (Exception ex)
        {
            <span style="color:#008000;">// general error</span>
            context.OutgoingResponse.StatusCode = HttpStatusCode.InternalServerError;
            <span style="color:#008000;">// response has header only</span>
            <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">null</span>;
        }
    }
}</pre>
<p>At this point everything works fine with Firefox, Google Chrome and Safari on Mac.</p>
<h2>Internet Explorer is different</h2>
<p>Internet Explorer acts a little bit different as expected. When opening an office document (Word, Excel, PowerPoint) from a web page in Internet Explorer the Fiddler call stack lock like this:</p>
<pre>winword:  HEAD    http://mydomain/docs/test.docx HTTP/1.1     405 NotAllowed
winword:  OPTIONS http://mydomain/docs/ HTTP/1.1              405 NotAllowed
winword:  GET     http://mydomain/docs/test.docx HTTP/1.1     302 Redirect</pre>
<p>As you can see the iexplore process isn’t involved. What’s going on? IE detects the mime type from the file name extension, if it is preserved in the URL string. Here some examples:</p>
<pre> </pre>
<table border="0" cellspacing="2" cellpadding="2" width="600">
<tbody>
<tr>
<td width="300" valign="top">http://mydomain/docs/test.docx</td>
<td width="300" valign="top">IE detects mime type as docx</td>
</tr>
<tr>
<td width="300" valign="top">http://mydomain/service.svc/test.docx</td>
<td width="300" valign="top">IE cannot detect mime type (it isn’t svc)</td>
</tr>
<tr>
<td width="300" valign="top">http://mydomain/resource.ashx?file=test.docx</td>
<td width="300" valign="top">IE cannot detect mime type (it isn’t ash)</td>
</tr>
</tbody>
</table>
<p>What happened with iexplore? Office 2007 and 2010 Beta are designed to make a more collaborative workspace. Therefore, several changes have been made to how Office works with web content. These changes provide better authoring features for the following Web servers that support Office:</p>
<ul>
<li>Microsoft Windows SharePoint Services</li>
<li>Microsoft SharePoint Portal Server</li>
<li>Microsoft Exchange Web Store</li>
</ul>
<p>IE detects if the web resource is an Office format by analyzing the URL. If so IE will start the corresponding Office program with the URL as process start parameter like the following:</p>
<p><em>&#8220;C:\Program Files\Microsoft Office\Office14\WINWORD.EXE&#8221; /n http://mydomain/docs/test.docx”</em></p>
<p>Office is now downloading the content from the web:</p>
<p><a href="http://bkiener.files.wordpress.com/2010/04/image.png"><img style="display:inline;border-width:0;" title="image" src="http://bkiener.files.wordpress.com/2010/04/image_thumb.png?w=347&#038;h=121" border="0" alt="image" width="347" height="121" /></a></p>
<p>The request will end with the status code 302 and a redirect location to our logon page, because session cookies are not shared between Internet Explorer and winword process. Well, Office Word will follow this redirection and tries to display our Silverlight logon page. This isn’t possible because the SL plug-in isn’t available for the Office suite and Word shows the following content.</p>
<p><a href="http://bkiener.files.wordpress.com/2010/04/image4.png"><img style="display:inline;border-width:0;" title="image" src="http://bkiener.files.wordpress.com/2010/04/image_thumb4.png?w=582&#038;h=346" border="0" alt="image" width="582" height="346" /></a> </p>
<p>When we change our service so that it doesn’t make a redirection and returning the status code 404 (Not Found), then word is telling me that it wasn’t able to open the doc.</p>
<p><a href="http://bkiener.files.wordpress.com/2010/04/image2.png"><img style="display:inline;border-width:0;" title="image" src="http://bkiener.files.wordpress.com/2010/04/image_thumb2.png?w=348&#038;h=160" border="0" alt="image" width="348" height="160" /></a></p>
<h2>First improvement</h2>
<p>Changing the service to get another URL pattern for our documents so that IE cannot detect the mime type through the URL.</p>
<p>Something like this.</p>
<ul>
<li><em>http://mydomain/docs/id=test.docx </em></li>
<li><em>http://mydomain/docs.svc/test.docx</em></li>
</ul>
<p>What’s happening now. Let’s look at Fiddler’s call stack.</p>
<pre>iexplore: GET     http://mydomain/docs/test.docx HTTP/1.1     200 OK
winword:  HEAD    http://mydomain/docs/test.docx HTTP/1.1     405 NotAllowed
winword:  OPTIONS http://mydomain/docs/ HTTP/1.1              405 NotAllowed
winword:  GET     http://mydomain/docs/test.docx HTTP/1.1     302 Redirect</pre>
<p>Now IE is downloading the content as expected. But then winword comes into the play again. What’s going on in this situation?</p>
<p>IE cannot detect the mime type from the URL so it will download the web content as any other browser. With the request to our service the browser is sending our session cookie to the server too. Based on the mime type from the response IE decides to open Microsoft Word. IE does that the same way as before: starting the winword process with a startup argument including the URL instead of the downloaded local file.</p>
<p>&#8220;C:\Program Files\Microsoft Office\Office14\WINWORD.EXE&#8221; /n http://mydomain/docs/test.docx”</p>
<p>Same situation, Office looks like this again:</p>
<p><a href="http://bkiener.files.wordpress.com/2010/04/image5.png"><img style="display:inline;border:0;" title="image" src="http://bkiener.files.wordpress.com/2010/04/image_thumb5.png?w=339&#038;h=202" border="0" alt="image" width="339" height="202" /></a> </p>
<p>Why the Office suite is doing that? Because Office lets you edit and author documents on a Web site if the server supports Web authoring and collaboration (Sharepoint, Exchange, etc). First, Office tries to communicate with the Web server with a series of HEAD and OPTIONS requests to discover the possibilities of the webserver (“Microsoft Office Protocol Discovery”, “Microsoft Office Existence Discovery” and “Microsoft Office Core Storage Infrastructure”). Then Office tries to directly bind to the resource with a GET request to the web resource.</p>
<h2>Second improvement</h2>
<p>Our WCF REST service doesn’t handle the HEAD and OPTIONS request from the “Microsoft Office Protocol Discovery” and “Microsoft Office Existence Discovery”, but it redirects the GET request from the “Microsoft Office Core Storage Infrastructure” to the logon page while winword can’t deliver the session cookie.</p>
<p>What we can do is to detect the caller via the User-Agent header. There are three main User-Agent’s used from the Office suite.</p>
<ul>
<li>Microsoft Office Protocol Discovery</li>
<li>Microsoft Office Core Storage Infrastructure</li>
<li>Microsoft Office Existence Discovery</li>
</ul>
<p>In case of one of these 3 user-agents our service returns the status code 401 (Unauthorized) instead of the 302 (Redirect).</p>
<p>Then winword ignores the 401 and opens the document from the cached document which was previously downloaded from IE.</p>
<p>Here the code snippet to do that:</p>
<pre><span style="color:#008000;">// check user agent for office product suite</span>
<span style="color:#0000ff;">bool</span> isOfficeSuite = <span style="color:#0000ff;">false</span>;
<span style="color:#0000ff;">if</span> (!<span style="color:#0000ff;">string</span>.IsNullOrEmpty(WebOperationContext.Current.IncomingRequest.UserAgent))
{
    <span style="color:#0000ff;">string</span>[] officeUserAgents = { "<span style="color:#8b0000;">Microsoft Office Protocol Discovery</span>",
                                    "<span style="color:#8b0000;">Microsoft Office Existence Discovery</span>",
                                    "<span style="color:#8b0000;">Microsoft Office Core Storage Infrastructure</span>" };

    <span style="color:#0000ff;">string</span> requestUserAgent = WebOperationContext.Current.IncomingRequest.UserAgent.ToLower();

    var q = from userAgent <span style="color:#0000ff;">in</span> officeUserAgents
            where requestUserAgent.ToLower().Replace("<span style="color:#8b0000;"> </span>", "")
                               .Contains(userAgent.ToLower().Replace("<span style="color:#8b0000;"> </span>", ""))
            select userAgent;

    isOfficeSuite = q.Count() &gt; 0;
}

<span style="color:#0000ff;">if</span> (isOfficeSuite)
{
    <span style="color:#008000;">// don't redirect when office program want getting the document via </span>
    <span style="color:#008000;">// "Microsoft Office Protocol Discovery" or </span>
    <span style="color:#008000;">// "Microsoft Office Core Storage Infrastructure" requests.</span>
    <span style="color:#008000;">// Excel/word whould redirect to the logon page an display the html!</span>
    WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Unauthorized;
}
<span style="color:#0000ff;">else</span>
{
    <span style="color:#008000;">// redirect to the logon page</span>
    WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Redirect;
    <span style="color:#008000;">// TODO</span>
    WebOperationContext.Current.OutgoingResponse.Location = "<span style="color:#8b0000;">http://rnd.glauxsoft.ch/evidencenovaweb/</span>";
}

<span style="color:#008000;">// response has header only</span>
<span style="color:#0000ff;">return</span> <span style="color:#0000ff;">null</span>;</pre>
<p> </p>
<p>Unfortunately this works well with Office 2007, but first tests with Office 2010 Beta are different again. Excel and PowerPoint 2010 doesn’t ignore the 401 status code and telling us “Could not open the document &#8230;”. Maybe this is an error in the beta.</p>
<h2>Third improvement</h2>
<p>However, this kind of communication doesn’t make me happy. What can we do that IE downloads the content and then simply start the process associated to the mime type as that other browsers still do?</p>
<p>The typical workaround I found is to use the Content-Disposition attachment header in the GET response when returning the file. This header will tell the web browser to treat the file as a download (read-only), so the file will open in Office from the web browser cache location instead of a URL. With that setting, the Office application will treat the file as local, and will therefore not make calls back to the web server.</p>
<p>Content-disposition is an extension to the MIME protocol that instructs a MIME user agent on how it should display an attached file. When Internet Explorer receives the header, it raises a <strong>File Download</strong> dialog box whose file name box is automatically populated with the file name that is specified in the header</p>
<p>In our service we set this header for all well-known Office formats, because other mime types should still be opened inline within the browser such as PDF, TXT or JPG, GIF, etc.</p>
<p>Here the code snippet to doing that</p>
<pre><span style="color:#008000;">// well-know office formats</span>
<span style="color:#0000ff;">string</span>[] officeMimeTypes =  {  "<span style="color:#8b0000;">.doc</span>","<span style="color:#8b0000;">.dot</span>","<span style="color:#8b0000;">.docx</span>","<span style="color:#8b0000;">.dotx</span>","<span style="color:#8b0000;">.docm</span>","<span style="color:#8b0000;">.dotm</span>",
        "<span style="color:#8b0000;">.xls</span>","<span style="color:#8b0000;">.xlt</span>","<span style="color:#8b0000;">.xla</span>","<span style="color:#8b0000;">.xlsx</span>","<span style="color:#8b0000;">.xltx</span>","<span style="color:#8b0000;">.xlsm</span>",
        "<span style="color:#8b0000;">.xltm</span>","<span style="color:#8b0000;">.xlam</span>","<span style="color:#8b0000;">.xlsb</span>","<span style="color:#8b0000;">.ppt</span>","<span style="color:#8b0000;">.pot</span>","<span style="color:#8b0000;">.pps</span>",
        "<span style="color:#8b0000;">.ppa</span>","<span style="color:#8b0000;">.pptx</span>","<span style="color:#8b0000;">.potx</span>","<span style="color:#8b0000;">.ppsx</span>","<span style="color:#8b0000;">.ppam</span>","<span style="color:#8b0000;">.pptm</span>",
        "<span style="color:#8b0000;">.potm</span>","<span style="color:#8b0000;">.ppsm</span>"};

<span style="color:#008000;">// add content-disposition header.This header will tell the web browser </span>
<span style="color:#008000;">// to treat the file as a download (read-only), so the file will open </span>
<span style="color:#008000;">// in Office from the web browser cache location instead of a URL. </span>
<span style="color:#0000ff;">if</span> (officeMimeTypes.Contains(requestedFile.Extension.ToLower()))
{
    WebOperationContext.Current.OutgoingResponse.Headers.Add("<span style="color:#8b0000;">Content-disposition</span>",
                                            "<span style="color:#8b0000;">attachment;filename=</span>" + requestedFile.Name);
}

<span style="color:#008000;">// set mime type (firefox, chrome and safari requires them always)</span>
WebOperationContext.Current.OutgoingResponse.ContentType = GetMimeType(requestedFile.Extension);
<span style="color:#0000ff;">return</span> <span style="color:#0000ff;">new</span> FileStream(requestedFile.FullName, FileMode.Open);

 </pre>
<h2>Summary</h2>
<p>Internet Explorer handles Microsoft Office formats other than expected. The main reason is to make a more collaborative workspace when working with SharePoint and Exchange. This different behavior let you run into troubles when you have your own service providing the documents.</p>
<p>In brief you should consider the following to get around these troubles:</p>
<ul>
<li>Don’t let the IE detect the mime type from the URL</li>
<li>Don’t redirect to the logon page when the User-Agent is “Microsoft Office Core Storage Infrastructure”</li>
<li>Set the Content-disposition header for all well-known Office formats when returning the file.</li>
</ul>
<h2>Resources</h2>
<p><a title="http://support.microsoft.com/kb/260519/en-us" href="http://support.microsoft.com/kb/260519/en-us">http://support.microsoft.com/kb/260519/en-us</a></p>
<p><a title="http://support.microsoft.com/default.aspx?scid=kb;EN-US;899927" href="http://support.microsoft.com/default.aspx?scid=kb;EN-US;899927">http://support.microsoft.com/default.aspx?scid=kb;EN-US;899927</a></p>
<p><a title="http://blogs.msdn.com/vsofficedeveloper/pages/Office-Existence-Discovery-Protocol.aspx" href="http://blogs.msdn.com/vsofficedeveloper/pages/Office-Existence-Discovery-Protocol.aspx">http://blogs.msdn.com/vsofficedeveloper/pages/Office-Existence-Discovery-Protocol.aspx</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bkiener.wordpress.com/427/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bkiener.wordpress.com/427/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bkiener.wordpress.com/427/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bkiener.wordpress.com/427/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bkiener.wordpress.com/427/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bkiener.wordpress.com/427/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bkiener.wordpress.com/427/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bkiener.wordpress.com/427/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bkiener.wordpress.com/427/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bkiener.wordpress.com/427/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bkiener.wordpress.com/427/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bkiener.wordpress.com/427/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bkiener.wordpress.com/427/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bkiener.wordpress.com/427/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.thekieners.com&amp;blog=7436128&amp;post=427&amp;subd=bkiener&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.thekieners.com/2010/04/21/opening-an-microsoft-office-documents-with-silverlight/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/bbdffd0b655e44e12898217ff1973ad3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">beatkiener</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/04/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/04/image_thumb4.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/04/image_thumb2.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/04/image_thumb5.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>evidence nova running on Windows Phone 7 Series</title>
		<link>http://blog.thekieners.com/2010/03/30/evidence-nova-running-on-windows-phone-7-series/</link>
		<comments>http://blog.thekieners.com/2010/03/30/evidence-nova-running-on-windows-phone-7-series/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 11:26:29 +0000</pubDate>
		<dc:creator>beatkiener</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[evidence]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Windows Phone 7]]></category>

		<guid isPermaLink="false">http://bkiener.wordpress.com/2010/03/30/evidence-nova-running-on-windows-phone-7-series/</guid>
		<description><![CDATA[Panoramic applications are a part of the core Windows Phone 7 CTP experience. We already have a large web application built with Silverlight 3. The fact that Windows Phone 7 supports Silverlight as its application platform gave me the inspiration to think about a version which is running on the Windows Phone 7 as a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.thekieners.com&amp;blog=7436128&amp;post=414&amp;subd=bkiener&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Panoramic applications are a part of the core Windows Phone 7 CTP experience. We already have a large web application built with Silverlight 3. The fact that Windows Phone 7 supports Silverlight as its application platform gave me the inspiration to think about a version which is running on the Windows Phone 7 as a panoramic applications. I created a small demo application to demonstrate how our platform “evidence nova” could look like when running on the Windows Phone 7.</p>
<p><a href="http://files.thekieners.com/blogcontent/2010/novaOnWinPhone7" target="_blank">Click here to run the showcase application</a> (after the application is started, click into the content to navigate through the application).</p>
<p><a href="http://files.thekieners.com/blogcontent/2010/novaOnWinPhone7" target="_blank"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="NovaOnMobile7" border="0" alt="NovaOnMobile7" src="http://bkiener.files.wordpress.com/2010/03/novaonmobile72.gif?w=506&#038;h=356" width="506" height="356" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bkiener.wordpress.com/414/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bkiener.wordpress.com/414/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bkiener.wordpress.com/414/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bkiener.wordpress.com/414/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bkiener.wordpress.com/414/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bkiener.wordpress.com/414/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bkiener.wordpress.com/414/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bkiener.wordpress.com/414/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bkiener.wordpress.com/414/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bkiener.wordpress.com/414/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bkiener.wordpress.com/414/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bkiener.wordpress.com/414/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bkiener.wordpress.com/414/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bkiener.wordpress.com/414/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.thekieners.com&amp;blog=7436128&amp;post=414&amp;subd=bkiener&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.thekieners.com/2010/03/30/evidence-nova-running-on-windows-phone-7-series/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/bbdffd0b655e44e12898217ff1973ad3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">beatkiener</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/03/novaonmobile72.gif" medium="image">
			<media:title type="html">NovaOnMobile7</media:title>
		</media:content>
	</item>
		<item>
		<title>Deep Zoom Collection Downloader</title>
		<link>http://blog.thekieners.com/2010/03/02/deep-zoom-collection-downloader/</link>
		<comments>http://blog.thekieners.com/2010/03/02/deep-zoom-collection-downloader/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 05:51:04 +0000</pubDate>
		<dc:creator>beatkiener</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Deep Zoom]]></category>
		<category><![CDATA[MVVM]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://bkiener.wordpress.com/2010/03/02/deep-zoom-collection-downloader/</guid>
		<description><![CDATA[Just for fun I’ve implemented a Deep Zoom Tiles Downloader which recreates the original image from a deep zoom composition. Additionally the downloader can download the original composition from the website to your local disk. The downloader follows the principals of a pure MVVM application. No, i have not too much time. I’m pretty well [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.thekieners.com&amp;blog=7436128&amp;post=404&amp;subd=bkiener&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Just for fun I’ve implemented a <i>Deep Zoom Tiles Downloader</i> which recreates the original image from a deep zoom composition. Additionally the downloader can download the original composition from the website to your local disk. The downloader follows the principals of a pure MVVM application.</p>
<p>No, i have not too much time. I’m pretty well experienced with the MVVM pattern in Silverlight but not in the WPF runtime. To get more familiar with MVVM in WPF and to see the difference in a practical manner I decided to put the implementation into a ViewModel and to build a WPF-View on top of them.</p>
<h2>The Application</h2>
<p>In the <em>Image Rebuilder</em> tab, the application allows to set the deep zoom image settings or discover them automatically from a standard Deep Zoom Composer xml-file.</p>
<p><a href="http://bkiener.files.wordpress.com/2010/03/image.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://bkiener.files.wordpress.com/2010/03/image_thumb.png?w=684&#038;h=511" width="684" height="511" /></a> </p>
<p>The downloader allows you to set each deep zoom composition parameter manually, because there are deep zoom compositions out there in the internet without having the standard Deep Zoom Composer XML format. </p>
<p>One nice example of such a collection is the <a href="http://rubrik.ch.msn.com/reportagen/zermatt.aspx" target="_blank">“Matterhorn Deep Zoom Collection”</a> created by Ronnie Saurenmann (Microsoft Switzerland).</p>
<p><a href="http://rubrik.ch.msn.com/reportagen/zermatt.aspx" target="_blank"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://bkiener.files.wordpress.com/2010/03/image1.png?w=90&#038;h=72" width="90" height="72" /></a> </p>
<p>Enter the following values to test the downloader with his collection:</p>
<ul>
<li>URL Pattern: http://ronnie.blob.core.windows.net/zermatt/Tiles/560mmMatterhornSchwarzsee/{0}/{1}_{2}.jpg</li>
<li>Levels: 17</li>
<li>Tile Width: 254</li>
<li>Tile Height: 254</li>
<li>Tile Overlap: 1</li>
<li>Image Width: 55569</li>
<li>Image Height: 30064</li>
</ul>
<p>&#160;</p>
</p>
<p>In the <em>Tiles Downloader</em> tab, the application allows to set the root folder of the deep zoom composition (containing the folder levels 0, 1, 2, 3, 4, 5, etc), specifying the download target folder and to start the download.</p>
<p><a href="http://bkiener.files.wordpress.com/2010/03/image2.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://bkiener.files.wordpress.com/2010/03/image_thumb1.png?w=665&#038;h=497" width="665" height="497" /></a> </p>
<h2>&#160;</h2>
<h2>ViewModel implementation</h2>
<p>I’m using the Bitmap from the GDI to rebuild the image. A 32 bit RGB formatted bitmap uses 32 bit i.e. 4 bytes to store data for each pixel. So if you try to rebuild a large deep zoom composition with as example of 50000&#215;50000 pixels then the memory the downloader needs to allocate dynamically for the image is very large 50000*50000*4 bytes which is roughly about 9 GB in memory. While this is not possible with GDI the downloader downloads the image in smaller portion so you might get an image portioned as following (example show image in 5 portions):</p>
<p><a href="http://bkiener.files.wordpress.com/2010/03/image3.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="image" border="0" alt="image" src="http://bkiener.files.wordpress.com/2010/03/image_thumb2.png?w=593&#038;h=438" width="593" height="438" /></a> </p>
<p>It should be very easy to build the final image with Photoshop or ImageMagick from the portioned tiles. Feel free to extend the downloader with a better approach while using a 3<sup>rd</sup> party image lib.</p>
<p>A note about the performance: the downloader rebuilds or downloads images tile-by-tile. I did not have enough time to do it parallel in several threads.</p>
<h2>Summary</h2>
<p>It’s quite the same to implement a ViewModel in WPF and Silverlight for such very small applications. The only difference I can see so far is the missing constraint for asynchron web request in WPF. This makes the implementation a little bit simpler. The binding expressions are identically to Silverlight, because I don’t need any special here. The command binding was easier to write because the Command property is supported from the ButtonBase natively (Silverlight will provide the same in version 4).</p>
<p>I know, this is a quite simple application and things get complicated when the application gets more complex.</p>
<p>Feedbacks are welcome.</p>
<h2>Source code</h2>
<p>You can download the source code here:</p>
<p><a href="http://files.thekieners.com/blogcontent/2010/DeepZoomDownloader/source.zip" target="_blank">http://files.thekieners.com/blogcontent/2010/DeepZoomDownloader/source.zip</a></p>
<h2>One final note</h2>
<p>This downloader was implemented just for fun. Please consider copyrights when downloading deep zoom images.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/bkiener.wordpress.com/404/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/bkiener.wordpress.com/404/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/bkiener.wordpress.com/404/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/bkiener.wordpress.com/404/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/bkiener.wordpress.com/404/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/bkiener.wordpress.com/404/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/bkiener.wordpress.com/404/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/bkiener.wordpress.com/404/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/bkiener.wordpress.com/404/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/bkiener.wordpress.com/404/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/bkiener.wordpress.com/404/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/bkiener.wordpress.com/404/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/bkiener.wordpress.com/404/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/bkiener.wordpress.com/404/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.thekieners.com&amp;blog=7436128&amp;post=404&amp;subd=bkiener&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.thekieners.com/2010/03/02/deep-zoom-collection-downloader/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/bbdffd0b655e44e12898217ff1973ad3?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">beatkiener</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/03/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/03/image1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/03/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://bkiener.files.wordpress.com/2010/03/image_thumb2.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
	</channel>
</rss>
