如何清除/避免列表项图像中的缓存 - Xamain.Forms

时间:2017-06-15 08:14:28

标签: listview caching xamarin.forms ffimageloading

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

<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属性来加载图像。

2 个答案:

答案 0 :(得分:3)

经过一段时间的花费,我已经解决了我的问题。最后它非常简单。 在我的listview的绑定模型类中,设置imageUrl时,我刚刚使用该URL清除了缓存。

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

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

答案 1 :(得分:1)

等待在没有异步的情况下无法正常工作,因此我添加了异步方法以进行干净的缓存。

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);
}