WPF ListBox loaded 100M items asynchronously in mvvm, virtualization and hardware accleration,image source with url via converter

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

 

 

using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Diagnostics.Eventing.Reader;
using System.Globalization;
using System.IO;
using System.Text;
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 WpfApp20
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public MainWindow(MainVM vm)
        {
            InitializeComponent();
            //Hardware acceleration
            RenderOptions.ProcessRenderMode=System.Windows.Interop.RenderMode.Default;
            this.DataContext=vm;
            this.SizeChanged+=MainWindow_SizeChanged;
            Loaded+=async (s, e) =>
            {
                await LoadDataAsync();
            };
        }

        private async Task LoadDataAsync()
        {
            if (DataContext is MainVM vm)
            {
                await vm.InitDataAsync();
            }
        }

        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;
        private IDialogService dialogService;
        public MainVM(IIDService idServiceValue, INameService nameServiceValue,
            IISBNService isbnServiceValue,IDialogService dialogServiceValue)
        {
            this.idService=idServiceValue;
            this.nameService=nameServiceValue;
            this.isbnService=isbnServiceValue;
            dialogService = dialogServiceValue;
        }

        public async Task InitDataAsync()
        {
            var imgDir = @"../../../Images";
            if (!Directory.Exists(imgDir))
            {
                return;
            }

            var imgs = Directory.GetFiles(imgDir);
            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 = 0; i<100000001; i++)
                {
                    booksList.Add(new Book()
                    {
                        Id=idService.GetID(),
                        Name=nameService.GetName(),
                        ISBN=isbnService.GetISBN(),
                        ImgUrl=imgs[i%imgsCount]
                    });

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

                if (booksList.Count>0)
                {
                    await PopulateBooksCollection(booksList);
                }
            });
            dialogService.ShowMsg($"Loaded {BooksCollection.Count} items finished!Memory:{Utility.GetMemory()}");
        }

        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,Memory:{Utility.GetMemory()}";
            }, System.Windows.Threading.DispatcherPriority.Background);
        }

        [ObservableProperty]
        private ObservableCollection<Book> booksCollection;

        [ObservableProperty]
        private double gridWidth;

        [ObservableProperty]
        private double gridHeight;

        [ObservableProperty]
        private string statusMsg;
    }


    public class Utility
    {
        public static string GetMemory()
        {
            double memory = Process.GetCurrentProcess().PrivateMemorySize64/1024.0d/1024.0d;
            return $"{memory.ToString("#,##0.00")} M";
        }
    }

    public class Book
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string ISBN { get; set; }
        public string ImgUrl { get; set; }
    }

    public interface IIDService
    {
        int GetID();
    }

    public class IDService : IIDService
    {
        int id = 0;
        public int GetID()
        {
            return Interlocked.Increment(ref id);
        }
    }

    public interface INameService
    {
        string GetName();
    }


    public class NameService : INameService
    {
        int idx = 0;
        public string GetName()
        {
            return $"Name_{Interlocked.Increment(ref idx)}";
        }
    }

    public interface IISBNService
    {
        string GetISBN();
    }

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

    public interface IDialogService
    {
        void ShowMsg(string msg = "", string caption = "");
    }

    public class DialogService : IDialogService
    {
        public void ShowMsg(string msg = "", string caption = "")
        {
            MessageBox.Show(msg, caption);
        }
    }

    public class ImagePathConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is string imagePath && !string.IsNullOrEmpty(imagePath))
            {
                try
                {
                    var bitmap = new BitmapImage();
                    bitmap.BeginInit();
                    bitmap.UriSource = new Uri(imagePath, UriKind.RelativeOrAbsolute);
                    bitmap.CacheOption = BitmapCacheOption.OnLoad;
                    bitmap.EndInit();
                    bitmap.Freeze();
                    return bitmap;
                }
                catch
                {
                    return null;
                }
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

 

 

 

 

 

 

 

 

image

 

 

image

 

 

 

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


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

namespace WpfApp20
{
    /// <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 win=serviceProvider.GetRequiredService<MainWindow>();
            win?.Show();
        }

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

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

}



//MainWindow.xaml
<Window x:Class="WpfApp20.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:WpfApp20"
        WindowState="Maximized"
        mc:Ignorable="d"
        Title="{Binding StatusMsg,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
        Height="450" Width="800">
    <Window.Resources>
        <local:ImagePathConverter x:Key="ImagePathConverter"/>
    </Window.Resources>
    <Grid>
        <ListBox ItemsSource="{Binding BooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                 VirtualizingPanel.IsVirtualizing="True"
                 VirtualizingPanel.VirtualizationMode="Recycling"
                 ScrollViewer.IsDeferredScrollingEnabled="True"
                 Background="Transparent">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Width="{Binding DataContext.GridWidth,RelativeSource={RelativeSource AncestorType=Window}}"
                          Height="{Binding DataContext.GridHeight,RelativeSource={RelativeSource AncestorType=Window}}"
                          >
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <Grid.Resources>
                            <Style TargetType="TextBlock">
                                <Setter Property="FontSize" Value="50"/>
                                <Setter Property="HorizontalAlignment" Value="Center"/>
                                <Setter Property="VerticalAlignment" Value="Center"/>
                                <Style.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter Property="FontSize" Value="100"/>
                                        <Setter Property="Foreground" Value="Red"/>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </Grid.Resources>

                        <Image Source="{Binding ImgUrl,Converter={StaticResource ImagePathConverter}}"
                               Grid.Row="0" Grid.RowSpan="2"
                               Grid.Column="0" Grid.ColumnSpan="2" Panel.ZIndex="-1"
                               Stretch="UniformToFill"
                               />
                        <TextBlock Text="{Binding Id}" Grid.Row="0" Grid.Column="0"/>
                        <TextBlock Text="{Binding Name}" Grid.Row="0" Grid.Column="1"/>
                        <TextBlock Text="{Binding ISBN}" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>


//MainWindow.cs
using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Diagnostics.Eventing.Reader;
using System.Globalization;
using System.IO;
using System.Text;
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 WpfApp20
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public MainWindow(MainVM vm)
        {
            InitializeComponent();
            //Hardware acceleration
            RenderOptions.ProcessRenderMode=System.Windows.Interop.RenderMode.Default;
            this.DataContext=vm;
            this.SizeChanged+=MainWindow_SizeChanged;
            Loaded+=async (s, e) =>
            {
                await LoadDataAsync();
            };
        }

        private async Task LoadDataAsync()
        {
            if (DataContext is MainVM vm)
            {
                await vm.InitDataAsync();
            }
        }

        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;
        private IDialogService dialogService;
        public MainVM(IIDService idServiceValue, INameService nameServiceValue,
            IISBNService isbnServiceValue,IDialogService dialogServiceValue)
        {
            this.idService=idServiceValue;
            this.nameService=nameServiceValue;
            this.isbnService=isbnServiceValue;
            dialogService = dialogServiceValue;
        }

        public async Task InitDataAsync()
        {
            var imgDir = @"../../../Images";
            if (!Directory.Exists(imgDir))
            {
                return;
            }

            var imgs = Directory.GetFiles(imgDir);
            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 = 0; i<100000001; i++)
                {
                    booksList.Add(new Book()
                    {
                        Id=idService.GetID(),
                        Name=nameService.GetName(),
                        ISBN=isbnService.GetISBN(),
                        ImgUrl=imgs[i%imgsCount]
                    });

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

                if (booksList.Count>0)
                {
                    await PopulateBooksCollection(booksList);
                }
            });
            dialogService.ShowMsg($"Loaded {BooksCollection.Count} items finished!Memory:{Utility.GetMemory()}");
        }

        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,Memory:{Utility.GetMemory()}";
            }, System.Windows.Threading.DispatcherPriority.Background);
        }

        [ObservableProperty]
        private ObservableCollection<Book> booksCollection;

        [ObservableProperty]
        private double gridWidth;

        [ObservableProperty]
        private double gridHeight;

        [ObservableProperty]
        private string statusMsg;
    }


    public class Utility
    {
        public static string GetMemory()
        {
            double memory = Process.GetCurrentProcess().PrivateMemorySize64/1024.0d/1024.0d;
            return $"{memory.ToString("#,##0.00")} M";
        }
    }

    public class Book
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string ISBN { get; set; }
        public string ImgUrl { get; set; }
    }

    public interface IIDService
    {
        int GetID();
    }

    public class IDService : IIDService
    {
        int id = 0;
        public int GetID()
        {
            return Interlocked.Increment(ref id);
        }
    }

    public interface INameService
    {
        string GetName();
    }


    public class NameService : INameService
    {
        int idx = 0;
        public string GetName()
        {
            return $"Name_{Interlocked.Increment(ref idx)}";
        }
    }

    public interface IISBNService
    {
        string GetISBN();
    }

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

    public interface IDialogService
    {
        void ShowMsg(string msg = "", string caption = "");
    }

    public class DialogService : IDialogService
    {
        public void ShowMsg(string msg = "", string caption = "")
        {
            MessageBox.Show(msg, caption);
        }
    }

    public class ImagePathConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is string imagePath && !string.IsNullOrEmpty(imagePath))
            {
                try
                {
                    var bitmap = new BitmapImage();
                    bitmap.BeginInit();
                    bitmap.UriSource = new Uri(imagePath, UriKind.RelativeOrAbsolute);
                    bitmap.CacheOption = BitmapCacheOption.OnLoad;
                    bitmap.EndInit();
                    bitmap.Freeze();
                    return bitmap;
                }
                catch
                {
                    return null;
                }
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

 

posted @ 2025-09-18 14:31  FredGrit  阅读(9)  评论(0)    收藏  举报