Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Embedded Resource Image Not Displaying in app

AndrewNovakAndrewNovak USMember
edited May 2017 in Xamarin.Forms

I just started learning xamarin and I can't get a local image to display. I'm following a tutorial and looking at the documentation, but I"m still having issues. I don't see what I'm doing different from the documentation. I named the image an embedded resource in properties and I set the page as the default page in app.xaml.cs. All I get is a blank app with no image or anything.

    `   MainPage = new Images.LocalImage();`

I've been stuck on this for a few days now so any help is greatly appreciated.

Screenshot showing image path and property settings

LocalImage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Images.LocalImage">

    <Image x:Name="myLocalImage" Aspect="AspectFit"/>

</ContentPage>  

LocalImage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Images
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class LocalImage : ContentPage
    {
        public LocalImage ()
        {
            InitializeComponent ();



            myLocalImage.Source = ImageSource.FromResource("Images.background.jpg");


        }
    }
}
Tagged:

Best Answer

Answers

  • AndrewNovakAndrewNovak USMember

    That was extremely helpful and good to know for the future. The console outputted

    [0:] found resource: Images.Droid.background.jpg
    

    I changed the path to Images.Droid.background.jpg and it outputted image to screen.

    My question now is if I"m creating a Xamarin Forms app, how can I use shared images between Android and IOS. If it's looking in the Android resource folder (I'm assuming) I have a feeling the IOS app won't look in the Droid folder. Can IOS and Android not share images? Because my goal is to have both platforms share one image which is why I put background.jpg in root directory and not in Android or IOS folder like you see in pic above. Also the background.jpg image in root directory didn't show up in console with added code with all the other "found resources" Why is that?

  • fourwheelerfourwheeler USMember ✭✭

    @AndrewNovak

    Did you solve the inconsistency problem? I encountered the same problem and had to solve by the same way as you did -- put "Droid" in the name tag. However, isn't it the point of shared project to "share" resources?

  • capitolcapitol Member ✭✭

    @JohnMiller said:
    @AndrewNovak,

    Try this debugging step to troubleshoot:

    There is likely some difference here because you are using a Shared project instead of a PCL.

    This is not helpful. The debugging section doesn't point to where you should enter that code and "typeof(EmbeddedImages)" doesn't exist. Where should that type be too?

  • RanjanPRanjanP Member
    I am facing similar issue. I put the image in the .net standard project and not in droid or ios. I referenced the image like

    myLocalImage.Source = ImageSource.FromResource("{project_name}.{image_folder_name}.{image_name}");

    When running Android, the images appear but if i run ios it does not!

    Any help?
  • stuart_Astuart_A Member ✭✭
    edited November 2020

    @capitol said:
    @JohnMiller said:
    @AndrewNovak,

    Try this debugging step to troubleshoot:

    There is likely some difference here because you are using a Shared project instead of a PCL.

    This is not helpful. The debugging section doesn't point to where you should enter that code and "typeof(EmbeddedImages)" doesn't exist. Where should that type be too?

    I know this is an old discussion, but I encountered a similar problem lately.

    It turns out typeof(EmbeddedImages) is just an example for the documentation. I was so perplexed by this that I submitted an issue to Microsoft and they've now changed the docs so it says typeof(MyClass) instead. See https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=windows#embedded-images

    Either way, the typeof part isn't hugely necessary anyway. It's just for getting the correct assembly for loading the image, and the fastest way to do that is Assembly currentAssembly = Assembly.GetExecutingAssembly();

    Then you can get your image as so:

    Assembly currentAssembly = Assembly.GetExecutingAssembly();
    
    Image embeddedImage = new Image
    {
        Source = ImageSource.FromResource("myFolder.mySubfolder.myImage.jpg", currentAssembly)
    };
    
Sign In or Register to comment.