<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: A tip for referencing Assets in your WPF application</title>
	<atom:link href="http://blog.pixelingene.com/?feed=rss2&#038;p=352" rel="self" type="application/rss+xml" />
	<link>http://blog.pixelingene.com/?p=352&amp;utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=a-tip-for-referencing-assets-in-your-wpf-application</link>
	<description>Exploring creativity with Design / Graphics / Technology</description>
	<lastBuildDate>Mon, 06 Sep 2010 08:10:11 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Steven</title>
		<link>http://blog.pixelingene.com/?p=352&#038;cpage=1#comment-510</link>
		<dc:creator>Steven</dc:creator>
		<pubDate>Tue, 13 Jan 2009 15:42:06 +0000</pubDate>
		<guid isPermaLink="false">http://blog.pixelingene.com/?p=352#comment-510</guid>
		<description>A welcomed extension!  If you do not specify the parameter name &quot;Path=&quot; in the XAML, have you seen - and do you know why - VS08 warns that &quot;No constructor for AssetExtension has 1 parameter&quot;?  If you include the parameter name &quot;Path=&quot; that warning goes away.  Cheers.</description>
		<content:encoded><![CDATA[<p>A welcomed extension!  If you do not specify the parameter name &#8220;Path=&#8221; in the XAML, have you seen &#8211; and do you know why &#8211; VS08 warns that &#8220;No constructor for AssetExtension has 1 parameter&#8221;?  If you include the parameter name &#8220;Path=&#8221; that warning goes away.  Cheers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yogesh</title>
		<link>http://blog.pixelingene.com/?p=352&#038;cpage=1#comment-509</link>
		<dc:creator>Yogesh</dc:creator>
		<pubDate>Tue, 06 Jan 2009 11:22:23 +0000</pubDate>
		<guid isPermaLink="false">http://blog.pixelingene.com/?p=352#comment-509</guid>
		<description>Thanks for the article Pavan. I was using the direct link approach till now and my application uses a lot of small images. I changed by solution to use resource extensions and I feel this way is a much better and maintainable way of accessing resources.

BTW: Returning a BitmapImage at all places caused exceptions for me (specially in custom controls) so I took Mike&#039;s way by using Binding as the return value and solved the issue. Thanks to Mike for the resolution. :)</description>
		<content:encoded><![CDATA[<p>Thanks for the article Pavan. I was using the direct link approach till now and my application uses a lot of small images. I changed by solution to use resource extensions and I feel this way is a much better and maintainable way of accessing resources.</p>
<p>BTW: Returning a BitmapImage at all places caused exceptions for me (specially in custom controls) so I took Mike&#8217;s way by using Binding as the return value and solved the issue. Thanks to Mike for the resolution. <img src='http://blog.pixelingene.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike Strobel</title>
		<link>http://blog.pixelingene.com/?p=352&#038;cpage=1#comment-508</link>
		<dc:creator>Mike Strobel</dc:creator>
		<pubDate>Tue, 06 Jan 2009 06:14:51 +0000</pubDate>
		<guid isPermaLink="false">http://blog.pixelingene.com/?p=352#comment-508</guid>
		<description>I agree, the custom URI scheme is a really fun exercise.  Not only is it cool to have WPF &quot;automagically&quot; recognize your own custom URI format, but it&#039;s also the most lightweight implementation.  That is, after all, how pack URIs are handled (see PackWebRequest and PackWebResponse for reference).  A custom-tailored implementation should be even more efficient and flexible :).  You can also register your handler in your App.config file rather than relying solely on a runtime call to WebRequest.RegisterPrefix().</description>
		<content:encoded><![CDATA[<p>I agree, the custom URI scheme is a really fun exercise.  Not only is it cool to have WPF &#8220;automagically&#8221; recognize your own custom URI format, but it&#8217;s also the most lightweight implementation.  That is, after all, how pack URIs are handled (see PackWebRequest and PackWebResponse for reference).  A custom-tailored implementation should be even more efficient and flexible <img src='http://blog.pixelingene.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .  You can also register your handler in your App.config file rather than relying solely on a runtime call to WebRequest.RegisterPrefix().</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pavan Podila</title>
		<link>http://blog.pixelingene.com/?p=352&#038;cpage=1#comment-507</link>
		<dc:creator>Pavan Podila</dc:creator>
		<pubDate>Tue, 06 Jan 2009 00:28:02 +0000</pubDate>
		<guid isPermaLink="false">http://blog.pixelingene.com/?p=352#comment-507</guid>
		<description>I was thinking of going the route of using Converters but then realized its just unnecessary indirection and I could just save some CPU cycles too :-). 

The Binding option is also interesting. If I may suggest, you should also set the Mode to OneTime, just for good measure.

The suggestions about Caching and WebRequest are the most interesting. I am definitely going to try them out.</description>
		<content:encoded><![CDATA[<p>I was thinking of going the route of using Converters but then realized its just unnecessary indirection and I could just save some CPU cycles too <img src='http://blog.pixelingene.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> . </p>
<p>The Binding option is also interesting. If I may suggest, you should also set the Mode to OneTime, just for good measure.</p>
<p>The suggestions about Caching and WebRequest are the most interesting. I am definitely going to try them out.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike Strobel</title>
		<link>http://blog.pixelingene.com/?p=352&#038;cpage=1#comment-506</link>
		<dc:creator>Mike Strobel</dc:creator>
		<pubDate>Mon, 05 Jan 2009 23:33:21 +0000</pubDate>
		<guid isPermaLink="false">http://blog.pixelingene.com/?p=352#comment-506</guid>
		<description>A-ha, now I remember how I did it before.  I used a Binding inside my markup extension to leverage WPF&#039;s built-in value converters:

public override object ProvideValue(IServiceProvider serviceProvider)
{
    var binding = new Binding
                  {
                      Source = GetResourceUri(Path),
                      BindsDirectlyToSource = true
                  };
    return binding.ProvideValue(serviceProvider);
}</description>
		<content:encoded><![CDATA[<p>A-ha, now I remember how I did it before.  I used a Binding inside my markup extension to leverage WPF&#8217;s built-in value converters:</p>
<p>public override object ProvideValue(IServiceProvider serviceProvider)<br />
{<br />
    var binding = new Binding<br />
                  {<br />
                      Source = GetResourceUri(Path),<br />
                      BindsDirectlyToSource = true<br />
                  };<br />
    return binding.ProvideValue(serviceProvider);<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike Strobel</title>
		<link>http://blog.pixelingene.com/?p=352&#038;cpage=1#comment-505</link>
		<dc:creator>Mike Strobel</dc:creator>
		<pubDate>Mon, 05 Jan 2009 23:19:32 +0000</pubDate>
		<guid isPermaLink="false">http://blog.pixelingene.com/?p=352#comment-505</guid>
		<description>Whoops, my example Xaml got stripped out:

[Image Source=&quot;{Binding Source=&#039;Images/splash.png&#039;, Converter={StaticResource ResourcePathConverter}}&quot; /]</description>
		<content:encoded><![CDATA[<p>Whoops, my example Xaml got stripped out:</p>
<p>[Image Source="{Binding Source='Images/splash.png', Converter={StaticResource ResourcePathConverter}}" /]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike Strobel</title>
		<link>http://blog.pixelingene.com/?p=352&#038;cpage=1#comment-504</link>
		<dc:creator>Mike Strobel</dc:creator>
		<pubDate>Mon, 05 Jan 2009 23:18:26 +0000</pubDate>
		<guid isPermaLink="false">http://blog.pixelingene.com/?p=352#comment-504</guid>
		<description>Pavan, it seems you&#039;re right about an exception being thrown when you return just the URI.  I may have been remembering incorrectly.  I think I actually tried a different approach where I used a value converter to convert the &quot;short&quot; path to the fully qualified resource URI, like so:



Obviously, this isn&#039;t much better than typing the full URI, which is why I went on explore other options.

As for the caching, I simply meant that if you are using the same image in multiple places in your application (for example), your extension could cache a weak reference when it creates the first BitmapImage, and then return the same cached instance in subsequent requests for the same image (if the reference is still alive).  That would save you some video memory in theory.  It&#039;s possible that WPF uses a bitmap caching system internally that I&#039;m unaware of.</description>
		<content:encoded><![CDATA[<p>Pavan, it seems you&#8217;re right about an exception being thrown when you return just the URI.  I may have been remembering incorrectly.  I think I actually tried a different approach where I used a value converter to convert the &#8220;short&#8221; path to the fully qualified resource URI, like so:</p>
<p>Obviously, this isn&#8217;t much better than typing the full URI, which is why I went on explore other options.</p>
<p>As for the caching, I simply meant that if you are using the same image in multiple places in your application (for example), your extension could cache a weak reference when it creates the first BitmapImage, and then return the same cached instance in subsequent requests for the same image (if the reference is still alive).  That would save you some video memory in theory.  It&#8217;s possible that WPF uses a bitmap caching system internally that I&#8217;m unaware of.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pavan Podila</title>
		<link>http://blog.pixelingene.com/?p=352&#038;cpage=1#comment-503</link>
		<dc:creator>Pavan Podila</dc:creator>
		<pubDate>Mon, 05 Jan 2009 22:08:02 +0000</pubDate>
		<guid isPermaLink="false">http://blog.pixelingene.com/?p=352#comment-503</guid>
		<description>That&#039;s a useful thing to have Mike. When using the AssetExtension, I am aware of where it is being used, so the caching part is kind of redundant. Although I love the idea and I may find it useful when returning references of other kinds.

I tried returning the Uri directly but it causes exceptions. That&#039;s the reason why I had to create them myself. I&#039;ll give it a shot again just to confirm.

The WebRequest thing is interesting and would love to try that.</description>
		<content:encoded><![CDATA[<p>That&#8217;s a useful thing to have Mike. When using the AssetExtension, I am aware of where it is being used, so the caching part is kind of redundant. Although I love the idea and I may find it useful when returning references of other kinds.</p>
<p>I tried returning the Uri directly but it causes exceptions. That&#8217;s the reason why I had to create them myself. I&#8217;ll give it a shot again just to confirm.</p>
<p>The WebRequest thing is interesting and would love to try that.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike Strobel</title>
		<link>http://blog.pixelingene.com/?p=352&#038;cpage=1#comment-502</link>
		<dc:creator>Mike Strobel</dc:creator>
		<pubDate>Mon, 05 Jan 2009 20:39:26 +0000</pubDate>
		<guid isPermaLink="false">http://blog.pixelingene.com/?p=352#comment-502</guid>
		<description>You can actually just return GetResourceUri(Path), and the resulting string will be handled exactly as if you had just typed the entire pack URI in the Xaml.  That means image and font URIs will still get properly converted (e.g. WPF will cook up an ImageSource for you if you return a URI to an image file).

The real potential in your solution is that you have the opportunity to do caching.  If all of your assets are loaded with this extension, you should consider keeping a lookup table that maps asset paths to weak references of those assets.  That way, if they get reused (which many do), you&#039;re not loading them multiple times.

There is another option to consider.  You could register a custom web request handler.  If create a web request factory that implements IWebRequestCreate, you can register it to handle a custom URI scheme (see WebRequest.RegisterPrefix(...)).  Then you could replace your markup extensions with simple URIs, like &quot;asset:///Images/marker/self_fill.png&quot;.  You&#039;ll need to implement a custom WebResponse that overrides GetResponseStream().  I ended up doing this for a project where resources were organized in a virtual file system.  I needed to check different relative locations in priority order.  It worked out rather well.</description>
		<content:encoded><![CDATA[<p>You can actually just return GetResourceUri(Path), and the resulting string will be handled exactly as if you had just typed the entire pack URI in the Xaml.  That means image and font URIs will still get properly converted (e.g. WPF will cook up an ImageSource for you if you return a URI to an image file).</p>
<p>The real potential in your solution is that you have the opportunity to do caching.  If all of your assets are loaded with this extension, you should consider keeping a lookup table that maps asset paths to weak references of those assets.  That way, if they get reused (which many do), you&#8217;re not loading them multiple times.</p>
<p>There is another option to consider.  You could register a custom web request handler.  If create a web request factory that implements IWebRequestCreate, you can register it to handle a custom URI scheme (see WebRequest.RegisterPrefix(&#8230;)).  Then you could replace your markup extensions with simple URIs, like &#8220;asset:///Images/marker/self_fill.png&#8221;.  You&#8217;ll need to implement a custom WebResponse that overrides GetResponseStream().  I ended up doing this for a project where resources were organized in a virtual file system.  I needed to check different relative locations in priority order.  It worked out rather well.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
