【问题标题】:If the file size is 0kb, it is not displayed on the gridview and the file is deleted如果文件大小为0kb,gridview上不显示,文件被删除
【发布时间】:2018-07-05 02:39:52
【问题描述】:

我有一个显示一些文件的网格视图。我想如果文件大小为0 kb,则该文件不显示并删除,而其他文件仍然显示 代码:

StorageFolder cover = await komik.GetFolderAsync("cover");
foreach (StorageFile file in sortedfiles)
{
    bool bukuada = true;
    Buku buku = new Buku();
    buku.Judul = file.DisplayName.ToString();
    BasicProperties pro = await file.GetBasicPropertiesAsync();
    if (pro.Size != 0)
    {
        StorageFile thumbFile = file;
        try
        {
            thumbFile = await cover.GetFileAsync(file.DisplayName.ToString() + ".jpg");
            BitmapImage bi = new BitmapImage();
            bi.SetSource(await thumbFile.OpenAsync(FileAccessMode.Read));
            buku.Cover = bi;

            datasource.Add(buku);
            loading.IsActive = false;
            this.itemGridView.ItemsSource = datasource;
        }
        catch
        {

        }
    }
}

如果我使用上面的代码,那么如果有一个大小为 0 kb 的文件,则不会显示所有文件。如何仅获取未显示和删除的 0 kb 文件,而其他文件仍在显示?

【问题讨论】:

  • 目前您的代码有什么问题?你不只需要添加一个else 然后在里面删除文件吗?
  • 问题是如果有1个或一些0 kb的文件,那么所有文件都无法显示。我只想要不显示的 0 kb 文件并且文件被删除。
  • 你应该把this.itemGridView.ItemsSource = datasource;放在foreach循环之后。
  • Define datasource as ObservableCollection: ObservableCollection<Buku> datasource = new ObservableCollection<Buku>(); 并将 this.itemGridView.ItemsSource = datasource; 行移出 foreach 循环(例如在它之前)。然后您的 GridView 应该会自动更新,而无需重置 ItemsSource

标签: c# image gridview uwp


【解决方案1】:

您的代码在循环中重复设置ItemsSource,效率较低,但不应该影响结果 - 即使有一些大小为 0 的文件,它们也应该被跳过,“实际”文件应该导致 datasource 被更新。

我担心以下问题:

thumbFile = await cover.GetFileAsync(file.DisplayName.ToString() + ".jpg");

已知DisplayName 属性有时会返回文件名包括 扩展名。我建议宁愿使用以下内容:

thumbFile = await cover.GetFileAsync( Path.GetFileNameWithoutExtension( file.Name ) + ".jpg" );

Name属性返回文件的全名,所以如果我们应用Path.GetFileNameWithoutExtension,扩展名肯定会被删除,所以结果就是你想要的。

如果这没有帮助,请将catch 表达式更改为catch ( Exception ex ),然后在catch 块内放置一个断点,因为如果datasource 变量没有填写数据,则必须有一个提前发生异常。

【讨论】:

    【解决方案2】:

    因为你已经设置了this.itemGridView.ItemsSource = datasource;pro.Size != 0

    尝试使用此代码。

    StorageFolder cover = await komik.GetFolderAsync("cover");
    List<StorageFile> deletedFileList = new List<StorageFile>();
    foreach (StorageFile file in sortedfiles)
    {
        bool bukuada = true;
        Buku buku = new Buku();
        buku.Judul = file.DisplayName.ToString();
        BasicProperties pro = await file.GetBasicPropertiesAsync();
        if (pro.Size != 0)
        {
            StorageFile thumbFile = file;
            try
            {
                thumbFile = await cover.GetFileAsync(file.DisplayName.ToString() + ".jpg");
                BitmapImage bi = new BitmapImage();
                bi.SetSource(await thumbFile.OpenAsync(FileAccessMode.Read));
                buku.Cover = bi;
    
                datasource.Add(buku);
                loading.IsActive = false;           
            }
            catch
            {
    
            }
        }
        else
        {
            deletedFileList.Add(file);
        }
    }
    
    // display the data source.
     this.itemGridView.ItemsSource = datasource;
    
     // delete the file
        foreach(var temp in deletedFileList)
        {
            try
            {
                await temp.DeleteAsync();
            }
            catch(IOException)
            {
    
            }
        }
    

    【讨论】:

    • 我收到一条错误消息:“List”不包含“DeleteAsync”的定义,并且没有接受“List”类型的第一个参数的扩展方法“DeleteAsync”可能是找到(您是否缺少 using 指令或程序集引用?)
    • @Rose 我修好了。
    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多