需要帮助转换要在 gridview 中显示的图片
Need help converting the pictures to be shown in the gridview
我有问题需要解决,如果你能指导我如何解决这个问题,我将不胜感激:
首先,我的网格视图:
当它加载时,我得到像这样 [][][] 的项目,我需要它们像这样 [] [] []。我已将边距设置为 15,但它会使项目变大,而不是在它们之间放置我需要的 space:
<Grid Background="White">
<StackPanel Margin="0,200,0,0">
<GridView x:Name="AlGridView"
Foreground="#FF131212"
ItemsSource="{Binding}"
SelectionMode="Multiple">
<GridView.ItemTemplate>
<DataTemplate>
<Image Width="90"
Height="90"
Margin="15"
Source="{Binding}"
Stretch="UniformToFill" />
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</StackPanel>
<Button Content="CONTINUE"
HorizontalAlignment="Right"
Margin="0,0,10,0"
VerticalAlignment="Top"
Foreground="#FFF70F63"
BorderThickness="0"
FontSize="18.14"
Width="Auto" />
</Grid>
其次,我有自己的代码,可以从网上抓取图片,在 phone 中创建一个文件夹,将图片保存在那里。然后它访问该文件夹,从该文件夹中获取所有图像,将它们放入列表中......我已经将 GridView 的 DataContext 设为该列表
如果我 运行 解决方案,我会在 gridview 中得到 "boxes",上面什么也没有,并且出现错误
"Error: Converter failed to convert value of type
'Windows.Storage.StorageFile' to type 'ImageSource';"
所以我想我需要转换它们,但我不知道如何转换,也不知道我应该去哪里看看我应该做什么。
代码如下:
public void QueryPicturesToShow()
{
var pics = from medio in GlobalVariables.Medios
where medio.img != null
select new
{
Name = medio.name,
Id = medio.id,
Picture = medio.img
};
foreach(var item in pics)
{
savePicToDisk(item.Picture, item.Name, item.Id);
}
}
private async void savePicToDisk(string picAddress, string picName, string picId)
{
StorageFolder folder = await KnownFolders.PicturesLibrary.CreateFolderAsync("carpetaFunciona", CreationCollisionOption.OpenIfExists);
StorageFile file = await folder.CreateFileAsync((picName + picId + ".png"), CreationCollisionOption.ReplaceExisting);
string url = GlobalVariables.apiUrl + picAddress;
Debug.WriteLine(url);
HttpClient client = new HttpClient();
byte[] responseBytes = await client.GetByteArrayAsync(url);
var stream = await file.OpenAsync(FileAccessMode.ReadWrite);
using(var outputStream = stream.GetOutputStreamAt(0))
{
DataWriter writer = new DataWriter(outputStream);
writer.WriteBytes(responseBytes);
await writer.StoreAsync();
await outputStream.FlushAsync();
}
showPicturesInGrid();
}
public async void showPicturesInGrid()
{
var f1 = await KnownFolders.PicturesLibrary.GetFolderAsync("carpetaFunciona");
Debug.WriteLine(f1);
StorageFolder folder1 = f1;
List<StorageFile> listOfFiles = new List<StorageFile>();
await RetriveFilesInFolders(listOfFiles, folder1);
Debug.WriteLine(listOfFiles.Count);
foreach(var item in listOfFiles)
{
// here I should make the converter, I guess
}
AlGridView.DataContext = listOfFiles;
}
private async Task RetriveFilesInFolders(List<StorageFile> list, StorageFolder parent)
{
foreach(var item in await parent.GetFilesAsync())
list.Add(item);
foreach(var item in await parent.GetFoldersAsync())
await RetriveFilesInFolders(list, item);
}
我搞定了...
public async void showPicturesInGrid()
{
//Select folder
StorageFolder folder1 = await KnownFolders.PicturesLibrary.GetFolderAsync("carpetaFunciona");
//get all the pics in that folder
List<IStorageItem> file = (await folder1.GetItemsAsync()).ToList();
Debug.WriteLine(file.Count);
if (file.Count > 0)
{
// if there are some pics, make a list to put them and a bitmap
// for each pic found, with its properties
var images = new List<WriteableBitmap>();
foreach (StorageFile f in file)
{
var name = f.Name;
Debug.WriteLine(f.Name);
ImageProperties properties = await f.Properties.GetImagePropertiesAsync();
if (properties.Width > 0)
{
var bitmap = new WriteableBitmap((int)properties.Width, (int)properties.Height);
Debug.WriteLine(bitmap.PixelWidth);
using (var stream = await f.OpenAsync(FileAccessMode.ReadWrite))
{
bitmap.SetSource(stream);
}
// add the bitmaps to the list
images.Add(bitmap);
Debug.WriteLine(images.Count);
}
}
// Bind the list to the grid view
AlGridView.DataContext = images;
}
}
我有问题需要解决,如果你能指导我如何解决这个问题,我将不胜感激:
首先,我的网格视图: 当它加载时,我得到像这样 [][][] 的项目,我需要它们像这样 [] [] []。我已将边距设置为 15,但它会使项目变大,而不是在它们之间放置我需要的 space:
<Grid Background="White">
<StackPanel Margin="0,200,0,0">
<GridView x:Name="AlGridView"
Foreground="#FF131212"
ItemsSource="{Binding}"
SelectionMode="Multiple">
<GridView.ItemTemplate>
<DataTemplate>
<Image Width="90"
Height="90"
Margin="15"
Source="{Binding}"
Stretch="UniformToFill" />
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</StackPanel>
<Button Content="CONTINUE"
HorizontalAlignment="Right"
Margin="0,0,10,0"
VerticalAlignment="Top"
Foreground="#FFF70F63"
BorderThickness="0"
FontSize="18.14"
Width="Auto" />
</Grid>
其次,我有自己的代码,可以从网上抓取图片,在 phone 中创建一个文件夹,将图片保存在那里。然后它访问该文件夹,从该文件夹中获取所有图像,将它们放入列表中......我已经将 GridView 的 DataContext 设为该列表
如果我 运行 解决方案,我会在 gridview 中得到 "boxes",上面什么也没有,并且出现错误
"Error: Converter failed to convert value of type 'Windows.Storage.StorageFile' to type 'ImageSource';"
所以我想我需要转换它们,但我不知道如何转换,也不知道我应该去哪里看看我应该做什么。
代码如下:
public void QueryPicturesToShow()
{
var pics = from medio in GlobalVariables.Medios
where medio.img != null
select new
{
Name = medio.name,
Id = medio.id,
Picture = medio.img
};
foreach(var item in pics)
{
savePicToDisk(item.Picture, item.Name, item.Id);
}
}
private async void savePicToDisk(string picAddress, string picName, string picId)
{
StorageFolder folder = await KnownFolders.PicturesLibrary.CreateFolderAsync("carpetaFunciona", CreationCollisionOption.OpenIfExists);
StorageFile file = await folder.CreateFileAsync((picName + picId + ".png"), CreationCollisionOption.ReplaceExisting);
string url = GlobalVariables.apiUrl + picAddress;
Debug.WriteLine(url);
HttpClient client = new HttpClient();
byte[] responseBytes = await client.GetByteArrayAsync(url);
var stream = await file.OpenAsync(FileAccessMode.ReadWrite);
using(var outputStream = stream.GetOutputStreamAt(0))
{
DataWriter writer = new DataWriter(outputStream);
writer.WriteBytes(responseBytes);
await writer.StoreAsync();
await outputStream.FlushAsync();
}
showPicturesInGrid();
}
public async void showPicturesInGrid()
{
var f1 = await KnownFolders.PicturesLibrary.GetFolderAsync("carpetaFunciona");
Debug.WriteLine(f1);
StorageFolder folder1 = f1;
List<StorageFile> listOfFiles = new List<StorageFile>();
await RetriveFilesInFolders(listOfFiles, folder1);
Debug.WriteLine(listOfFiles.Count);
foreach(var item in listOfFiles)
{
// here I should make the converter, I guess
}
AlGridView.DataContext = listOfFiles;
}
private async Task RetriveFilesInFolders(List<StorageFile> list, StorageFolder parent)
{
foreach(var item in await parent.GetFilesAsync())
list.Add(item);
foreach(var item in await parent.GetFoldersAsync())
await RetriveFilesInFolders(list, item);
}
我搞定了...
public async void showPicturesInGrid()
{
//Select folder
StorageFolder folder1 = await KnownFolders.PicturesLibrary.GetFolderAsync("carpetaFunciona");
//get all the pics in that folder
List<IStorageItem> file = (await folder1.GetItemsAsync()).ToList();
Debug.WriteLine(file.Count);
if (file.Count > 0)
{
// if there are some pics, make a list to put them and a bitmap
// for each pic found, with its properties
var images = new List<WriteableBitmap>();
foreach (StorageFile f in file)
{
var name = f.Name;
Debug.WriteLine(f.Name);
ImageProperties properties = await f.Properties.GetImagePropertiesAsync();
if (properties.Width > 0)
{
var bitmap = new WriteableBitmap((int)properties.Width, (int)properties.Height);
Debug.WriteLine(bitmap.PixelWidth);
using (var stream = await f.OpenAsync(FileAccessMode.ReadWrite))
{
bitmap.SetSource(stream);
}
// add the bitmaps to the list
images.Add(bitmap);
Debug.WriteLine(images.Count);
}
}
// Bind the list to the grid view
AlGridView.DataContext = images;
}
}