Juan Francisco
Juan Francisco

Reputation: 179

Load images from a PCL into XAML

I'm trying to load an image from XAML in a WP8 project. The image is in another assembly which it's a portable class library. I'm using this to load the image:

<ImageBrush ImageSource="/PortableClassLibrary1;component/Assets/Images/Background.jpg" />

but the image is not loaded. Is it possible to load it?

Upvotes: 2

Views: 830

Answers (1)

Anton Sizikov
Anton Sizikov

Reputation: 9240

This is not possible in general, you are trying to access to image inside PCL using the Uri formatted as Windows Phone one.

The idea of PCL is to avoid platform specific parts and provide a common way to work with code. As a result images and other resources are not included.

You can move images to WP project. Anyway there is no sence to use them in W8 application. You need higher resolution images and so on.

There is a nice article about resources in PCL. http://msdn.microsoft.com/en-us/library/hh871422.aspx

The main idea is - you can store only string-based resorces inside:

The exclusion of other ResourceManager members from the Portable Class Library means that serialized objects, non-string data, and images cannot be retrieved from a resource file. You may be able to work around this limitation by storing object data in string form. For example, you can store numeric values in a resource file by converting them to strings, and you can retrieve them and then convert them back to numbers by using the numeric data type's Parse or TryParse method. You can convert images or other binary data to a string representation by calling the Convert.ToBase64String method, and restore them to a byte array by calling the Convert.FromBase64String method.

p.s. Here, you can find a long thread about it. There are some "solutions", but I'd prefer to store images in platform-specific assemblies if it's possible.

Upvotes: 2

Related Questions