How do I display an image in a Xamarin Forms Image

2019-08-01 13:32发布

问题:

I'm trying to display an image at the front of my image cell in Xamarin Form. Specifically I'm trying this in iOS at the moment and I have added an image to my iOS project's resource folder and have ensured that the image file is set to 'BundledResource. The image is not any particular size (it's quite large at the moment).

I'm creating all of my table cells via an MVVM approach so I have a factory that creates the cell as follows:

return new ImageCell() { ImageSource = "logo.png" };

I have hardcoded the image source or now as a local png image which is NOT 60x60 (does this matter?).

When displayed, no placeholder for the image, or the image itself appears. Do I need to set an additional flag somewhere to display it? There is no margin to display it in my table view.

EDIT: My image cell factory code looks like this:

public class ImageCellFactory : CellFactory<ImageCellViewModel>
{
    protected override Cell OnCreateCell(ImageCellViewModel model)
    {
        return new ImageCell();
    }

    protected override void OnSetBindings(Cell cell)
    {

        cell.SetBinding(TextCell.TextProperty, "Text");
        cell.SetBinding(TextCell.DetailProperty, "Detail");
        cell.SetBinding(ImageCell.ImageSourceProperty, new Binding("Icon", BindingMode.OneWay));
    }
}

I've tried directly setting the image source as per my previous code (thus removing the bindings and so on). I've tried with and without binding mode being set. I can see that the ImageSource is creating an instance of the FileImageSource and I've even set the icon to be 30x30 just in case that helps.

For the 'Icon' property of the view model I have tried ImageSource and just a straight string to invoke the standard converter.

Even with a direct TableView definition set up in Xaml with no funny business in the code-behind my images do not appear.

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="TesterApp.CodedTableView">
<ContentPage.Content>
    <TableView>
        <TableSection>  
            <ImageCell Text="My Logo" ImageSource="logo.jpg"/>  
        </TableSection>
    </TableView>
</ContentPage.Content>

My logo is in the 'Resources' folder of my iOS application project And is set to a bundled resource. The above image source is the exact casing for the filename and extension.