Iterating through App Resources<p> </p> <p><font size=2>Again, I am quite new to .NET &amp; WPF so please be patient... I am trying to figure out different ways in which resources can be stored and accessed in a WPF project.</font></p> <p><font size=2></font> </p> <p><font size=2>Method 1: Adding a folder to the Project and adding resources to this folder</font></p> <p><font size=2>example: add folder named &quot;images&quot;. add 2 images to folder, &quot;image1.png&quot; &amp; &quot;image2.png&quot;.</font></p> <p><font size=2>These images can then be accessed in XAML like:</font></p> <p><font face="Courier New, Courier, Monospace" size=2>&lt;Image Source=&quot;images/image1.png&quot;/&gt;</font></p> <p><font size=2></font> </p> <p><font size=2>Method 2: Adding resources to the project using the resource manager, i.e. goto the project properties and select the &quot;Resources&quot; tab and add a couple images : &quot;image3.png&quot; and &quot;image4.png&quot;. This method creates a &quot;Resources&quot; folder and creates a Resources.Designer.cs file and some methods for accessing the images.</font></p> <p><font size=2>However, I am not able to programmatically access the images this way - or at least I do not know the correct way to access the images. </font></p> <p><font size=2>interestingly, </font></p> <p><font face="Courier New, Courier, Monospace" size=2>int c  = Resources.Count;</font></p> <p><font size=2>returns a value of 0 - even though I would assume there are 2 resources.</font></p> <p><font size=2>Also the call to <font face="Courier New, Courier, Monospace">this.FindResource(&quot;image3&quot;) </font>- causes an exception</font></p> <p><font size=2>All I am interested in are the ways to enumerate the resources in each method. Nothing I have tried has worked...</font></p>© 2009 Microsoft Corporation. All rights reserved.Thu, 19 Jun 2008 00:15:52 Z1bb025e8-a20a-43c4-a760-8666c63ff624http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1bb025e8-a20a-43c4-a760-8666c63ff624#1bb025e8-a20a-43c4-a760-8666c63ff624http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1bb025e8-a20a-43c4-a760-8666c63ff624#1bb025e8-a20a-43c4-a760-8666c63ff624jturpinhttp://social.msdn.microsoft.com/Profile/en-US/?user=jturpinIterating through App Resources<p> </p> <p><font size=2>Again, I am quite new to .NET &amp; WPF so please be patient... I am trying to figure out different ways in which resources can be stored and accessed in a WPF project.</font></p> <p><font size=2></font> </p> <p><font size=2>Method 1: Adding a folder to the Project and adding resources to this folder</font></p> <p><font size=2>example: add folder named &quot;images&quot;. add 2 images to folder, &quot;image1.png&quot; &amp; &quot;image2.png&quot;.</font></p> <p><font size=2>These images can then be accessed in XAML like:</font></p> <p><font face="Courier New, Courier, Monospace" size=2>&lt;Image Source=&quot;images/image1.png&quot;/&gt;</font></p> <p><font size=2></font> </p> <p><font size=2>Method 2: Adding resources to the project using the resource manager, i.e. goto the project properties and select the &quot;Resources&quot; tab and add a couple images : &quot;image3.png&quot; and &quot;image4.png&quot;. This method creates a &quot;Resources&quot; folder and creates a Resources.Designer.cs file and some methods for accessing the images.</font></p> <p><font size=2>However, I am not able to programmatically access the images this way - or at least I do not know the correct way to access the images. </font></p> <p><font size=2>interestingly, </font></p> <p><font face="Courier New, Courier, Monospace" size=2>int c  = Resources.Count;</font></p> <p><font size=2>returns a value of 0 - even though I would assume there are 2 resources.</font></p> <p><font size=2>Also the call to <font face="Courier New, Courier, Monospace">this.FindResource(&quot;image3&quot;) </font>- causes an exception</font></p> <p><font size=2>All I am interested in are the ways to enumerate the resources in each method. Nothing I have tried has worked...</font></p>Fri, 10 Nov 2006 17:35:40 Z2007-01-15T18:29:05Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1bb025e8-a20a-43c4-a760-8666c63ff624#7e93a62c-a605-45ac-8c9c-e5b81720ddeahttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1bb025e8-a20a-43c4-a760-8666c63ff624#7e93a62c-a605-45ac-8c9c-e5b81720ddeaMuscleHeadhttp://social.msdn.microsoft.com/Profile/en-US/?user=MuscleHeadIterating through App Resources<p>I did something similar to what you suggest in your first approach:</p><font color="#0000ff" size=2> <p>&lt;</font><font color="#800000" size=2>Application.Resources</font><font color="#0000ff" size=2>&gt;</font></p> <p><font color="#0000ff" size=2>     &lt;</font><font color="#800000" size=2>Image</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Source</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>winter.jpg</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Width</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>800</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>Height</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>600</font><font size=2>&quot;</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>x:Key</font><font color="#0000ff" size=2>=</font><font size=2>&quot;</font><font color="#0000ff" size=2>winter</font><font size=2>&quot;</font><font color="#0000ff" size=2> /&gt;</font></p> <p><font color="#0000ff" size=2>...</font></p> <p><font color="#0000ff" size=2>Then in the c# code, to access it:</font></p><font color="#0000ff" size=2><font color="#008080" size=2> <p>Image winter = Application</font><font color="#000000" size=2>.Current.FindResource(</font><font color="#800000" size=2>&quot;winter&quot;</font><font color="#000000" size=2>) </font><font color="#0000ff" size=2>as</font><font color="#000000" size=2> </font><font color="#008080" size=2>Image;</font></p> <p><font color="#008080" size=2>(you can use Application.Current.Resources to access the collection)</font></p> <p><font color="#008080" size=2> </p></font></font>Fri, 10 Nov 2006 18:28:35 Z2006-11-10T18:28:35Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1bb025e8-a20a-43c4-a760-8666c63ff624#1f3800a7-a6fa-42e3-89d4-3e0a2f4d02a9http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1bb025e8-a20a-43c4-a760-8666c63ff624#1f3800a7-a6fa-42e3-89d4-3e0a2f4d02a9jturpinhttp://social.msdn.microsoft.com/Profile/en-US/?user=jturpinIterating through App Resources<p><font size=2>Ok, so I have figured out how to iterate through resources added using Method 2 (Project Resource Manager):</font></p> <p> <hr id="[object]"> <p></p> <p><font face="Courier New, Courier, Monospace" size=1>ResourceManager myResMgr = AssembliesAndResources.Properties.Resources.ResourceManager;</font></p> <p><font face="Courier New, Courier, Monospace" size=1>if (myResMgr != null)<br>{<br>   ResourceSet myResSet = myResMgr.GetResourceSet(Thread.CurrentThread.CurrentCulture, true, false);</font></p> <p><font face="Courier New, Courier, Monospace" size=1>   if (myResSet != null)<br>   {<br>       IDictionaryEnumerator myEnum = myResSet.GetEnumerator();<br>       if (myEnum != null)<br>       {<br>           while (myEnum.MoveNext())<br>           {<br>               Console.WriteLine(&quot;key = {0}&quot;, myEnum.Key.ToString());<br>               Console.WriteLine(&quot;value type = {0}&quot;, myEnum.Value.GetType().ToString());</font></p> <p><font face="Courier New, Courier, Monospace" size=1>               // pngs = System.Drawing.Bitmap<br>           }<br>       }<br>   }<br>}</font></p> <p><font face="Courier New, Courier, Monospace"></font> <p> <hr id="[object]"> </p> <p></p> <p><font face="Courier New" size=2>However, there is still the mystery of iterating through resources added using Method 1 (adding a folder to the project). Also, how would you refer to resources added using Method 2 in XAML?</font></p> <p><font size=2>The weird thing is that</font></p> <p><font face="Courier New, Courier, Monospace" size=1>&lt;Window x:Class=&quot;AssembliesAndResources.Window1&quot;<br>    xmlns=&quot;</font><a title="http://schemas.microsoft.com/winfx/2006/xaml/presentation" href="http://schemas.microsoft.com/winfx/2006/xaml/presentation"><font face="Courier New, Courier, Monospace" size=1>http://schemas.microsoft.com/winfx/2006/xaml/presentation</font></a><font face="Courier New, Courier, Monospace" size=1>&quot;<br>    xmlns:x=&quot;</font><a title="http://schemas.microsoft.com/winfx/2006/xaml" href="http://schemas.microsoft.com/winfx/2006/xaml"><font face="Courier New, Courier, Monospace" size=1>http://schemas.microsoft.com/winfx/2006/xaml</font></a><font face="Courier New, Courier, Monospace" size=1>&quot;<br>    Title=&quot;AssembliesAndResources&quot; Height=&quot;300&quot; Width=&quot;300&quot;<br>    &gt;<br>    &lt;Grid&gt;<br>       &lt;Image Source=&quot;Resources/image3.png&quot;/&gt;<br>    &lt;/Grid&gt;<br>&lt;/Window&gt;<br></font></p> <p><font size=2><font face="Courier New, Courier, Monospace" size=1><font face="Geneva, Arial, Sans-serif" size=2>works in &quot;Design&quot; mode</font> </font>(i.e., the image shows up) - however when you run the app, no image shows up. The following (accessing Method 1 resources) - works in both Design mode and when the app is run:</font></p> <p><font face="Courier New, Courier, Monospace" size=1>&lt;Window x:Class=&quot;AssembliesAndResources.Window1&quot;<br>    xmlns=&quot;</font><a title="http://schemas.microsoft.com/winfx/2006/xaml/presentation" href="http://schemas.microsoft.com/winfx/2006/xaml/presentation"><font face="Courier New, Courier, Monospace" color="#0000ff" size=1>http://schemas.microsoft.com/winfx/2006/xaml/presentation</font></a><font face="Courier New, Courier, Monospace" size=1>&quot;<br>    xmlns:x=&quot;</font><a title="http://schemas.microsoft.com/winfx/2006/xaml" href="http://schemas.microsoft.com/winfx/2006/xaml"><font face="Courier New, Courier, Monospace" color="#0000ff" size=1>http://schemas.microsoft.com/winfx/2006/xaml</font></a><font face="Courier New, Courier, Monospace" size=1>&quot;<br>    Title=&quot;AssembliesAndResources&quot; Height=&quot;300&quot; Width=&quot;300&quot;<br>    &gt;<br>    &lt;Grid&gt;<br>       &lt;Image Source=&quot;images/image1.png&quot;/&gt;<br>    &lt;/Grid&gt;<br>&lt;/Window&gt;</font></p> <p> </p> <p> </p>Fri, 10 Nov 2006 18:31:51 Z2006-11-10T18:31:51Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1bb025e8-a20a-43c4-a760-8666c63ff624#cde09959-5345-4500-af79-5e479a261e91http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1bb025e8-a20a-43c4-a760-8666c63ff624#cde09959-5345-4500-af79-5e479a261e91MuscleHeadhttp://social.msdn.microsoft.com/Profile/en-US/?user=MuscleHeadIterating through App ResourcesDid my previous post not answer how to iterate through resources using method 1?Mon, 13 Nov 2006 22:28:19 Z2006-11-13T22:28:19Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1bb025e8-a20a-43c4-a760-8666c63ff624#42766379-7f41-422f-b097-590841765439http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1bb025e8-a20a-43c4-a760-8666c63ff624#42766379-7f41-422f-b097-590841765439Zhou Yonghttp://social.msdn.microsoft.com/Profile/en-US/?user=Zhou%20YongIterating through App ResourcesI've no interest in resources other than baml resources, so several days ago, I write a simple collection which can hold up the baml resources defined by a specified assembly:<br><br> <div style="background:white none repeat scroll 0%;font-family:Verdana;font-size:9pt;color:black"><pre style="margin:0px"><span style="color:blue;font-family:Verdana">public</span><span style="font-family:Verdana"> </span><span style="color:blue;font-family:Verdana">class</span><span style="font-family:Verdana"> </span><span style="color:teal;font-family:Verdana">XamlResourceCollection</span><span style="font-family:Verdana"> : </span><span style="color:teal;font-family:Verdana">ObservableCollection</span><span style="font-family:Verdana">&lt;</span><span style="color:teal;font-family:Verdana">XamlResourceItem</span><span style="font-family:Verdana">&gt;</span></pre><pre style="margin:0px"><span style="font-family:Verdana">{</span></pre><pre style="margin:0px"><span style="font-family:Verdana">    </span><span style="color:blue;font-family:Verdana">public</span><span style="font-family:Verdana"> XamlResourceCollection(</span><span style="color:teal;font-family:Verdana">Assembly</span><span style="font-family:Verdana"> assembly)</span></pre><pre style="margin:0px"><span style="font-family:Verdana">    {</span></pre><pre style="margin:0px"><span style="font-family:Verdana">        </span><span style="color:blue;font-family:Verdana">foreach</span><span style="font-family:Verdana"> (</span><span style="color:teal;font-family:Verdana">String</span><span style="font-family:Verdana"> resourceName </span><span style="color:blue;font-family:Verdana">in</span><span style="font-family:Verdana"> assembly.GetManifestResourceNames())</span></pre><pre style="margin:0px"><span style="font-family:Verdana">        {</span></pre><pre style="margin:0px"><span style="font-family:Verdana">            </span><span style="color:blue;font-family:Verdana">if</span><span style="font-family:Verdana"> (resourceName.ToLower().EndsWith(</span><span style="color:maroon;font-family:Verdana">&quot;.g.resources&quot;</span><span style="font-family:Verdana">))</span></pre><pre style="margin:0px"><span style="font-family:Verdana">            {</span></pre><pre style="margin:0px"><span style="font-family:Verdana">                </span><span style="color:blue;font-family:Verdana">using</span><span style="font-family:Verdana"> (</span><span style="color:teal;font-family:Verdana">Stream</span><span style="font-family:Verdana"> stream = assembly.GetManifestResourceStream(resourceName))</span></pre><pre style="margin:0px"><span style="font-family:Verdana">                {</span></pre><pre style="margin:0px"><span style="font-family:Verdana">                    </span><span style="color:blue;font-family:Verdana">using</span><span style="font-family:Verdana"> (</span><span style="color:teal;font-family:Verdana">ResourceReader</span><span style="font-family:Verdana"> reader = </span><span style="color:blue;font-family:Verdana">new</span><span style="font-family:Verdana"> </span><span style="color:teal;font-family:Verdana">ResourceReader</span><span style="font-family:Verdana">(stream))</span></pre><pre style="margin:0px"><span style="font-family:Verdana">                    {</span></pre><pre style="margin:0px"><span style="font-family:Verdana">                        </span><span style="color:blue;font-family:Verdana">foreach</span><span style="font-family:Verdana"> (</span><span style="color:teal;font-family:Verdana">DictionaryEntry</span><span style="font-family:Verdana"> entry </span><span style="color:blue;font-family:Verdana">in</span><span style="font-family:Verdana"> reader)</span></pre><pre style="margin:0px"><span style="font-family:Verdana">                        {</span></pre><pre style="margin:0px"><span style="font-family:Verdana">                            </span><span style="color:blue;font-family:Verdana">if</span><span style="font-family:Verdana"> (entry.Key.ToString().ToLower().EndsWith(</span><span style="color:maroon;font-family:Verdana">&quot;.baml&quot;</span><span style="font-family:Verdana">))</span></pre><pre style="margin:0px"><span style="font-family:Verdana">                            {</span></pre><pre style="margin:0px"><span style="font-family:Verdana">                                </span><span style="color:teal;font-family:Verdana">XamlResourceItem</span><span style="font-family:Verdana"> item = </span><span style="color:blue;font-family:Verdana">new</span><span style="font-family:Verdana"> </span><span style="color:teal;font-family:Verdana">XamlResourceItem</span><span style="font-family:Verdana">(entry.Key.ToString(), assembly.GetName().Name);</span></pre><pre style="margin:0px"><span style="font-family:Verdana">                                </span><span style="color:blue;font-family:Verdana">this</span><span style="font-family:Verdana">.Add(item);</span></pre><pre style="margin:0px"><span style="font-family:Verdana">                            }</span></pre><pre style="margin:0px"><span style="font-family:Verdana">                        }</span></pre><pre style="margin:0px"><span style="font-family:Verdana">                    }</span></pre><pre style="margin:0px"><span style="font-family:Verdana">                }</span></pre><pre style="margin:0px"><span style="font-family:Verdana">            }</span></pre><pre style="margin:0px"><span style="font-family:Verdana">        }</span></pre><pre style="margin:0px"><span style="font-family:Verdana">    }</span></pre><pre style="margin:0px"><span style="font-family:Verdana">}</span></pre><pre style="margin:0px"> </pre><pre style="margin:0px"><span style="color:blue;font-family:Verdana">public</span><span style="font-family:Verdana"> </span><span style="color:blue;font-family:Verdana">class</span><span style="font-family:Verdana"> </span><span style="color:teal;font-family:Verdana">XamlResourceItem</span></pre><pre style="margin:0px"><span style="font-family:Verdana">{</span></pre><pre style="margin:0px"><span style="font-family:Verdana">    </span><span style="color:blue;font-family:Verdana">private</span><span style="font-family:Verdana"> </span><span style="color:teal;font-family:Verdana">String</span><span style="font-family:Verdana"> resourceName;</span></pre><pre style="margin:0px"><span style="font-family:Verdana">    </span><span style="color:blue;font-family:Verdana">private</span><span style="font-family:Verdana"> </span><span style="color:teal;font-family:Verdana">Uri</span><span style="font-family:Verdana"> resourceUri;</span></pre><pre style="margin:0px"> </pre><pre style="margin:0px"><span style="font-family:Verdana">    </span><span style="color:blue;font-family:Verdana">public</span><span style="font-family:Verdana"> XamlResourceItem(</span><span style="color:teal;font-family:Verdana">String</span><span style="font-family:Verdana"> resourceName, </span><span style="color:teal;font-family:Verdana">String</span><span style="font-family:Verdana"> containingAssemblyName)</span></pre><pre style="margin:0px"><span style="font-family:Verdana">    {</span></pre><pre style="margin:0px"><span style="font-family:Verdana">        </span><span style="color:blue;font-family:Verdana">this</span><span style="font-family:Verdana">.resourceName = resourceName;</span></pre><pre style="margin:0px"><span style="font-family:Verdana">        </span><span style="color:blue;font-family:Verdana">this</span><span style="font-family:Verdana">.resourceUri = </span><span style="color:blue;font-family:Verdana">new</span><span style="font-family:Verdana"> </span><span style="color:teal;font-family:Verdana">Uri</span><span style="font-family:Verdana">(</span><span style="color:maroon;font-family:Verdana">&quot;/&quot;</span><span style="font-family:Verdana"> + containingAssemblyName + </span><span style="color:maroon;font-family:Verdana">&quot;;component/&quot;</span><span style="font-family:Verdana"> + resourceName.ToLower().Replace(</span><span style="color:maroon;font-family:Verdana">&quot;.baml&quot;</span><span style="font-family:Verdana">, </span><span style="color:maroon;font-family:Verdana">&quot;.xaml&quot;</span><span style="font-family:Verdana">), </span><span style="color:teal;font-family:Verdana">UriKind</span><span style="font-family:Verdana">.RelativeOrAbsolute);</span></pre><pre style="margin:0px"><span style="font-family:Verdana">    }</span></pre><pre style="margin:0px"> </pre><pre style="margin:0px"><span style="font-family:Verdana">    </span><span style="color:blue;font-family:Verdana">public</span><span style="font-family:Verdana"> </span><span style="color:teal;font-family:Verdana">String</span><span style="font-family:Verdana"> ResourceName</span></pre><pre style="margin:0px"><span style="font-family:Verdana">    {</span></pre><pre style="margin:0px"><span style="font-family:Verdana">        </span><span style="color:blue;font-family:Verdana">get</span><span style="font-family:Verdana"> { </span><span style="color:blue;font-family:Verdana">return</span><span style="font-family:Verdana"> resourceName; }</span></pre><pre style="margin:0px"><span style="font-family:Verdana">    }</span></pre><pre style="margin:0px"> </pre><pre style="margin:0px"><span style="font-family:Verdana">    </span><span style="color:blue;font-family:Verdana">public</span><span style="font-family:Verdana"> </span><span style="color:teal;font-family:Verdana">Uri</span><span style="font-family:Verdana"> ResourceUri</span></pre><pre style="margin:0px"><span style="font-family:Verdana">    {</span></pre><pre style="margin:0px"><span style="font-family:Verdana">        </span><span style="color:blue;font-family:Verdana">get</span><span style="font-family:Verdana"> { </span><span style="color:blue;font-family:Verdana">return</span><span style="font-family:Verdana"> resourceUri; }</span></pre><pre style="margin:0px"><span style="font-family:Verdana">    }</span></pre><pre style="margin:0px"><span style="font-family:Verdana">}</span></pre></div> <br>When you can grab the ResourceUri off the XamlResourceItem, you can use LoadComponent() method to instantiate it.<br><br>Sheva<br><br>Tue, 14 Nov 2006 13:31:11 Z2006-11-14T13:31:11Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1bb025e8-a20a-43c4-a760-8666c63ff624#b494114d-5e0f-4e5e-9338-b096731514f3http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1bb025e8-a20a-43c4-a760-8666c63ff624#b494114d-5e0f-4e5e-9338-b096731514f3jturpinhttp://social.msdn.microsoft.com/Profile/en-US/?user=jturpinIterating through App Resources<P><FONT size=2>Musclehead, I guess when I say "iterate" through resources, what I mean is that I am looking for a way to "enumerate" all of the resources - i.e., be able to access the resources in a list without not knowing what the actual resource names or "key" values are - and step through the list one-by-one. Your example was referencing a resource "explicitly" by its name.</FONT></P> <P><FONT size=2>&nbsp;I guess for now I will just make sure that all the resources that I want to access this way are included as "embedded" resources in the .resx file and not simply in a folder added to the project.</FONT></P> <P><FONT size=2>Thanks</FONT></P> <P>&nbsp;</P>Tue, 14 Nov 2006 15:27:04 Z2006-11-14T15:27:04Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1bb025e8-a20a-43c4-a760-8666c63ff624#6d4721a6-c387-4d65-87b4-fb79284a121bhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1bb025e8-a20a-43c4-a760-8666c63ff624#6d4721a6-c387-4d65-87b4-fb79284a121bMuscleHeadhttp://social.msdn.microsoft.com/Profile/en-US/?user=MuscleHeadIterating through App Resources<p>Maybe you missed the last line in my post?</p> <p><font color="#000000">(you can use Application.Current.Resources to access the collection)</font></p>Tue, 14 Nov 2006 21:42:42 Z2007-01-15T18:29:05Z