【问题标题】:How to clear/avoid FFImageLoading's caching in list item image如何清除/避免列表项图像中的 FFImageLoading 缓存
【发布时间】:2020-02-21 15:15:04
【问题描述】:

我在我的项目中使用FFImageLoading 而不是Image 以获得更高级的功能,例如占位符。在我的项目中,有一个需要显示的图像列表。所以我也使用列表视图。

<ListView x:Name="membersList" ItemTapped="Handle_ItemTappedMember" HorizontalOptions="FillAndExpand" HasUnevenRows="true" SeparatorVisibility="None" BackgroundColor="#EEEEEE" Grid.Row="0" Grid.Column="0">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.View>
                            <StackLayout Orientation="Horizontal" Padding="5,5,5,5" HeightRequest="75" Margin="10,5,10,5" BackgroundColor="White" HorizontalOptions="FillAndExpand">
                                <ffimageloading:CachedImage Source="{Binding imageUrl}" x:Name="patImage" WidthRequest="60" HeightRequest="60" Aspect="AspectFill" VerticalOptions="Center">
                                    <ffimageloading:CachedImage.Transformations>
                                        <fftransformations:CircleTransformation />
                                    </ffimageloading:CachedImage.Transformations>
                                </ffimageloading:CachedImage>
                                <StackLayout Spacing="3" Orientation="Vertical" VerticalOptions="Center" HorizontalOptions="FillAndExpand">
                                    <Label FontAttributes="Bold" FontSize="14" TextColor="#212121" Text="{Binding FullName}" />
                                    <Label FontSize="12" TextColor="#212121" Text="{Binding Relation}" />
                                    <Label FontSize="12" TextColor="#212121" Text="{Binding Pat_ID}" />
                                </StackLayout>
                            </StackLayout>
                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

我的问题是我不想在这里使用缓存技术,因为我来自服务器的图像可能会在不更改 URL 的情况下更改。

我知道如何使用 FFImageLoading 清除单个图像视图的缓存

await CachedImage.InvalidateCache(Image.Source, CacheType.All, true);

但是如何在 ListView 中实现呢?我在这里使用 Binding 属性加载图像。

【问题讨论】:

  • 您不能将CacheDuration 属性设置为0 或-1 之类的吗?
  • 让我检查一下,已经用 0 试过了,但到目前为止没有运气
  • @GeraldVersluis 0 & -1 不起作用
  • 当我再次重新启动应用程序时,图像似乎更新了。
  • 难道你不能通过你的图像“foreach”并使缓存无效吗?在后台任务中弹出它,这样你就不会阻塞主 UI。

标签: listview caching xamarin.forms ffimageloading


【解决方案1】:

在同样的事情上花了一些时间之后,我已经解决了我的问题。最后它非常简单。 在我的列表视图的绑定模型类中,在设置imageUrl 时,我只是同时使用该 url 清除了缓存。

private string mImageUrl { get; set; }
public string imageUrl
{
get{
     return mImageUrl;
   };
set{
     mImageUrl = value;
     await CachedImage.InvalidateCache(mImageUrl, CacheType.All, true);
   };
}

这将清除以 imageUrl 作为键的缓存图像。 谢谢大家的支持。快乐编码:)

【讨论】:

    【解决方案2】:

    await 在没有异步的情况下无法工作,因此我添加了用于清理缓存的异步方法。

    private string mImageUrl { get; set; }
    public string imageUrl
    {
    get{
         return mImageUrl;
       };
    set{
         mImageUrl = value;
         cleanCache();
    
       };
    }
    
    private async void cleanCache()
    {
       await CachedImage.InvalidateCache(ImageURL, CacheType.All, true);
    }
    

    【讨论】:

    • 你没有提供任何你解决的问题你不能只给最终用户一个没有描述的功能,请做出改变来解释你的答案。
    猜你喜欢
    • 1970-01-01
    • 2017-08-25
    • 2011-02-22
    • 2013-11-21
    • 2011-04-12
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多