testing
testing

Reputation: 20279

Working with embedded images in a shared project

I want to add embedded images to my shared project (no PCL). I added the images to the shared project (not platform specific) as Embedded resource. I'm able to use the embedded images if I use the following:

ImageSource.FromResource("TestProject.Droid.Images.Document Folder.png")

Simply using the string

ProjectNamespace.Subfolder.Filename.ext

doesn't work as described by the docs. One has to add the platform namespace (iOS, Droid, ...).

Another option which did work is to edit .projitems. I added LogicalName:

<ItemGroup>
    <EmbeddedResource Include="$(MSBuildThisFileDirectory)Images\Document Folder.png">
        <LogicalName>Document Folder.png</LogicalName>
    </EmbeddedResource>
</ItemGroup>

but that isn't something which does work out of the box. There is no Resource ID. I also tried looking into the sample project, but that doesn't start up at all.

How can I use embedded images in my shared project?

Upvotes: 0

Views: 2015

Answers (1)

testing
testing

Reputation: 20279

The official documentation only handles the PCL project. For shared project you either have to use the full string or add the LogicalName.

Full string:

Load the resource from the full string

ImageSource.FromResource("TestProject.Droid.Images.Document Folder.png")

The string is build like this

<ProjectNamespace>.<PlatformNameSpace>.{<Subfolder>}.<Filename>.<Fileextension>

<Subfolder> is only used if you have the image in it and for each subfolder you have to add the string. So you only need a mechanism to get the full namespace for each platform. Perhaps you define a property which returns a different string (managed by yourself) depending on the platform.

LogicalName:

Edit .projitems and add LogicalName for each image:

<ItemGroup>
    <EmbeddedResource Include="$(MSBuildThisFileDirectory)Images\Document Folder.png">
        <LogicalName>Document Folder.png</LogicalName>
    </EmbeddedResource>
</ItemGroup>

Then you can do this

ImageSource.FromResource("Document Folder.png")

It has been mentioned that you can add the Resource ID directly in Visual Studio so that you don't need to add LogicalName by yourself, but that seems not to be true.

So there is no easier way. The difference between PCL and shared project is also explained here (in French).

Also the Working with files in Xamarin.Forms article explains some important details.

Upvotes: 0

Related Questions