2
votes

I've a Xamarin Forms project - with the 3 projects for UWP, iOS and Android. Now I want to use images in my Xamarin Pages. As intended by Xamarin I want to use the same code for all three targets ... e.g.:

<Image Source="Assets/appIcon-256.png"/>

This works for iOS and UWP - both have a folder "Assets" where I placed the image. Ok, I've to maintain the image twice, but I've the same XAML code for both project.

But for Android, that doesn't seems to work. The image is not displayed in Android, if I put the png in the Assets folder of the Anroid project.

What can I do to use the same location for all images for all the three projects?

3
Try to avoid hyphens for Android, it acts weird for some reasonGerald Versluis
Spaces, hyphens, and periods are not allowed : stackoverflow.com/a/43651220/4984832SushiHangover
there is an entire doc that specifies how to handle images in Forms - developer.xamarin.com/guides/xamarin-forms/user-interface/…Jason

3 Answers

4
votes

It's very easy.

IOS => You need put the images on "Resources" (note: no more routes) , for ANDROID => you need put the images on "Resources/drawable" .for UWP => put the images on root project UWP.

On Diferents platforms the images have diferents routes.

Then on the xaml files you can put <Image Source="imageName.jpg" />

Send feedback please.

UPDATE

You can put the images on UWP/Assets and then :

<ToolbarItem
        Text="Search">
        <ToolbarItem.Icon>
            <OnPlatform x:TypeArguments="FileImageSource">
                <On Platform="Android, iOS" Value="movies-search.png"/>
                <On Platform="UWP" Value="Assets/movies-search.png"/>
            </OnPlatform>
        </ToolbarItem.Icon>
    </ToolbarItem>
2
votes

Why the path starting with "Assets/"? For Android Place your image to MyProject.Android/Resources/drawable. For iOS myProject.iOS/Resources/ and in xaml

<Image Source="appIcon-256.png"/>

It will work. In addition for iOS if you have same picture with different sizes, you should rename them like "appIcon-256.png" "[email protected]" "[email protected]"

0
votes

Replace your dashes with underscores. Example, ic_launcher_calendar.png

Try also to remove the digits at the end of the name.

Lastly, you can check out the naming conventions of drawable. http://petrnohejl.github.io/Android-Cheatsheet-For-Graphic-Designers/

Let me know if it works.