0
votes

Attempting to add an image to my xamarin app ->

add image to project, then add code behind

public Products()
        {
            InitializeComponent();

            Products_List = new List<App1.Classes.Product>();
            Products_List.Add(new App1.Classes.Product
            {
                Id = 0001,
                Name = "Coors",
                ImageUrl = "App1.Images.Coors.jpg",
                Price = "£9.99"
            });
            Products_List.Add(new App1.Classes.Product
            {
                Id = 0002,
                Name = "Bud",
                ImageUrl = "App1.Images.Bud.jpg",
                Price = "£13.99"
            });
            Products_List.Add(new App1.Classes.Product
            {
                Id = 0003,
                Name = "Guinness",
                ImageUrl = "App1.Images.Coors.jpg",
               //have also tried-> ImageUrl = "App1\\Images\\Coors.jpg",
                Price = "£9.99"
            });

            BindingContext = this;


        }

then add front end ->

 <ListView ItemsSource="{Binding Products_List}" 
                  HasUnevenRows="true"
      ItemSelected="OnListViewItemSelected"
      ItemTapped="OnListViewItemTapped">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid Padding="10">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Image Grid.RowSpan="2"
                       Source="{Binding ImageUrl}"
                       Aspect="AspectFill"
                       HeightRequest="60"
                       WidthRequest="60" />
                            <Label Grid.Column="1"
                       Text="{Binding Name}"
                       FontAttributes="Bold"/>
                            <Label Grid.Row="1"
                       Grid.Column="1"
                       Text="{Binding Price}"
                       VerticalOptions="End" />

                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

So the text appears on screen but no image -> output window shows...image not found

1
thats great thanks for your help - John

1 Answers

0
votes

As Jason said that we put the image in Project.Android---Resources---drawable, then you can change image property for Build action as AndroidResource, then you can use

 Products_List.Add(new App1.Classes.Product
        {
            Id = 0001,
            Name = "Coors",
            ImageUrl = "Coors.jpg",
            Price = "£9.99"
        });

If my reply help you, please remember to mark my reply as answer, thanks.