0

I have a problem with a Xamarin forms app crashing in Android after getting images from the web. Everything works fine if I use this code:

Image myImage = new Image
{
   Source = ImageSource.FromUri(new Uri(myObj.ImagePath1)),
};

When I get the images for a listview using a custom renderer for an image cell is crashes pretty quickly.

This is my code for the listview with databindings

listView.ItemTemplate = new DataTemplate
  (typeof(MyImageCell))
    Bindings = {
        {TextCell.TextProperty,
            new Binding("MyTitle")},
        {TextCell.DetailProperty,
            new Binding("MyAddress")},
      {ImageCell.ImageSourceProperty,
         new Binding("MyURL")},
  }
};

I am new to C# and all things Xamarin and I have no idea how I declare it like the first example and still use it in the binding code. I have tried many different ways but nothing seems to work. Hopefully it something simple that I am missing.

Cheers

user1667474
  • 819
  • 6
  • 24
  • 37

1 Answers1

0

You are creating complex rows, right. In that case you won't use primitive cells but instead ViewCell with complex layout (i.e. StackLayout). Here is a sample that creates a StackLayout with a bound Image and an unbound Label (you could bind that as well). My ItemsSource is an array of Src instances (that'd be your MyImageCell probably) where Src.Source is a string containing an URL to an image.

        ListView lv = new ListView();
        lv.ItemTemplate = new DataTemplate(() =>
            {
                Image image = new Image();
                image.SetBinding(Image.SourceProperty, "Source");

                return new ViewCell
                {
                    View = new StackLayout
                    {
                        Children = {
                            image, new Label{ Text = "Tubo"}
                        }
                    }
                };
            });
        lv.ItemsSource = new Src[] { new Src(), new Src() };
        ...

public class Src
{
    public string Source { get; set; }

    public Src()
    {
        Source = "http://blog.rthand.com//images/Logo_vNext.png";
    }
}
Miha Markic
  • 3,158
  • 22
  • 28
  • Thanks for you reply. That doesn't seem to make any difference. It seems that it needs to be wrapped in ImageSource.FromUri(new Uri(ImagePath)) i tried having an image in the object, that uses the object's string ImagePath but couldn't get it to work – user1667474 Sep 27 '14 at 08:50
  • Path as string is enough since there is an implicit conversion. Perhaps you are having another problem? http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/images/ – Miha Markic Sep 27 '14 at 11:45
  • Must be. Thanks Miha. Will look more closely – user1667474 Sep 28 '14 at 01:34
  • it is getting quite a few images for the list. Do you think caching them would help? – user1667474 Sep 28 '14 at 01:46
  • It seems I have the same problem as here -http://stackoverflow.com/questions/25807196/xamarin-forms-listview-outofmemeoryerror-on-android?rq=1 – user1667474 Sep 28 '14 at 08:44