C# 向GridView添加图像
我试图将图像添加到GridView中,但这些图像从未真正显示出来。 相反,我只是在缩略图布局中显示类名 这是我一直在调整的代码C# 向GridView添加图像,c#,windows-8,microsoft-metro,windows-store,C#,Windows 8,Microsoft Metro,Windows Store,我试图将图像添加到GridView中,但这些图像从未真正显示出来。 相反,我只是在缩略图布局中显示类名 这是我一直在调整的代码 public async void setImage(string url) { Byte[] imageBytes = await httpClient.GetByteArrayAsync(url); MemoryStream stream = new MemoryStream(imageBytes); var randomAccessSt
public async void setImage(string url) {
Byte[] imageBytes = await httpClient.GetByteArrayAsync(url);
MemoryStream stream = new MemoryStream(imageBytes);
var randomAccessStream = new MemoryRandomAccessStream(stream);
BitmapImage image = new BitmapImage();
await image.SetSourceAsync(randomAccessStream);
imagesGrid.Items.Add(image);
}
谢谢你的帮助。我最终得到了下面这段代码
public void setImage(string url) {
Image img = new Image();
img.Source = new BitmapImage(new Uri(url));
imagesGrid.Items.Add(img);
}
这是因为您正在将
BitmapImage
s添加到可视化树中<代码>位图图像不是可视元素。您需要做的是创建一个,并将属性设置为图像文件的URI。xaml中有许多这样的例子,我的博客上也有代码
我还没有测试过,但应该是这样的:
Image img = new Image();
img.Source = new BitmapImage(new Uri(url));
imagesGrid.Items.Add(img);