我有问题需要解决,所以,如果你可以指导我如何做到这一点,我将不胜感激:
首先,我的gridview: 当它加载时,我得到像[] [] []这样的项目,我需要它们像这样[] [] []。我已将边距设置为15,但它会使项目更大,而不是在它们之间放置我需要的空间:
<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>
其次,我的代码可以从网络上抓取图像,在手机中创建一个文件夹,将图片保存在那里。然后它访问该文件夹,抓取该文件夹中的所有图像,将它们放入列表中......我已将GridView的DataContext作为该列表
如果我运行解决方案,我会在gridview中找到“框”,但没有任何内容,并且出现错误
“错误:转换器无法转换类型的值 'Windows.Storage.StorageFile'键入'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);
}
答案 0 :(得分:0)
我做完了......
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;
}
}