【问题标题】:ImageCell not displaying image loaded from a URIImageCell 不显示从 URI 加载的图像
【发布时间】:2019-03-25 06:13:45
【问题描述】:

我正在编写一个从 FLickr 提取 JSON 数据并显示图像单元列表的应用程序。该应用程序没有给出任何错误,并允许输入搜索并提取数据。标题数据显示没有问题,但图像保持空白。几年前,我在所有更新之前在大学学习了 Xamarin,但我无法解决这个问题。我似乎找不到有关如何解决此问题的信息。

<SearchBar x:Name="Searchbar" Placeholder="Search" TextChanged="
<ScrollView>
<ListView x:Name="listView">
  <ListView.ItemTemplate>
    <DataTemplate>
      <ImageCell Height="50" 
                 Text="{Binding title}"
                 ImageSource="{Binding url}"
                 Tapped="ImageCell_Tapped">
       </ImageCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>
</ScrollView>


using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using System.Collections.ObjectModel;
using Android.Graphics;

namespace InvestCloudChallenge
{

public partial class MainPage : ContentPage
{
    string searchText;
    HttpClient myClient = new HttpClient();
    string APIKey = "api-key here";
    string url = "https://api.flickr.com/services/rest/?            
    method=flickr.photos.search" +
                 "&api_key={0}" +
                 "&text={1}" +
                 "&format=json&nojsoncallback=1";
    string photoUrl = "http://farm{0}.staticflikr.com/{1}/{2}_{3}_n.jpg";
    IList<Photo> imageList = new ObservableCollection<Photo>();

    public MainPage()
    {   
        InitializeComponent();
    }

    private async void updateList()
    {
        imageList.Clear();
        var completeUrl = string.Format(url,APIKey,searchText);
        var results = await myClient.GetStringAsync(completeUrl);

        PhotoData apidata = JsonConvert.DeserializeObject<PhotoData>    
        (results);

        if (apidata.stat == "ok")
        {
           foreach (Photo data in apidata.photos.photo)
           {
                Photo myPhoto = new Photo()
                {
                    title = data.title,
                    url = new Uri(string.Format(photoUrl, data.farm, 
                    data.server, data.id, data.secret)),
                };

                imageList.Add(myPhoto);
            }
            listView.ItemsSource = imageList;
        }
    }

    private void OnTextChanged(object sender,TextChangedEventArgs 
    eventArgs)
    {
        if (Searchbar?.Text != "")
        {
            searchText = Searchbar.Text.ToString();

            updateList();
        }
    }

    public class Photo
    {
        public string id { get; set; }
        public string owner { get; set; }
        public string secret { get; set; }
        public string server { get; set; }
        public int farm { get; set; }
        public string title { get; set; }
        public int ispublic { get; set; }
        public int isfriend { get; set; }
        public int isfamily { get; set; }
        public Uri url { get; set; }
    }

    public class Photos
    {
        public int page { get; set; }
        public int pages { get; set; }
        public int perpage { get; set; }
        public string total { get; set; }
        public List<Photo> photo { get; set; }
    }

    public class PhotoData
    {
        public Photos photos { get; set; }
        public string stat { get; set; }
    }
}
}

【问题讨论】:

  • 您是否检查过应用程序输出以查看它是否显示任何相关消息?您是否验证了您正在构建的 url 是有效的并且可以在设备/模拟器浏览器中使用?
  • 最初我没有在输出中看到任何内容,但我进行了一些挖掘并反复发现此消息。 [0:] ImageLoaderSourceHandler: 无法检索图像或图像数据无效: Uri: farm8.staticflikr.com/7899/33582703938_1e830f040f_n.jpg 我检查了设备浏览器中的链接,它正确地提取了图像。
  • 该网址无法为我加载。是私人图片吗?您是否必须设置任何标头或身份验证才能检索它?
  • 它在我的计算机浏览器中不起作用,也只有当我在设备浏览器中尝试时才会加载。我不知道为什么。
  • staticfliCkr.com - 你遗漏了第二个“C”

标签: c# xamarin


【解决方案1】:

Flickr 的网址拼错了

string photoUrl = "http://farm{0}.staticflikr.com/{1}/{2}_{3}_n.jpg";

应该是

string photoUrl = "http://farm{0}.staticflickr.com/{1}/{2}_{3}_n.jpg";

【讨论】:

  • *facepalm.... 呃,导致问题的总是微小的错字。非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多