Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Helpful Shared Project Image information

$
0
0

Fellow devs,

I just wanted to share some information in case it is helpful to you. I am exploring the Shared Projects approach for Xamarin.Forms. In my app, I wanted to be able to put images into the shared project and load them into my UI as needed (ie logos, etc.). This was not obvious to me at all.

I found a great bit of information here which provided most of the clues: link

Important information is this:
1. Images must be marked as an embedded resource (if in shared project)
2. In Shared Projects, the path to the image location translates at run-time to <Assembly Name> + . + imagename (note, this is not the same for PCLs)
3. In order to have shared code find the right image location at run-time, I did the following inside of my view class:

private string _baseResource;
public string BaseResource
{
  get
  {
    return _baseResource ?? (_baseResource = Assembly.GetExecutingAssembly().FullName.Split(',').FirstOrDefault());
  }
}

private ImageSource PlatformImageResource(string resource)
{
  return ImageSource.FromResource(BaseResource + "." + resource);
}

This allowed for me to load the image more easily and have it be platform specific, as follows:

  var logo = new Image
  {
    Source = PlatformImageResource("app_header_logo.jpg")
  };

If there's an easier way, I'd love to hear about it!

I hope this helps others : )

Thanks!
Michael


Viewing all articles
Browse latest Browse all 91519

Trending Articles