{"id":293,"date":"2018-04-24T20:21:41","date_gmt":"2018-04-25T01:21:41","guid":{"rendered":"http:\/\/blog.cubicleninja.com\/?p=293"},"modified":"2018-04-24T20:21:41","modified_gmt":"2018-04-25T01:21:41","slug":"xamarin-forms-imagesource-from-resource-files","status":"publish","type":"post","link":"https:\/\/blog.cubicleninja.com\/?p=293","title":{"rendered":"Xamarin.Forms ImageSource from Resource Files"},"content":{"rendered":"<p>This is a useful helper class for Xamarin.Forms that will allow you to easily reference a file asset that is tagged as an\u00a0<strong>Embedded Resource<\/strong> easily from your XAML mark-up.\u00a0 This is taken from the <a href=\"https:\/\/docs.microsoft.com\/en-us\/xamarin\/xamarin-forms\/user-interface\/images?tabs=vswin#Embedded_Images\">Xamarin.Forms documentation<\/a> and modified slightly because I was having issues with it properly loading the resource files after upgrading to the latest version.<\/p>\n<pre class=\"lang:c# decode:true \">    [Xamarin.Forms.Internals.Preserve(AllMembers = true)]\n    [Xamarin.Forms.ContentProperty(nameof(Source))]\n    class ImageResourceExtension : Xamarin.Forms.Xaml.IMarkupExtension\n    {\n        public String Source { get; set; }\n        \/\/ Store out the current assembly to use for referencing the resources\n        private static System.Reflection.Assembly execAssembly = System.Reflection.Assembly.GetExecutingAssembly();\n\n        public object ProvideValue(IServiceProvider serviceProvider)\n        {\n            Xamarin.Forms.ImageSource imageSource = null;\n\n            try\n            {\n                if (!String.IsNullOrEmpty(Source))\n                {\n                    \/\/ Load the named resource file from the current executing assembly\n                    imageSource = Xamarin.Forms.ImageSource.FromResource(Source, execAssembly);\n                }\n            }\n            catch (Exception err)\n            {\n                System.Diagnostics.Debug.WriteLine(err.Message);\n            }\n\n            return imageSource;\n        }\n    }<\/pre>\n<p>Once that is in place, you use it the same as the Xamarin.Forms documentation:<\/p>\n<ol>\n<li>Add a custom xmlns\u00a0to your XAML page referencing your namespace and assembly:\n<pre class=\"lang:c# decode:true\">xmlns:local=\"clr-namespace:MyNameSpace;assembly=MyAssembly\"<\/pre>\n<\/li>\n<li>Use your custom xmlns\u00a0as the source for your Image\n<pre class=\"lang:c# decode:true\">&lt;Image Aspect=\"AspectFit\" Source=\"{local:ImageResource MyNamespaceForResources.filename.ext}\"&gt;&lt;\/Image&gt;<\/pre>\n<p><strong>(keep in mind that if you created a folder for your embedded resources, that will be part of the resource namespace)<\/strong><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This is a useful helper class for Xamarin.Forms that will allow you to easily reference a file asset that is tagged as an\u00a0Embedded Resource easily from your XAML mark-up.\u00a0 This is taken from the Xamarin.Forms documentation and modified slightly because I was having issues with it properly loading the resource files after upgrading to the &hellip; <a href=\"https:\/\/blog.cubicleninja.com\/?p=293\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Xamarin.Forms ImageSource from Resource Files&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,12],"tags":[],"class_list":["post-293","post","type-post","status-publish","format-standard","hentry","category-csharp","category-xamarin"],"_links":{"self":[{"href":"https:\/\/blog.cubicleninja.com\/index.php?rest_route=\/wp\/v2\/posts\/293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.cubicleninja.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.cubicleninja.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.cubicleninja.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.cubicleninja.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=293"}],"version-history":[{"count":0,"href":"https:\/\/blog.cubicleninja.com\/index.php?rest_route=\/wp\/v2\/posts\/293\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.cubicleninja.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.cubicleninja.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.cubicleninja.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}