使用Akavache在列表视图适配器中显示图像

时间:2014-12-11 14:35:01

标签: c# android listview xamarin akavache

我正在使用Xamarin开发跨平台应用程序。我找到了Akavache,但我无法在listview适配器中使用Akavache处理图像加载。它工作得很慢。所以我需要修复它或找到另一种方法来处理这个问题。我也找不到好的例子或样本。

我正在尝试使用getView方法在Listview适配器中下载和加载图像,如下所示:

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        var view = convertView;
        ViewHolder holder = null;
        if (view != null) // otherwise create a new one
            holder = view.Tag as ViewHolder;

        if (holder == null)
        {
            holder = new ViewHolder();
            view = _activity.LayoutInflater.Inflate(Resource.Layout.Book_Item, null);
            holder.TextItem1 = view.FindViewById<TextView>(Resource.Id.TextItem1);
            holder.TextItem2 = view.FindViewById<TextView>(Resource.Id.TextItem2);
            holder.ImageView = view.FindViewById<ImageView>(Resource.Id.BookImage);
            getImage(position, holder.ImageView, image => holder.ImageView.SetImageDrawable(image.ToNative())); 
        }

        holder.TextItem1.Text = _list[position].volumeInfo.title;
        holder.TextItem2.Text = _list[position].volumeInfo.publishedDate;
        return view;
    }


    public async void getImage(int position, ImageView imageView, Action <IBitmap> setImage)
    {
        var image = await _cache.GetAnImage(_list[position].volumeInfo.imageLinks.smallThumbnail);
        setImage(image);
    }

在我的DataCache类中,我尝试以下列方式获取图像,在滚动时loadImageFromUrl无法按预期工作,重新加载图像需要很长时间。 GetAndFetchLatest和GetOrFetchObject方法提供InvalidCastException。异常消息结束。你有任何同样的例子或任何建议来解决我的问题吗?

    public async Task<IBitmap> GetAnImage(string imageUrl)
    {
        // return await BlobCache.InMemory.GetAndFetchLatest( imageUrl, async() => await DownloadImage(imageUrl), offset => true);
        // return await BlobCache.InMemory.GetOrFetchObject(imageUrl, async () => await DownloadImage(imageUrl));
        return await DownloadImage(imageUrl);
    }

    public async Task<IBitmap> DownloadImage(string imageUrl)
    {
        return await BlobCache.InMemory.LoadImageFromUrl(imageUrl, true, 100, 100);
    }
  

UNHANDLED EXCEPTION:   12-11 12:48:54.560 I / MonoDroid(2017):System.InvalidCastException:无法从源类型转换为目标类型。   12-11 12:48:54.560 I / MonoDroid(2017):at(wrapper castclass)对象:__ castclass_with_cache(object,intptr,intptr)   12-11 12:48:54.564 I / MonoDroid(2017):at Akavache.JsonSerializationMixin + c__AnonStorey1 1[System.Byte[]].<>m__0 (System.Exception _) [0x00000] in <filename unknown>:0 12-11 12:48:54.564 I/MonoDroid( 2017): at System.Reactive.Linq.ObservableImpl.Catch 2 + _ [System.Byte [],System.Exception] .OnError(System.Exception error)[0x00000在:0   12-11 12:48:54.572 I / MonoDroid(2017):---从抛出异常的先前位置开始的堆栈跟踪结束---   12-11 12:48:54.572 I / MonoDroid(2017):在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x00000] in:0   12-11 12:48:54.580 I / MonoDroid(2017):在System.Reactive.PlatformServices.ExceptionServicesImpl.Rethrow(System.Exception异常)[0x00000] in:0   12-11 12:48:54.580 I / MonoDroid(2017):在System.Reactive.ExceptionHelpers.ThrowIfNotNull(System.Exception异常)[0x00000] in:0   12-11 12:48:54.580 I / MonoDroid(2017):在System.Reactive.Subjects.AsyncSubject`1 [Splat.IBitmap] .GetResult()[0x00000] in:0   发生了未处理的异常。

更新: Paul现在回答我,我知道我不能将GetOrFetchObject与IBitmaps一起使用,因为它们是UI元素。所以异常消息现在是不必要的。 但是loadImageFromUrl方法在滚动时阻止我的UI线程。所以对我来说这仍然是个大问题。

1 个答案:

答案 0 :(得分:1)

最后我解决了这个问题。首先,我通过尝试不同的库(MonoDroid.UrlImageViewHelper)发现它不是Akavache问题 然后我认为这个问题可能与listview本身有关。我是一名高级开发人员,所以我知道listview优化,但我仍在寻找新的优化策略,看看我是否错过了什么。

首先,我建议你看一下这个网站: http://leftshift.io/6-ways-to-make-your-lists-scroll-faster-than-the-wind

我已经了解了很多这些建议,但真正不同的是改变listview的高度以匹配来自换行内容的父级。您也可以尝试将其设为0dp。它会产生相同的结果。

然后我发现这个帖子讲述了#34; match_parent&#34;背后的故事。优化

Why is 0dp considered a performance enhancement?

相关问题