Xamarin.Forms 图像未从源显示

Posted

技术标签:

【中文标题】Xamarin.Forms 图像未从源显示【英文标题】:Xamarin.Forms Image not showing from Source 【发布时间】:2021-02-22 12:15:53 【问题描述】:

我正在使用假 Api 来测试应用程序。Api 在此链接上:http://jsonplaceholder.typicode.com/。我连接我的应用程序,获取所有照片并添加到 CollectionView。一切正常,但没有显示图像。这是我的代码:

    <Grid>
    <StackLayout>
        <Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
            <Label Text="Welcome to Xamarin.Forms!" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>
        </Frame>
            <RefreshView IsRefreshing="Binding IsRefreshing, Mode=OneWay"
             Command="Binding LoadRefreshCommand">
                <CollectionView ItemsSource="Binding Photos" SelectedItem="Binding SelectedPhoto" >
                    <CollectionView.ItemTemplate>
                        <DataTemplate>

                            <Grid Padding="0,0,5,0" >
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="3*"/>

                                </Grid.ColumnDefinitions>
                                <Grid.GestureRecognizers>
                                    <TapGestureRecognizer  Command="Binding Source=x:Reference CurrentPage, Path=BindingContext.LoadPhotosCommand" />
                                </Grid.GestureRecognizers>
                                <Image  Source="Binding Url" Grid.Column="0"></Image>
                                <Frame HasShadow="False" VerticalOptions="Center"  Grid.Column="1">
                                    <Label Text="Binding Title"></Label>
                                </Frame>
                            </Grid>
                        </DataTemplate>
                    </CollectionView.ItemTemplate>
                </CollectionView>
            </RefreshView>
        </StackLayout>
        <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="Center">
            <Frame IsVisible="Binding IsBusy" BorderColor="#07987f" HasShadow="False" BackgroundColor="#eeeeee">
                <StackLayout>
                    <ActivityIndicator IsVisible="Binding IsBusy" IsRunning="Binding IsBusy" HorizontalOptions="Center" VerticalOptions="Center"></ActivityIndicator>
                    <Label TextColor="#07987f" Text="Loading Data, Please Wait..." HorizontalTextAlignment="Center" VerticalTextAlignment="Center" HorizontalOptions="Center" VerticalOptions="Center" IsVisible="Binding IsBusy"/>
                </StackLayout>
            </Frame>
        </StackLayout>
    </Grid>

这是我从调试应用程序获得的图像:

可能是什么问题或所有图像都是空白的。 我也在控制台中得到了这个:

[0:] HTTP Request: Could not retrieve https://via.placeholder.com/600/f66b97, status code Gone
[0:] ImageLoaderSourceHandler: Could not retrieve image or image data was invalid: Uri: https://via.placeholder.com/600/f66b97

有人遇到过同样的问题吗?

【问题讨论】:

"via.placeholder.com/600/f66b97" 返回 304 状态码 @Jason 304 未修改是什么意思? @Jason 有什么建议可以解决这个问题吗? 使用不同的服务。听起来返回的图像 url 行为不正常 @Jason 图片网址看起来不错 【参考方案1】:

我不知道为什么 source 会导致阅读问题。我就是这样解决的:

    只需更改以下选项,它也应该适用于 https。 android 选项 => 高级 HttpClient 实施 = 设置为“托管”选项值 SSL/TLS 实施 = 设置为“Native TLS 1.2+”选项值

【讨论】:

以上是关于Xamarin.Forms 图像未从源显示的主要内容,如果未能解决你的问题,请参考以下文章

Xamarin Forms 可折叠 StackLayout

ReactiveUI 中的 Xamarin.Forms 控件是不是需要自定义绑定?

图像不显示 Xamarin.Forms

使用 Xamarin.Forms 从 URL 显示图像

Xamarin Forms 使用 XAML 显示来自嵌入式资源的图像

图像未显示在 Xamarin Forms ImageCircle 插件中