{"id":35031,"date":"2018-01-23T14:28:44","date_gmt":"2018-01-23T22:28:44","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=35031"},"modified":"2019-04-04T08:58:55","modified_gmt":"2019-04-04T15:58:55","slug":"better-resource-organization-xamarin-forms","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/better-resource-organization-xamarin-forms\/","title":{"rendered":"Better Resource Organization in Xamarin.Forms"},"content":{"rendered":"<p>\t\t\t\t<a href=\"https:\/\/developer.xamarin.com\/guides\/xamarin-forms\/xaml\/resource-dictionaries\/\">Resource Dictionary<\/a> is a foundational building block to all Xamarin.Forms mobile development. It&#8217;s the primary vehicle for declaring XAML resources you\u2019ll use throughout your mobile application, including everything from styles to value converters, content templates, and data templates. In the most <a href=\"https:\/\/developer.xamarin.com\/releases\/xamarin-forms\/xamarin-forms-2.5\/2.5.0-sr3\/#Roadmap_Features\">recent stable release<\/a> of Xamarin.Forms, we gained the ability to merge multiple resource dictionaries for even greater flexibility in composing resources. In this article, I\u2019ll show you how to take advantage of that and give you a glimpse at some of the new syntax improvements coming your way.<\/p>\n<h2>Merged Dictionaries<\/h2>\n<h3>Out with the Old<\/h3>\n<p>Prior to 2.5.0, you probably found yourself implementing a single resource dictionary per <code>ContentPage<\/code> or other control. Every control has a <code>Resources<\/code> property of type <code>ResourceDictionary<\/code> where you declare reusable resources.<\/p>\n<pre class=\"lang:xhtml decode:true \">&lt;Application ...&gt;\n    &lt;Application.Resources&gt;\n        &lt;ResourceDictionary&gt;\n            &lt;Color x:Key=\"PageBackgroundColor\"&gt;Yellow&lt;\/Color&gt;\n            &lt;Color x:Key=\"HeadingTextColor\"&gt;Black&lt;\/Color&gt;\n            &lt;Color x:Key=\"NormalTextColor\"&gt;Blue&lt;\/Color&gt;\n            &lt;Style x:Key=\"LabelPageHeadingStyle\" TargetType=\"Label\"&gt;\n                &lt;Setter Property=\"FontAttributes\" Value=\"Bold\" \/&gt;\n                &lt;Setter Property=\"HorizontalOptions\" Value=\"Center\" \/&gt;\n                &lt;Setter Property=\"TextColor\" Value=\"{StaticResource HeadingTextColor}\" \/&gt;\n            &lt;\/Style&gt;\n        &lt;\/ResourceDictionary&gt;\n    &lt;\/Application.Resources&gt;\n&lt;\/Application&gt;\n<\/pre>\n<p>You might define colors and styles that you wish to use throughout your application at the <code>Application<\/code> level in this code.<\/p>\n<p>If you&#8217;re really industrious, you might have discovered that you merge one dictionary with another using the <code>MergedWith<\/code> property.<\/p>\n<pre class=\"marking:true lang:xhtml mark:2,4 decode:true \">&lt;Application ...\n    xmlns:local=\"using:MyApplication\"&gt;\n    &lt;Application.Resources&gt;\n        &lt;ResourceDictionary MergedWith=\"local:Colors\"&gt;\n        &lt;Style x:Key=\"LabelPageHeadingStyle\" TargetType=\"Label\"&gt;\n            &lt;Setter Property=\"FontAttributes\" Value=\"Bold\" \/&gt;\n            &lt;Setter Property=\"HorizontalOptions\" Value=\"Center\" \/&gt;\n            &lt;Setter Property=\"TextColor\" Value=\"{StaticResource HeadingTextColor}\" \/&gt;\n        &lt;\/Style&gt;\n        &lt;\/ResourceDictionary&gt;\n    &lt;\/Application.Resources&gt;\n&lt;\/Application&gt;\n<\/pre>\n<p>In this example the primary dictionary is being merged with another resource dictionary of colors.<\/p>\n<pre class=\"marking:true lang:xhtml mark:5 decode:true\">&lt;?xml version=\"1.0\" encoding=\"utf-8\" ?&gt;\n&lt;ResourceDictionary\n    xmlns=\"http:\/\/xamarin.com\/schemas\/2014\/forms\"\n    xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2009\/xaml\"\n    x:Class=\"MyApplication.Colors\"&gt;\n    &lt;Color x:Key=\"PageBackgroundColor\"&gt;Yellow&lt;\/Color&gt;\n    &lt;Color x:Key=\"HeadingTextColor\"&gt;Black&lt;\/Color&gt;\n    &lt;Color x:Key=\"NormalTextColor\"&gt;Blue&lt;\/Color&gt;\n&lt;\/ResourceDictionary&gt;\n<\/pre>\n<p>Using a <code>ResourceDictionary<\/code> in this fashion requires a code file to declare the class.<\/p>\n<pre class=\"lang:c# decode:true\">...\n\npublic partial class Colors : ResourceDictionary\n{\n  ...\n}\n<\/pre>\n<h3>In with the New<\/h3>\n<p>This syntax gets much cleaner with the new implementation of merged dictionaries, and you can merge multiple dictionaries instead of just the one. Instead of using <code>MergedWith<\/code>, you now add to a new <code>MergedDictionaries<\/code> collection property.<\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;Application ...\n    xmlns:local=\"using:MyApplication\"&gt;\n    &lt;Application.Resources&gt;\n        &lt;ResourceDictionary&gt;\n            &lt;ResourceDictionary.MergedDictionaries&gt;\n                &lt;local:Colors \/&gt;\n                &lt;!-- Add more resource dictionaries here --&gt;\n            &lt;\/ResourceDictionary.MergedDictionaries&gt;\n            &lt;Style x:Key=\"LabelPageHeadingStyle\" TargetType=\"Label\"&gt;\n                &lt;Setter Property=\"FontAttributes\" Value=\"Bold\" \/&gt;\n                &lt;Setter Property=\"HorizontalOptions\" Value=\"Center\" \/&gt;\n                &lt;Setter Property=\"TextColor\" Value=\"{StaticResource HeadingTextColor}\" \/&gt;\n            &lt;\/Style&gt;\n        &lt;\/ResourceDictionary&gt;\n    &lt;\/Application.Resources&gt;\n&lt;\/Application&gt;\n<\/pre>\n<blockquote style=\"text-align: left;font-size: 12px;padding: 10px 20px;border-left: 3px solid #2c3e50\"><p>A big thanks to Microsoft MVP <a href=\"https:\/\/twitter.com\/adpedley?lang=en\">Adam Pedley<\/a> for his proposal and initial pull request for merged dictionaries and for working with us to make this happen.<\/p><\/blockquote>\n<h2>Upcoming ResourceDictionary Improvements<\/h2>\n<p>The next major version of Xamarin.Forms will introduce several improvements to <code>ResourceDictionary<\/code> in XAML, which you can get a taste for now in the <a href=\"https:\/\/github.com\/xamarin\/\/wiki\/Nightly-Builds\">nightly build feed<\/a>.<\/p>\n<p><strong>Pure XAML ResourceDictionaries<\/strong>\nYou\u2019ll notice the <code>Colors.xaml<\/code> needs to have a partial class to accompany it in the example above, even though it\u2019s totally empty. That\u2019s no longer required in the nightly build, where you can declare a XAML file without any Class and it will still be compiled with XAML.<\/p>\n<p>Here\u2019s another file <code>Colors.xaml<\/code> example, which contains all the colors to be used in an application. The\u00a0<span class=\"lang:xhtml decode:true  crayon-inline \">&lt;?xaml-comp compile=&#8221;true&#8221; ?&gt;<\/span>\u00a0 does the magic.<\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;?xml version=\"1.0\" encoding=\"utf-8\" ?&gt;\n&lt;?xaml-comp compile=\"true\" ?&gt;\n&lt;ResourceDictionary\n    xmlns=\"http:\/\/xamarin.com\/schemas\/2014\/forms\"\n    xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2009\/xaml\"&gt;\n    &lt;Color x:Key=\"AccentColor\"&gt;#FF4B14&lt;\/Color&gt;\n    &lt;Color x:Key=\"BlackOpacityColor\"&gt;#99253748&lt;\/Color&gt;\n    &lt;Color x:Key=\"BlackTextColor\"&gt;#253748&lt;\/Color&gt;\n    &lt;Color x:Key=\"BackgroundColor\"&gt;#F8F8F8&lt;\/Color&gt;\n    &lt;Color x:Key=\"PinkColor\"&gt;#ED0241&lt;\/Color&gt;\n    &lt;Color x:Key=\"GrayColor\"&gt;#ACB1B4&lt;\/Color&gt;\n    &lt;Color x:Key=\"DarkColor\"&gt;#203446&lt;\/Color&gt;\n    &lt;Color x:Key=\"WhiteColor\"&gt;#FFFFFF&lt;\/Color&gt;\n    &lt;Color x:Key=\"GreenColor\"&gt;#368F95&lt;\/Color&gt;\n&lt;\/ResourceDictionary&gt;\n<\/pre>\n<p><strong>More Concise Syntax<\/strong>\nXAML can get pretty verbose as you declare all the resources for your application, so we\u2019ve done some work to cut the corners and save some keystrokes. Instead of writing:<\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;ContentPage.Resources&gt;\n  &lt;ResourceDictionary&gt;\n    &lt;ResourceDictionary.MergedDictionaries&gt;\n        &lt;ResourceDictionary Source=\"Colors.xaml\" \/&gt;\n    &lt;\/ResourceDictionary.MergedDictionaries&gt;\n    &lt;StyleSheet Source=\"style.css\" \/&gt;\n    &lt;Color x:Key=\"notgreen\"&gt;FF00FF&lt;\/Color&gt;\n  &lt;\/ResourceDictionary&gt;\n&lt;\/ContentPage.Resources&gt;\n<\/pre>\n<p>You can simply write:<\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;ContentPage.Resources&gt;\n  &lt;ResourceDictionary Source=\"Colors.xaml\" \/&gt;\n  &lt;StyleSheet Source=\"style.css\" \/&gt;\n  &lt;Color x:Key=\"notgreen\"&gt;FF00FF&lt;\/Color&gt;\n&lt;\/ContentPage.Resources&gt;\n<\/pre>\n<p>In this new syntax:<\/p>\n<ul>\n<li><strong>Source<\/strong>: You can now load your dictionary with the new <code>Source<\/code> property. The file should be relative to the document or absolute from the application root<\/li>\n<li>No need to specify a top level <code>&lt;ResourceDictionary&gt;<\/code>, as it&#8217;s now created on demand<\/li>\n<li>No need to specify <code>&lt;ResourceDictionary.MergedDictionaries&gt;<\/code> if your <code>StyleSheet<\/code> or <code>ResourceDictionary<\/code> is keyless<\/li>\n<\/ul>\n<p>These improvements provide more flexibility for you to organize your application resources and compose them on a control however you need them.<\/p>\n<h2>Be Resourceful<\/h2>\n<p>Multiple merged dictionaries are available today on NuGet in the Xamarin.Forms 2.5.0 stable release. Be sure to also check out the <a href=\"https:\/\/github.com\/xamarin\/\/wiki\/Nightly-Builds\">nightly feed<\/a> and explore the new features we have on the horizon.<\/p>\n<p>Do you have an idea or request like Adam\u2019s that you\u2019d like to contribute? Start by exploring the <a href=\"https:\/\/github.com\/xamarin\/\/projects\/5\">enhancements project board<\/a> on GitHub, and then open an <a href=\"https:\/\/github.com\/xamarin\/\/issues\/new?labels=enhancement,proposal-open&amp;body=%23%23Summary%0APlease%20provide%20a%20brief%20summary%20of%20your%20proposal.%20Two%20to%20three%20sentences%20is%20best%20here.%0A%0A%23%23API%20Changes%0A%0AInclude%20a%20list%20of%20all%20API%20changes%2C%20additions%2C%20subtractions%20as%20would%20be%20required%20by%20your%20proposal.%20These%20APIs%20should%20be%20considered%20placeholders%2C%20so%20the%20naming%20is%20not%20as%20important%20as%20getting%20the%20concepts%20correct.%20If%20possible%20you%20should%20include%20some%20%22example%22%20code%20of%20usage%20of%20your%20new%20API.%0A%0Ae.g.%0A%0AIn%20order%20to%20facilitate%20the%20new%20Shiny%20Button%20api%2C%20a%20bool%20is%20added%20to%20the%20Button%20class.%20This%20is%20done%20as%20a%20bool%20because%20it%20is%20simpler%20to%20data%20bind%20and%20other%20reasons...%0A%0A%20%20%20%20var%20button%20%3D%20new%20Button%20()%3B%0A%20%20%20%20button.MakeShiny%20%3D%20true%3B%20%2F%2F%20new%20API%0A%0AThe%20MakeShiny%20API%20works%20even%20if%20the%20button%20is%20already%20visible.%0A%0A%23%23Intended%20Use%20Case%0AProvide%20a%20detailed%20example%20of%20where%20your%20proposal%20would%20be%20used%20and%20for%20what%20purpose.\">enhancement proposal<\/a>.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Resource Dictionary is a foundational building block to all Xamarin.Forms mobile development. It&#8217;s the primary vehicle for declaring XAML resources you\u2019ll use throughout your mobile application, including everything from styles to value converters, content templates, and data templates. In the most recent stable release of Xamarin.Forms, we gained the ability to merge multiple resource dictionaries [&hellip;]<\/p>\n","protected":false},"author":553,"featured_media":39167,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[4,16],"class_list":["post-35031","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-xamarin-platform","tag-xamarin-forms"],"acf":[],"blog_post_summary":"<p>Resource Dictionary is a foundational building block to all Xamarin.Forms mobile development. It&#8217;s the primary vehicle for declaring XAML resources you\u2019ll use throughout your mobile application, including everything from styles to value converters, content templates, and data templates. In the most recent stable release of Xamarin.Forms, we gained the ability to merge multiple resource dictionaries [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/35031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/553"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=35031"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/35031\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/39167"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=35031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=35031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=35031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}