首页 > 解决方案 > Xamarin FFImageLoading.CachedImage 第一次不显示

问题描述

我在 xamarin 上遇到了 FFImageLoading 的一些故障,发生的事情是图像似乎只在页面第二次重新加载时才显示(例如,我离开页面而不是按后退按钮)我已经尝试加载这些图像首先在加载 ContentPage 之前创建,但它没有工作。我尝试使用 ForceLayout 来显示它,但效果不佳。

这是我在视图中的关注代码

<ListView.ItemTemplate>
<DataTemplate>
    <ViewCell Tapped="OnDetailsButtonClicked">
        <Frame
            Margin="2,2,2,2"
            Padding="5"
            BackgroundColor="White"
            IsClippedToBounds="True">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="20*" />
                    <ColumnDefinition Width="60*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>

                <ffimageloading:CachedImage
                    x:Name="profilePhoto"
                    Grid.Row="0"
                    Grid.Column="0"
                    Aspect="AspectFit"
                    HeightRequest="50"
                    HorizontalOptions="Center"
                    Source="{Binding CoworkerProfilePhoto}"
                    VerticalOptions="Center"
                    WidthRequest="50">
                    <ffimageloading:CachedImage.Transformations>
                        <fftransformations:CircleTransformation />
                    </ffimageloading:CachedImage.Transformations>
                </ffimageloading:CachedImage>

                ....

如您所见,我的投标清单上的 CoworkerProfilePhoto 变量正在投标该文件。

这些图像被存储在设备上,然后显示在屏幕上,这是一个简单的文件保存

public async static Task SaveImagem(this byte[] imagem, String nomeArquivo, IFolder pastaRaiz = null)

{ IFolder 意大利面 = PastaRaiz ?? FileSystem.Current.LocalStorage;

IFile arquivo = await pasta.CreateFileAsync(nomeArquivo, CreationCollisionOption.ReplaceExisting);

using (System.IO.Stream stream = await arquivo.OpenAsync(FileAccess.ReadAndWrite))
{
    stream.Write(imagem, 0, imagem.Length);
}

}

任何想法 ?我检查了这两个链接

https://github.com/luberda-molinet/FFImageLoading/issues/238

https://xamarin.github.io/bugzilla-archives/41/41087/bug.html

这似乎是相关的,但它们都没有起作用。

标签: c#xamarinxamarin.formsxamarin.android

解决方案


我找到了一个适合我的解决方案第一个问题是我只在 CoworkerProfilePhoto 属性中使用文件名

如下图

item.CoworkerProfilePhoto =  photo.Name;

但是系统不可能找到文件的存储位置,所以我必须传递绝对的 Uri 文件路径。

如下图

var folder = PCLStorage.FileSystem.Current.LocalStorage.GetFolderAsync(myPath).Result;
item.CoworkerProfilePhoto = new Uri(folder.Path + "/" + photo.Name, UriKind.Absolute);

推荐阅读