adamasan
adamasan

Reputation: 1182

How to load an image to a ImageCell on Xamarin.Forms?

I'm trying to load icons into a ImageCell inside a ListView, but when I build my project the icon doesn't appear on the ImageCell.

Here's my project structure:

enter image description here

Here's my XAML page:

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

  <MasterDetailPage.Master Title="Menu">
    <ContentPage Title="Menu">
      <StackLayout VerticalOptions="FillAndExpand">
        <ListView x:Name="listViewMaster" VerticalOptions="FillAndExpand">
          <ListView.ItemTemplate>
            <DataTemplate>
              <ImageCell Text="{Binding Title}" ImageSource="{Binding IconSource}" />
            </DataTemplate>
          </ListView.ItemTemplate>
        </ListView>
      </StackLayout>
    </ContentPage>
  </MasterDetailPage.Master>

  <MasterDetailPage.Detail>
    <ContentPage>

    </ContentPage>
  </MasterDetailPage.Detail>

</MasterDetailPage>

And here's the code behind:

using System.Collections.Generic;
using Xamarin.Forms;

namespace MyNotes
{
    public partial class StartPage : MasterDetailPage
    {
        public StartPage ()
        {
            InitializeComponent ();

            List<MasterPageItem> masterPageItems = new List<MasterPageItem>()
            {
                new MasterPageItem()
                {
                    Title = "Contacts",
                    IconSource = ImageSource.FromFile(@"c:\Projetos\MyNotes\MyNotes\MyNotes\Resources\image\contacts.png"),
                    TargetType = typeof(ContactPage)
                },
                new MasterPageItem()
                {
                    Title = "Todo List",
                    IconSource = ImageSource.FromFile(@"Resources\image\todo.png"),
                    TargetType = typeof(TodoListPage)
                },
                new MasterPageItem()
                {
                    Title = "Reminder",
                    IconSource = ImageSource.FromResource("Resources.image.reminder.png"),
                    TargetType = typeof(ContactPage)
                }
            };

            listViewMaster.ItemsSource = masterPageItems;
        }
    }
}

What am I doing wrong?

Upvotes: 1

Views: 9464

Answers (2)

SushiHangover
SushiHangover

Reputation: 74104

The images go into your "native" projects:

  • Xamarin.iOS = BundledResource Build Type

  • Xamarin.Android = AndroidResource Build Type


IconSource = ImageSource.FromFile("SanFran.jpg")

or using the implicit conversion:

IconSource = "SanFran.jpg";

enter image description here

Working with Images

Upvotes: 4

Avi K.
Avi K.

Reputation: 1802

Have you marked the images that you added to the project as embedded reousrces?

embedded resource

Upvotes: 2

Related Questions