【问题标题】:xamarin.forms bind listview-imagecell imagesource to byte[]xamarin.forms 将 listview-imagecell 图像源绑定到 byte[]
【发布时间】:2014-09-26 19:45:15
【问题描述】:

我目前正在开始使用 Xamarin.Forms。我的页面上有一个 ListView,我绑定到我的 ViewModel。 ItemTemplate 的类型为“ImageCell”

绑定单元格的Text和Detail属性没有问题。但是,我无法绑定“ImageSourceProperty”。这是使用 byte[] 生成的图像源(我的图像是 SQLite 数据库中的 blob)

我想知道是否有人知道如何解决这个问题(或将 byte[]-image 绑定到 listview-item 的另一种方法)

这里有一些源代码:

var model = Graanziekten.Select(g => new OnkruidViewModel
            {
                Id = g.Id, Naam = g.Naam, Omschrijving = g.Omschrijving, Afbeelding = g.BitmapThumbnail
            }).ToList();

            var cell = new DataTemplate(typeof(ImageCell));
            cell.SetBinding(TextCell.TextProperty, "Naam");
            cell.SetBinding(TextCell.DetailProperty, "Omschrijving");
            cell.SetBinding(ImageCell.ImageSourceProperty, "Afbeelding");

            var listview = new ListView
            {
                ItemsSource = model,
                ItemTemplate = cell
            };

“BitmapThumbnail”属性定义为:

public ImageSource BitmapThumbnail
        {
            get
            {
                //AfbeeldingSmall is a byte[]
                return ImageSource.FromStream(() => new MemoryStream(Afbeeldingen.First().AfbeeldingSmall));
            }
        }

如果我使用虚拟图像(来自 uri),它可以正常工作。但是,如果我使用上面显示的代码,则根本不会呈现内容页面(黑屏)。

起初我认为问题可能与 byte[] 是从属性中动态获取的事实有关,但是当我获取所有必要的 byte[] 时,也会出现同样的效果。

此外,当我将单个图像添加到我的内容页面时,使用相同的方法它确实有效。只是不在列表视图中。

我正在尝试在 WinPhone8 上执行此操作(尽管我认为平台并不重要)

提前致谢。

【问题讨论】:

    标签: c# windows xamarin cross-platform xamarin.forms


    【解决方案1】:

    您是否尝试将其直接绑定到您的列表?而不是加载该模型对象。

        var cell = new DataTemplate(typeof(ImageCell));
        cell.SetBinding(ImageCell.ImageSourceProperty, "Afbeelding");
        cell.SetBinding(TextCell.TextProperty, "Naam");
        cell.SetBinding(TextCell.DetailProperty, "Omschrijving");
    
        var listview = new ListView
        {
            ItemsSource = Graanziekten,
            ItemTemplate = cell
        };
    

    你也可以把 Image 属性留给这个:

        public ImageSource BitmapThumbnail
        {
            get
            {
                 return Afbeeldingen.First().AfbeeldingSmall;
            }
        }
    

    并与它一起使用转换器:

        public class ByteArrayToImageConverter: IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                byte[] imageAsBytes = (byte[])value;
                return ImageSource.FromStream(() => new MemoryStream(imageAsBytes);
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    

    如果您要使用转换器,您需要将您的 SetBinding 更改为:

        cell.SetBinding(ImageCell.ImageSourceProperty, "Afbeelding", BindingMode.OneWay, new ByteArrayToImageConverter());
    

    编辑: 您的 SetBinding(TextCell) 应该是 SetBinding(ImageCell)。 你也可以尝试像这样构建数据模板吗?应该没什么区别,但我没有想法:

    var listview = new ListView
    {
        ItemsSource = Graanziekten,
        ItemTemplate = new DataTemplate(() =>
        {
            ImageCell imageCell = new ImageCell();
            imageCell.SetBinding(ImageCell.ImageSourceProperty, new Binding("Afbeelding", BindingMode.OneWay, new ByteArrayToImageConverter()));
            imageCell.SetBinding(ImageCell.TextProperty, "Naam");
            imageCell.SetBinding(ImageCell.DetailProperty, "Omschrijving");
            return imageCell;
        };
    };
    

    而不是

    var listview = new ListView
    {
        ItemsSource = Graanziekten,
        ItemTemplate = cell
    };
    

    【讨论】:

    • 我必须将最后一行更改为: cell.SetBinding(ImageCell.ImageSourceProperty, new Binding("Afbeelding", BindingMode.OneWay, new ByteArrayToImageConverter()));但是,它给我留下了空白屏幕(完全省略了图像确实呈现了实际的列表视图)
    • @PeterBrachwitz - 对DataTemplate 进行了编辑,希望对您有所帮助.. 仍在使用转换器。
    • 不幸的是,没有区别。但是,我尝试使用字节数组将单个图像添加到我的内容页面。它也没有用;导致异常。这是跟踪:消息“无效的跨线程访问”。在 mscorlib.ni.dll 中发生了“System.IO.FileNotFoundException”类型的异常,在托管/本机边界之前未进行处理 System.Windows.ni.dll 中出现“System.UnauthorizedAccessException”类型的异常,并且没有t 在托管/本机边界之前处理 也许表单的当前版本没有正确支持此功能
    • 您是否在单独的线程上执行代码?在后台线程中更新 UI 很可能会出现该错误。
    • 不,此时都在 UI 线程上。自应用程序启动后,字节 [] 也在内存中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    • 2016-09-15
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多