Xamarin 表单,试图将我的图像 URL 从我的数据库绑定到 XAML 但图像没有出现

Xamarin forms, trying to bind my Image URL from my database to XAML but an image does not appear

我尝试从我的数据库(在本例中为本地主机)获取图像,但它似乎不起作用。我将结果(URL 绑定到图像)到我的 XAML 没有任何运气。我没有收到任何错误,但也没有出现图像。这是我的代码。

可能和我的URL有关? 当我使用 parse(作为后端)时,他们给了我们一个 URL,它是这样开始的: “http://files.parsetfss.com/.....”

我的 URL 刚开始是 "localhost"。我需要以某种方式调整后端代码吗?

我在其中检索 http 和 jsondata 的 cs 文件:

static public async Task<JObject> getImages ()
{
    var httpClientRequest = new HttpClient ();
    var result = await httpClientRequest.GetAsync ("http://www.localhost.com/image.php");
    var resultString = await result.Content.ReadAsStringAsync ();
    var jsonResult = JObject.Parse (resultString);

    return jsonResult;
} 

在本地主机上看起来像这样:

{
    "results": [
    {
        "GetImage": {
            "ID": 1,
            "Name": "Aqua",
            "URL": "http://localhost.com/Images/Aquavit.jpg"
        }
    }]
}

这是我尝试接收图像的内容页面:

async void clickHere (object s, EventArgs ar)
{
    var createImage = await phpApi.getImages ();
    list = new List<pictures> ();

    foreach (var currentItem in createImage["results"]) {
        var prodPic = "";
        if(currentItem["GetImage"] != null)
        {
            prodPic = (string)currentItem ["GetImage"]["URL"];
        }

        list.Add (new pictures () { 
            image = prodPic,
        });

        System.Diagnostics.Debug.WriteLine (currentItem ["GetImage"]["URL"]); //here I get the exact URL.

    }
}

我的class:

public class pictures 
{
    public string image { get; set; }
}

及其 XAML:

<Image Source = "{Binding image}" />

我认为您应该使用 UriImageSource 而不是字符串。因为它是 url。试试这个:

你的class:

public class pictures 
{
    public UriImageSource image { get; set; }
}

内容页:

prodPic = (string)currentItem ["GetImage"]["URL"];

list.Add (new pictures () {
    image = new UriImageSource { Uri = new Uri(prodPic) },
});

此外,我认为您的列表需要 ObservableCollection 而不是 List

另外:

如果你想加载更多图片 and/or 允许在加载时显示占位符,我推荐 FFImageLoading 库。它提供了很好的功能,如下载、缓存、显示占位符和错误图像,最重要的是:将图像下采样到目标大小。这一直是 android.

的痛点

您可以将包含 url 的字符串直接绑定到 Source。代码如下:

<ffimageloading:CachedImage 
    Source="http://thecatapi.com/?id=MTQ5MzcyNA">
</ffimageloading:CachedImage>