需要帮助转换要在网格视图中显示的图片

本文关键字:显示 网格 帮助 转换 视图 | 更新日期: 2023-09-27 18:17:03

我有问题要解决,所以请,如果你能指导我如何做到这一点,我将不胜感激:

首先,我的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设置为list

如果我运行解决方案,我会得到gridview中没有任何内容的"box",并出现错误说

"错误:转换器转换类型的值失败"Windows.Storage。存储文件'类型'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;
        }
    }