WPF ListBox load 10M items and no ui block issues, load data asynchronously, populate data DispatcherPriority.Background. resolve service via DependencyInjection

Install-Package Microsoft.Extensions.DependencyInjection;
Install-Package CommunityToolkit.MVVM;

 

//Virtualization
<ListBox ItemsSource="{Binding BooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
         VirtualizingPanel.IsVirtualizing="True"
         VirtualizingPanel.VirtualizationMode="Recycling"
         ScrollViewer.IsDeferredScrollingEnabled="True" />

 

 

//Load  data asynchronously and DispatcherPriority.Background

 private async Task InitData()
 {
     string imagesDir = @"../../../Images";
     if (!Directory.Exists(imagesDir))
     {
         return;
     }

     var imgs = Directory.GetFiles(imagesDir);
     if (imgs==null || !imgs.Any())
     {
         return;
     }

     int imgsCount = imgs.Count();

     BooksCollection=new ObservableCollection<Book>();
     List<Book> booksList = new List<Book>();

     await Task.Run(async () =>
     {
         for (int i = 1; i<10000001; i++)
         {
             booksList.Add(new Book()
             {
                 Id=idService.GetID(),
                 Name=nameService.GetName(),
                 ISBN=isbnService.GetISBN(),
                 Author=$"Author_{i}",
                 Title=$"Title_{i}",
                 Topic=$"Topic_{i}",
                 Comment=$"Comment_{i}",
                 ImgSource=GetImgsSourceViaUrl(imgs[i%imgsCount])
             });

             if (i<10001 && i%1000==0)
             {
               await PopulateBooksCollection(booksList);
             }
             else if (i>=10001 && i%1000000==0)
             {
                await PopulateBooksCollection(booksList);
             }
         }

         if (booksList.Count>0)
         {
            await PopulateBooksCollection(booksList);
         }
     });
     MessageBox.Show($"Initialization finished, loaded {BooksCollection.Count} items");
 }

 private async Task PopulateBooksCollection(List<Book> booksList)
 {
     var tempList = booksList.ToList();
     booksList.Clear();
     await Application.Current.Dispatcher.InvokeAsync(() =>
     {
         foreach (var bk in tempList)
         {
             BooksCollection.Add(bk);
         }
         StatusMsg=$"Loaded {BooksCollection.Count} items";
     }, System.Windows.Threading.DispatcherPriority.Background);
 }

 private ImageSource GetImgsSourceViaUrl(string imgUrl)
 {
     BitmapImage bmi = new BitmapImage();
     bmi.BeginInit();
     bmi.UriSource=new Uri(imgUrl, UriKind.RelativeOrAbsolute);
     bmi.EndInit();
     if (bmi.CanFreeze)
     {
         bmi.Freeze();
     }
     return bmi;
 }

 

 

 

 

image

 

 

 

image

 

 

//App.xaml
<Application x:Class="WpfApp17.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp17">
    <Application.Resources>
         
    </Application.Resources>
</Application>


//App.xaml.cs
using Microsoft.Extensions.DependencyInjection;
using System.Configuration;
using System.Data;
using System.Windows;

namespace WpfApp17
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        ServiceProvider serviceProvider;
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            var services = new ServiceCollection();
            ConfigureServices(services);

            serviceProvider= services.BuildServiceProvider();
            var mainWin = serviceProvider.GetRequiredService<MainWindow>();
            mainWin?.Show();
        }

        private void ConfigureServices(ServiceCollection services)
        {
            services.AddSingleton<IIDservice, IDService>();
            services.AddSingleton<INameService, NameService>();
            services.AddSingleton<IISBNService, ISBNService>();
            services.AddSingleton<MainVM>();
            services.AddSingleton<MainWindow>();
        }

        protected override void OnExit(ExitEventArgs e)
        {
            base.OnExit(e);
            serviceProvider?.Dispose();
        }
    }

}



//MainWindow.xaml
<Window x:Class="WpfApp17.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp17"
        WindowState="Maximized"
        mc:Ignorable="d"
        Title="{Binding StatusMsg,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
        Height="450" Width="800">
    <Grid>
        <ListBox ItemsSource="{Binding BooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                 VirtualizingPanel.IsVirtualizing="True"
                 VirtualizingPanel.VirtualizationMode="Recycling"
                 ScrollViewer.IsDeferredScrollingEnabled="True" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Width="{Binding DataContext.GridWidth,RelativeSource={RelativeSource AncestorType=Window}}"
                          Height="{Binding DataContext.GridHeight,RelativeSource={RelativeSource AncestorType=Window}}">
                        <Grid.Background>
                            <ImageBrush ImageSource="{Binding ImgSource}"/>
                        </Grid.Background>
                        <Grid.Resources>
                            <Style TargetType="TextBlock">
                                <Setter Property="FontSize" Value="30"/>
                                <Style.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter Property="Foreground" Value="Red"/>
                                        <Setter Property="FontSize" Value="50"/>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </Grid.Resources>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Id}"/>
                        <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Name}"/>
                        <TextBlock Grid.Row="0" Grid.Column="2" Text="{Binding Author}"/>
                        <TextBlock Grid.Row="0" Grid.Column="3" Text="{Binding Comment}"/>
                        <TextBlock Grid.Row="0" Grid.Column="4" Text="{Binding Title}"/>
                        <TextBlock Grid.Row="0" Grid.Column="5" Text="{Binding Topic}"/>
                        <TextBlock Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="6" Text="{Binding ISBN}"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>



//MainWindow.xaml.cs
using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp17
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public MainWindow(MainVM vm)
        {
            InitializeComponent();
            this.DataContext = vm;
            this.SizeChanged+=MainWindow_SizeChanged;
        }

        private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (DataContext is MainVM vm)
            {
                var fe = this.Content as FrameworkElement;
                if (fe==null)
                {
                    return;
                }
                vm.GridWidth=fe.ActualWidth;
                vm.GridHeight=fe.ActualHeight/2;
            }
        }
    }

    public partial class MainVM : ObservableObject
    {
        private IIDservice idService;
        private INameService nameService;
        private IISBNService isbnService;
        public MainVM(IIDservice idServiceValue, INameService nameServiceValue,
            IISBNService isbnServiceValue)
        {
            idService=idServiceValue;
            nameService=nameServiceValue;
            isbnService=isbnServiceValue;
            InitData();
            StatusMsg="Loading...";
        }

        private async Task InitData()
        {
            string imagesDir = @"../../../Images";
            if (!Directory.Exists(imagesDir))
            {
                return;
            }

            var imgs = Directory.GetFiles(imagesDir);
            if (imgs==null || !imgs.Any())
            {
                return;
            }

            int imgsCount = imgs.Count();

            BooksCollection=new ObservableCollection<Book>();
            List<Book> booksList = new List<Book>();

            await Task.Run(async () =>
            {
                for (int i = 1; i<10000001; i++)
                {
                    booksList.Add(new Book()
                    {
                        Id=idService.GetID(),
                        Name=nameService.GetName(),
                        ISBN=isbnService.GetISBN(),
                        Author=$"Author_{i}",
                        Title=$"Title_{i}",
                        Topic=$"Topic_{i}",
                        Comment=$"Comment_{i}",
                        ImgSource=GetImgsSourceViaUrl(imgs[i%imgsCount])
                    });

                    if (i<10001 && i%1000==0)
                    {
                      await PopulateBooksCollection(booksList);
                    }
                    else if (i>=10001 && i%1000000==0)
                    {
                       await PopulateBooksCollection(booksList);
                    }
                }

                if (booksList.Count>0)
                {
                   await PopulateBooksCollection(booksList);
                }
            });
            MessageBox.Show($"Initialization finished, loaded {BooksCollection.Count} items");
        }

        private async Task PopulateBooksCollection(List<Book> booksList)
        {
            var tempList = booksList.ToList();
            booksList.Clear();
            await Application.Current.Dispatcher.InvokeAsync(() =>
            {
                foreach (var bk in tempList)
                {
                    BooksCollection.Add(bk);
                }
                StatusMsg=$"Loaded {BooksCollection.Count} items";
            }, System.Windows.Threading.DispatcherPriority.Background);
        }

        private ImageSource GetImgsSourceViaUrl(string imgUrl)
        {
            BitmapImage bmi = new BitmapImage();
            bmi.BeginInit();
            bmi.UriSource=new Uri(imgUrl, UriKind.RelativeOrAbsolute);
            bmi.EndInit();
            if (bmi.CanFreeze)
            {
                bmi.Freeze();
            }
            return bmi;
        }

        [ObservableProperty]
        private double gridWidth;

        [ObservableProperty]
        private double gridHeight;

        [ObservableProperty]
        private ObservableCollection<Book> booksCollection;

        [ObservableProperty]
        private string statusMsg;
    }


    public interface IISBNService
    {
        string GetISBN();
    }

    public class ISBNService : IISBNService
    {
        int idx = 0;
        public string GetISBN()
        {
            return $"ISBN_{++idx}_{Guid.NewGuid():N}";
        }
    }


    public interface INameService
    {
        string GetName();
    }

    public class NameService : INameService
    {
        int idx;

        public string GetName()
        {
            return $"Name_{++idx}";
        }
    }


    public interface IIDservice
    {
        int GetID();
    }

    public class IDService : IIDservice
    {
        int id = 0;

        public int GetID()
        {
            return ++id;
        }
    }

    public class Book
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Author { get; set; }
        public string Comment { get; set; }
        public string Title { get; set; }
        public string Topic { get; set; }
        public string ISBN { get; set; }
        public ImageSource ImgSource { get; set; }
    }
}

 

posted @ 2025-09-17 16:12  FredGrit  阅读(7)  评论(0)    收藏  举报