Xamarin Forms ListView with F# part 2: Custom cell

As promised, this post is part 2 of the ListView series. If you missed part 1, I covered setting up a Xamarin Forms ListView with a data source as per the Xamarin guide. In this post, I’ll cover the next page in the Xamarin guide with a custom cell.

Getting started: add the xaml
Let’s get to it, create another forms project with F#, for this post I’ll use the name CustomSimpleList. Add the xaml file (see my part 1 if you’ve forgotten this), I’ll name this CustomListView.xaml and add the following:


Be sure to update the following items:

  • Class to have the correct namespace and name
  • Remove the line: 

    (details why are at the bottom).

Create some Types
With the xaml created, it’s time to create some types. As in the last post open, the fs file that was automatically created. For the main type I’ll use the name CustomCellPage, so update app to load this as main page (don’t forget to open the required libraries):

inherit Application(MainPage = CustomCellPage())

and then above that line add the type:

type CustomCellPage() =
    inherit ContentPage() 

    let _ = base.LoadFromXaml(typeof)
    let movies = [
        {Title = "Zootopia (2016)"; Rating = "98%"} 
        {Title = "Love & Friendship (2016)"; Rating = "99%"}
        {Title = "The Jungle Book (2016)"; Rating = "94%"}
        {Title = "Hunt for the Wilderpeople (2016)"; Rating = "98%"}
        {Title = "Finding Dory (2016)"; Rating = "94%"} ]
    let movieList = base.FindByName("listView")
    do movieList.ItemsSource <- ((movies |> List.toSeq) :> Collections.IEnumerable)

This should look familiar as the only thing that has changed is the record type.

Make it compile
To finish this small example, add the last required type above the CustomCellPage:

type Movie = {
    Title: string
    rating: string

With that last type added everything should compile and run now. Voila!

But where are my images
So the original guide from Xamarin had images, what’s up? Well, there’s a bug in F# with forms and Android (I’m not sure if this affects iOS. If some has tried it out I would love to know). I have filled a bug with xamarin for this and will add an update when I get one.

What’s actually broken
Following this guide using either local images or embedded images an exception is thrown when trying to load the image from the image source. There is an example available on github if you want the details, it uses local images. For embedded images, I also added the debugging code and verified that images are in the package. Additionally, I haven’t found a work around for this (maybe writing some custom UI for forms). Again leave me a comment if you have found a workaround or also encounter this bug.

That’s a wrap folks!