1

我正在使用代码创建动态 UI。我正在使用 for 循环将网格添加到页面中,在多行和 4 列中,因此在页面上加载 UI 需要时间。我想懒惰地加载每个子网格,我不知道怎么做,请帮忙。这是 XAML 文件

<ScrollViewer Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CanContentScroll="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Margin="0 0 0 0">
    <Grid x:Name="mainGrid" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="10" Width="Auto">
    </Grid>
</ScrollViewer>

这是来自 .cs 文件的伪代码

for (int column = 0; column < dsMacDtls.Tables[0].Rows.Count; column++)
{
    grid = new Grid();
    grid.Width = 108;
    grid.Height = 108;

    if (column % 4 == 0)
    {
        mainGrid.RowDefinitions.Add(new RowDefinition());                    
    }
    else
    {
        mainGrid.RowDefinitions.Add(new RowDefinition());
    }

    box = new CheckBox();               
    TextBlock tb = new TextBlock();               
    tb.Text = "abcd";
    box.Content = tb;
    grid.Children.Add(box);

    ImageBrush myBrush = new ImageBrush();
    image = new Image();
    image.Source = new BitmapImage(new Uri("abcd.png"));
    grid.Background = myBrush;
    Grid.SetColumn(grid, 0);
    Grid.SetRow(grid, i);
    Border br = new Border();
    br.BorderThickness = new Thickness(1);
    br.BorderBrush = new SolidColorBrush(Colors.Gray);
    Grid.SetColumn(br, 0);
    Grid.SetRow(br, rowNumberofMachine);
    br.Width = 108;
    br.Height = 108;
    grid.Children.Add(br);
    mainGrid.Children.Add(grid);
}
4

0 回答 0