{"id":3042,"date":"2015-03-27T20:02:23","date_gmt":"2015-03-27T20:02:23","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=3042"},"modified":"2015-03-27T20:02:23","modified_gmt":"2015-03-27T20:02:23","slug":"accessing-xaml-resources-in-code","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/accessing-xaml-resources-in-code\/","title":{"rendered":"Accessing XAML resources in code"},"content":{"rendered":"<p>I have a bunch of brushes, colours etc. within the Generic.xaml file inside a ResourceDictionary and I needed to get at the resources from code. <\/p>\n<p>So let&#8217;s assume our XAML looks like this<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;ResourceDictionary \r\n   xmlns=&quot;http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\/presentation&quot;\r\n   xmlns:x=&quot;http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml&quot;&gt;\r\n    &lt;SolidColorBrush \r\n       x:Key=&quot;DisabledBackgroundBrush&quot; \r\n       Color=&quot;#FFF0F0F0&quot; \/&gt;\r\n    &lt;SolidColorBrush \r\n       x:Key=&quot;DisabledForegroundTextBrush&quot; \r\n       Color=&quot;DarkGray&quot; \/&gt;\r\n&lt;\/ResourceDictionary&gt;\r\n<\/pre>\n<p>Now to access these resource from code I&#8217;m going to use the following class\/code<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic static class GenericResources\r\n{\r\n   private static readonly ResourceDictionary genericResourceDictionary;\r\n\r\n   static GenericResources()\r\n   {\r\n      var uri = new Uri(&quot;\/MyAssembly;component\/Generic.xaml&quot;, UriKind.Relative);\r\n      genericResourceDictionary = (ResourceDictionary)Application.LoadComponent(uri);\t\t\t\r\n   }\r\n\r\n   public static Brush ReferenceBackColor\r\n   {\r\n      get { return (Brush) genericResourceDictionary&#x5B;&quot;DisabledBackgroundBrush&quot;]; }\r\n   }\r\n\r\n   public static Brush DisabledForeground\r\n   {\r\n      get { return (Brush)genericResourceDictionary&#x5B;&quot;DisabledForegroundTextBrush&quot;]; }\r\n   }\r\n}\r\n<\/pre>\n<p>As you can see we create a Uri to the XAML using the format &#8220;\/&lt;assembly_name&gt;;component\/&lt;subfolders&gt;\/&lt;xaml_filename&gt;&#8221;. Next we get the ResourceDictionary via the LoadComponent method. Now to access the resource dictionary assets via the key&#8217;s we simply use the dictionary&#8217;s indexer and cast to our expected type.<\/p>\n<p>And that&#8217;s all there is to it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a bunch of brushes, colours etc. within the Generic.xaml file inside a ResourceDictionary and I needed to get at the resources from code. So let&#8217;s assume our XAML looks like this &lt;ResourceDictionary xmlns=&quot;http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\/presentation&quot; xmlns:x=&quot;http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml&quot;&gt; &lt;SolidColorBrush x:Key=&quot;DisabledBackgroundBrush&quot; Color=&quot;#FFF0F0F0&quot; \/&gt; &lt;SolidColorBrush x:Key=&quot;DisabledForegroundTextBrush&quot; Color=&quot;DarkGray&quot; \/&gt; &lt;\/ResourceDictionary&gt; Now to access these resource from code I&#8217;m going to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13],"tags":[],"class_list":["post-3042","post","type-post","status-publish","format-standard","hentry","category-wpf"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3042","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=3042"}],"version-history":[{"count":7,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3042\/revisions"}],"predecessor-version":[{"id":3051,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3042\/revisions\/3051"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=3042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=3042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=3042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}