首页 > 解决方案 > Xamarin Forms 5 - 下载图像以供将来使用

问题描述

对于我的应用程序,我有一个对象列表视图,其中一些对象持有一个指向我们服务器上图像的 url。无论如何,此图像必须 - 在任何情况下 - 可用。

当用户单击其中一个 ListView-Items 时,他导航到包含此对象信息的 ContentPage。包括一个包含该图像的 ImageView。

该页面的 XAML 如下所示:

                  IsVisible="False">
                <Grid.GestureRecognizers>
                    <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" NumberOfTapsRequired="1" />
                </Grid.GestureRecognizers>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                <forms:CachedImage x:Name="gridImageControl" 
                                   Grid.Row="0" Margin="10" 
                                   CacheType="All"
                                   BindingContext="{Binding theObject}" 
                                   VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand"
                                   Source="{Binding TheUrl}"
                                   Aspect="AspectFill"
                                   DownsampleToViewSize="False"
                                   LoadingPlaceholder="{fa:ImageResource thenamespace.Images.langlogo.png}"
                                   ErrorPlaceholder="{fa:ImageResource thenamespace.Images.errorplaceholder.png}"
                                   RetryCount= "3" RetryDelay= "600" />

                <StackLayout>
                    <Image HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" Aspect="AspectFill" >
                        <Image.Source>
                            <FontImageSource FontFamily="faSolid" Glyph="{x:Static fa:FontAwesomeIcons.SearchPlus}"
                            Color="Black" Size="50">
                            </FontImageSource>
                        </Image.Source>
                    </Image>
                    
                    <Label x:Name="ClickOnMeLabel" Style="{DynamicResource label_base_text_style}"  FontAttributes="Bold" 
                       Text="Klicke zum Betrachten" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" 
                       HorizontalTextAlignment="Center" VerticalTextAlignment="Center" FontSize="Large" TextColor="Black">
                    </Label>
                </StackLayout>
                
            </Grid>

无法将图像包含在应用程序包中,因为图像由我们平台的管理员用户上传并下载到与该管理员用户关联的所有用户设备。

我尝试使用 FFImageLoading 的 CachedImage 和ImageService.Instance.LoadURL("THEURL").WithCache(CacheType.All).DownloadOnlyAsync()

ImageService.Instance.LoadURL("THEURL").WithCache(CacheType.All).PreloadAsync()

两者都会导致应用程序出现奇怪的行为,尤其是在 iOS 上。

图像似乎是随机加载或未加载的。有时它们在打开页面后出现,有时它们在页面出现时已经存在。有时,它们不会出现在 ImageView 中,并且即使在 DownloadOnly 和 Preload 周围有一个 try/catch 子句,也会使应用程序崩溃而不会显示任何错误。

尽管在页面甚至可以打开之前调用了 DownloadOnly 和 PreloadOnly - 即在应用程序的启动过程中。使用这两种方法中的任何一种时都会出现此行为。

我想知道我是否错过了有关如何使用这两种方法的一些信息,但是我在其他地方发现的关于 FFImageLoading 的内容并没有帮助我。行为不断出现。

也许有更好的方法?

谢谢你们 :)

标签: xamarinmobileimageviewffimageloading

解决方案


推荐阅读