显示保存在数据库中的地址的图像,例如 /image/news/frist.jpg
Posted
技术标签:
【中文标题】显示保存在数据库中的地址的图像,例如 /image/news/frist.jpg【英文标题】:Show Image that Address saved in database like /image/news/frist.jpg 【发布时间】:2019-07-03 18:25:25 【问题描述】:我通过 MVC Api 将数据从数据库加载到列表视图,要显示的数据之一是 Image。 像这样“\images\NewsImage\Untitled-4.jpg”这样保存在数据库中的图像 现在我们有一个网站,用图片显示新闻,图片地址只保存在数据库中,而不是图片。 这是我在列表视图中显示数据的代码 标题显示正确,但图片未显示
`HttpClient httpClient = new HttpClient(); var response = await 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】:你不能创建这样的绑定
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;
【讨论】:
谢谢 我如何定义和填充 ImageUrl?也更新我的问题。 在数据库中保存的图像地址与 \News\Image\Untitle.jpg 相同,我和上面的帖子一样,图像不显示,但是当在图像的数据库地址中将 \ 更改为 / 时,它可以工作,如何我可以改变这个吗? docs.microsoft.com/en-us/dotnet/api/… 谢谢我喜欢这个公共字符串 Image get ;放; public string ImageURL get return @"http:/mydomain.com"+Image.Replace(@"\","/");以上是关于显示保存在数据库中的地址的图像,例如 /image/news/frist.jpg的主要内容,如果未能解决你的问题,请参考以下文章
如何使用其他字段进行文件上传,例如:使用 Vue.js 在 Node.js 中的标题、名称、图像、image2