0

Visual Studio Project Image Brand new to Xamarin and I have wasted more than a day trying to get a simple image to display on a simple page.

I am using Visual Studio 2017.

The image is set as an embedded resource (see the attached image). There are no errors when run, the image simply doesn't display.

I have tried the following:

<Image Source="XF0003.Assets.logo.png" />
<Image Source="Assets.logo.png" />
<Image Source="logo.png" />
<Image Source="{local:ImageResource XF0003.Assets.logo.png}" />
<Image Source="{local:ImageResource Assets.logo.png}" />
<Image Source="{local:ImageResource logo.png}" />

XAML Page Code:

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

    <StackLayout>
        <Image Source="Assets.logo.png" />
        <Label Text="Username" />
        <Entry x:Name="usernameEntry" Placeholder="username" />
        <Label Text="Password" />
        <Entry x:Name="passwordEntry" IsPassword="true" />
        <Button Text="Login" Clicked="PageOne" />
        <Label x:Name="messageLabel" />
    </StackLayout>
</ContentPage>

XAML.CS Page Code:

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

namespace XF0003
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private async void PageOne(object sender, EventArgs e)
        {
            Navigation.InsertPageBefore(new Page1(), this);
            await Navigation.PopAsync();
        }
    }
    [ContentProperty("Source")]
    public class ImageResourceExtension : Xamarin.Forms.Xaml.IMarkupExtension
    {
        public string Source { get; set; }

        public object ProvideValue(IServiceProvider serviceProvider)
        {
            if (Source == null)
            {
                return null;
            }
            // Do your translation lookup here, using whatever method you require
            var imageSource = ImageSource.FromResource(Source);

            return imageSource;
        }
    }
}
2
  • You forgot to add the image Commented Jun 2, 2017 at 11:11
  • Not sure what happened, but I added the image again to my post...
    – jf974
    Commented Jun 2, 2017 at 18:58

1 Answer 1

2

In the image you linked, it looks like you only saved the image in your xamarin.forms project. I believe the images need to be saved in their respective projects.

i.e.

 XF0003.Droid/Resources/Drawable (For Android)

 XF0003.iOS/Resources (For iOS)

Make sure your images have the same name.

For the Android pictures make the build action AndroidResource. For the iOS pictures make the build action BundleResource.

DoNotCopy for both.

When you refer to the image, just use the [name].[filetype]

<Image Source="logo.png" />

It's also explained in great detail here.

5
  • I took most of my code from the Embedded Images section of the link you provided. From that section of the page, the images appear to be the project level, not at an individual project level. However, I moved the image to the Android/Resources/Drawable folder, set it to AndroidResources, and still do not get an image to display. I used both: <Image Source="{local:ImageResource XF0003.Android.Resources.Drawable.logo.png}" /> and <Image Source=" XF0003.Android.Resources.Drawable.logo.png" />. Neither one showed the image.
    – jf974
    Commented Jun 2, 2017 at 19:34
  • I'm not sure what you are referring to. Here is a link to the exact section that explains it. I used this same page for my code as well. developer.xamarin.com/guides/xamarin-forms/user-interface/…
    – mac10688
    Commented Jun 2, 2017 at 19:36
  • 1
    When you refer to the image in xamarin forms, just refer to it was logo.png
    – mac10688
    Commented Jun 2, 2017 at 19:37
  • I don't know about all the local:ImageResource. I didn't use that. And in your code above, I don't know what the ImageResourceExtension is for. I don't think that method is necessary.
    – mac10688
    Commented Jun 2, 2017 at 19:38
  • Yes, thank you, just using the Source="logo.png", after moving the image to the project specific folder, worked. Thank you!
    – jf974
    Commented Jun 2, 2017 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.