以编程方式将单元格添加到网格的代码会导致闪烁

发布于 2024-12-21 04:21:43 字数 1263 浏览 4 评论 0原文

我正在为 Windows 8 Metro 使用 C#/XAML 编写一个新项目。我正在尝试将单元格动态添加到网格中。我想要 3 列,而不仅仅是一列,我认为 Grid 可以解决这个问题。我正在使用的代码如下。问题是添加到网格中的每个新图像首先出现在一个位置,然后移动到另一个位置——或者其他地方;有很多闪烁。我的内容是动态的(在运行时确定),因此我无法在 XAML 中编写结构代码。我在 XAML 中唯一拥有的是包含三列和一行定义的网格。

StorageFolder picturesFolder = KnownFolders.PicturesLibrary;
StorageFolderQueryResult queryResult = picturesFolder.CreateFolderQuery();
IReadOnlyList<IStorageFolder> folderList = await queryResult.GetFoldersAsync();

int row = 0, col = 0;
foreach (IStorageFolder folder in folderList)
{
    IReadOnlyList<IStorageFile> fileList = await folder.GetFilesAsync();
    foreach (IStorageFile file in fileList)
    {
        Image image = new Image();
        image.Source = await LoadImageFromFileAsync(file);
        image.Width = 150;
        image.Height = 150;
        Grid.SetColumn(image, col);
        Grid.SetRow(image, row);
        ++col;
        if (col == 3)
        { 
            col = 0; 
            ++row;
            RowDefinition rd = new RowDefinition();
            GridLength gl = new GridLength() { Value = 150 };
            rd.Height = gl;
            grid.RowDefinitions.Add(rd); 
        }
        grid.Children.Add(image);
    }
}

那么:如何消除闪烁呢?有没有办法在 XAML 中设置项目模板来创建三列项目?

I'm coding up a new project in C#/XAML for Windows 8 Metro. I'm trying to dynamically add cells to a grid. I want 3 columns, not just one, and I thought Grid would do the trick. The code I'm using is below. The problem is that each new Image added to the Grid first appears in one place and then moves to another -- or something; there's a lot of flickering. My content is dynamic (determined at runtime) so I can't code up the structure in XAML. The only thing I have in the XAML is the grid with definitions for three columns and one row.

StorageFolder picturesFolder = KnownFolders.PicturesLibrary;
StorageFolderQueryResult queryResult = picturesFolder.CreateFolderQuery();
IReadOnlyList<IStorageFolder> folderList = await queryResult.GetFoldersAsync();

int row = 0, col = 0;
foreach (IStorageFolder folder in folderList)
{
    IReadOnlyList<IStorageFile> fileList = await folder.GetFilesAsync();
    foreach (IStorageFile file in fileList)
    {
        Image image = new Image();
        image.Source = await LoadImageFromFileAsync(file);
        image.Width = 150;
        image.Height = 150;
        Grid.SetColumn(image, col);
        Grid.SetRow(image, row);
        ++col;
        if (col == 3)
        { 
            col = 0; 
            ++row;
            RowDefinition rd = new RowDefinition();
            GridLength gl = new GridLength() { Value = 150 };
            rd.Height = gl;
            grid.RowDefinitions.Add(rd); 
        }
        grid.Children.Add(image);
    }
}

So: how do I get rid of the flickering? Is there a way to set up an item template in the XAML to create three columns of items?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

全部不再 2024-12-28 04:21:44

您是否严格需要 3 列?
在任何情况下,您可能需要考虑使用带有 WrapGrid 的 ItemsControl 作为其 ItemsPanelTemplate,而不是像此 页面。希望这有帮助。

Do you strictly need 3 columns?
In any case, you may want to consider using an ItemsControl with WrapGrid as its ItemsPanelTemplate instead like on this page. Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文