显示地址保存在数据库中的图像,例如/image/news/frist.jpg

时间:2019-02-09 20:45:20

标签: listview xamarin xamarin.forms

我将数据从数据库加载到MVC Api的列表视图中,要显示的数据之一是Image。 像这样保存在数据库中的图像“ \ images \ NewsImage \ Untitled-4.jpg” 现在,我们有一个网站,其中显示带有图片的新闻,图片地址仅保存在数据库中而不是图片。 这是我在列表视图中显示数据的代码 标题显示正确,但图像未显示

`HttpClient httpClient = new HttpClient(); var response =等待httpClient.GetStringAsync(“ http://somedomain.com/api/values/getlastten”);

    var TProducts = JsonConvert.DeserializeObject<List<News>>(response);
    Label header = new Label
    {
        Text = "ten",
        FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
        HorizontalOptions = LayoutOptions.Center
    };

    ListView listView = new ListView
    {
        ItemsSource = TProducts,
        ItemTemplate = new DataTemplate(() =>
        {
            // Create views with bindings for displaying each property.
            Label nameLabel = new Label();
            nameLabel.SetBinding(Label.TextProperty, "Title");

            Image nImage = new Image();
            nImage.SetBinding (Image.SourceProperty, @"www.mydomain.com/"+"Image");

            Label ViewdLabel = new Label();
            ViewdLabel.SetBinding(Label.TextProperty, "Viewed");
            nameLabel.SetBinding(Label.FontFamilyProperty, "BNazanin.ttf#Nazanin");

            // Return an assembled ViewCell.
            return new ViewCell
            {
                View = new StackLayout
                {
                    Padding = new Thickness(0, 5),
                    Orientation = StackOrientation.Horizontal,
                    Children =
                        {
                            nImage,
                            new StackLayout
                            {
                                VerticalOptions = LayoutOptions.Center,
                                Spacing = 0,
                                Children =
                                {
                                    nameLabel,
                                    ViewdLabel
                                }
                                }
                        }
                }
            };
        })
    };
    listView.ItemSelected += async (sender, e) =>
    {
        News = (News)e.SelectedItem;

      await  Navigation.PushAsync(new NewsDetails(News));
    };
        this.Content = new StackLayout
    {
        Children =
        {
            header,
            listView
        }
    };

}


private void ProductLV_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
    News =(News) e.SelectedItem;

    Navigation.PushAsync(new NewsDetails(News));
}`

更新我的模型是

 public class News
{
    public int Id { get; set; }
    public string SubTitle { get; set; }
    public string Title { get; set; }
    public string Summery { get; set; }
    public string FullText { get; set; }
    public string Image { get; set; }

}

1 个答案:

答案 0 :(得分:1)

您不能创建这样的绑定

nImage.SetBinding (Image.SourceProperty, @"www.mydomain.com/"+"Image");

相反,请在模型中创建一个ImageURL属性,该属性将返回图像的正确格式的url并绑定到该图像。

nImage.SetBinding (Image.SourceProperty, "ImageURL");

像这样

public class News
{
    ...

    public string Image { get; set; }

    public string ImageURL { 
      get {
        return "http://www.example.com/" + Image;
      }
    }
}