title: Xamarin.Forms setting Image Source in shared project
body: <p>I have a Xamarin.Forms solution with Android and iOS projects.<br />
  I want the same images to be in both applications.<br />
  I know I can add the same image to both projects code seperately, but that doesn't sound very elegant.<br />
  I want to have only one image referred to by both projects, from the code in the shared project.</p>
  <p>My images are in the shared project, and are <code>Embedded Resource</code></p>
  <pre><code>SharedProject
  |
  - Resources
    |
    - icon192.png
  </code></pre>
  <p>My XAML (shared project)</p>
  <pre><code>&lt;ContentPage xmlns:myExtensions=&quot;clr-namespace:LibraryApp.Extensions;assembly=App&quot;&gt;
  
      &lt;Image x:Name=&quot;image2&quot;
         VerticalOptions=&quot;Center&quot; HeightRequest=&quot;50&quot; Aspect=&quot;AspectFit&quot;  /&gt;
  
      &lt;Image Source=&quot;{myExtensions:ImageResourceExtension Resources.icon192.png}&quot;
         VerticalOptions=&quot;Center&quot; HeightRequest=&quot;50&quot; Aspect=&quot;AspectFit&quot;  /&gt;
  
  &lt;/ContentPage&gt;
  </code></pre>
  <p>My code-behind (shared project)</p>
  <pre><code>public partial class AboutPage : ContentPage
  {
      public AboutPage()
      {
          InitializeComponent();
  
          image2.Source = ImageSource.FromResource(&quot;LibraryApp.AndroidClient.Resources.icon192.png&quot;);
      }
  }
  </code></pre>
  <p>The first Image, <code>image2</code> <strong>works perfectly</strong> when set from code-behind.<br />
  But I don't want to have to deal with images in code-behind, because they are a presentation detail, not implementation.</p>
  <p>So my extension method to try and solve this</p>
  <pre><code>namespace LibraryApp.Extensions
  {
      [Preserve(AllMembers = true)]
      [ContentProperty(nameof(Source))]
      public class ImageResourceExtension : IMarkupExtension
      {
          public string Source { get; set; }
  
          public object ProvideValue(IServiceProvider serviceProvider)
          {
              if (Source == null) return null;
  
              // do some work to get the name of the correct assembly/project, here
              // var stream = Assembly.GetCallingAssembly().GetManifestResourceStream(Source);
  
              // just for testing, return something we know that works
              return ImageSource.FromResource(&quot;LibraryApp.AndroidClient.Resources.icon192.png&quot;);
          }
      }
  }
  </code></pre>
  <p>This extension method <em>does</em> get called from the other image (with the correct <code>Source</code> parameter).<br />
  Even though the code is effectively <em>exactly</em> the same, I cannot get an image using the extension method.</p>
  <p>I get a <code>FileImageSourceHandler: Could not find image or image file was invalid</code>.</p>
  <p>How would you solve this problem?</p>