WPF,MVVM,DI,load asynchronously, CancellationTokenSource, populate collections use DispactherPriority.Background will resolve UI blocking issues

Install-Package Microsoft.Extensions.DependencyInjection;
Install-Package CommunityToolkit.MVVM;
Install-Package Microsoft.Xaml.Behaviors.WPF;
using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace WpfApp18
{
    /// <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;
            Loaded+= async (s, e) => await LoadedAsync(s, e);
        }

        private async Task LoadedAsync(object s, RoutedEventArgs e)
        {
            if (DataContext is MainVM vm)
            {
                await vm.InitializeAsyc();
            }
        }

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

    public partial class MainVM : ObservableObject, IDisposable
    {
        IIDService idService;
        INameService nameService;
        IISBNService isbnService;
        IDialogService dialogService;
        IMemoryService memoryService;
        CancellationTokenSource cts;
        // Use caching or load images on demand
        private static readonly Dictionary<string, BitmapImage> imgDicCache = new Dictionary<string, BitmapImage>();
        public MainVM(IIDService idServiceValue,
            INameService nameServiceValue,
            IISBNService isbnServiceValue,
            IDialogService dialogServiceValue,
            IMemoryService memoryServiceValue)
        {
            idService=idServiceValue;
            nameService=nameServiceValue;
            isbnService=isbnServiceValue;
            dialogService=dialogServiceValue;
            memoryService=memoryServiceValue;
            cts=new CancellationTokenSource();
            RightMouseDownCommand=new DelCommand(RightMouseDownCommandExecuted);
        }

        private void RightMouseDownCommandExecuted(object? obj)
        {
            cts?.Cancel();
        }

        public async Task InitializeAsyc()
        {
            await InitData(cts.Token);
        }         

        private async Task InitData(CancellationToken token)
        {
            try
            {
                string 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 = 1; i<20000001; i++)
                    {
                        booksList.Add(new Book()
                        {
                            Id=idService.GetID(),
                            Name=nameService.GetName(),
                            ISBN=isbnService.GetISBN(),
                            Comment=$"Comment_{i}",
                            Author=$"Author_{i}",
                            Summary=$"Summary_{i}",
                            Title=$"Title_{i}",
                            Topic=$"Topic_{i}",
                            ImgSource=GetImgSourceViaUrl(imgs[i%imgsCount])
                        });

                        if (token.IsCancellationRequested)
                        {
                            await dialogService.ShowMessage("Cancelled initialization", "Cancelled!");
                            return;
                        }

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

                    if (booksList.Count>0)
                    {
                        await PopulateBooksCollection(booksList);
                    }
                });

                if(!cts.IsCancellationRequested)
                {
                    await dialogService.ShowMessage($"Initialization completely,loaded {BooksCollection.Count} items,memory:{memoryService.GetMemory()}");
                }
            }
            catch (Exception ex)
            {
                StatusMsg=$"Loaded failed";
                await dialogService.ShowMessage($"{ex.Message}\n{ex.StackTrace}");
            }
        }

        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:{memoryService.GetMemory()}";
                // Use lower priority to keep UI responsive
            }, System.Windows.Threading.DispatcherPriority.Background);
        }

        private ImageSource GetImgSourceViaUrl(string imgUrl)
        {
            if (imgDicCache.TryGetValue(imgUrl, out var cacheBmi))
            {
                return cacheBmi;
            }

            BitmapImage bmi = new BitmapImage();
            bmi.BeginInit();
            bmi.UriSource=new Uri(imgUrl, UriKind.RelativeOrAbsolute);
            bmi.CacheOption = BitmapCacheOption.OnDemand;
            bmi.EndInit();
            bmi.Freeze();
            imgDicCache[imgUrl] = bmi;
            return bmi;
        }

        public void Dispose()
        {
            cts?.Cancel();
            cts?.Dispose();
            cts=null;
        }

        [ObservableProperty]
        private double gridWidth;

        [ObservableProperty]
        private double gridHeight;

        [ObservableProperty]
        private ObservableCollection<Book> booksCollection;

        [ObservableProperty]
        private string statusMsg;
        public ICommand RightMouseDownCommand { get; set; }
    }


    #region Services

    public interface IIDService
    {
        int GetID();
    }

    //make service thread-safe with Interlocked for better performance.
    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
    {
        Task ShowMessage(string message = "", string caption = "");
    }

    public class DialogService : IDialogService
    {
        public Task ShowMessage(string message, string caption)
        {
            return Application.Current.Dispatcher.InvokeAsync(() =>
            {
                MessageBox.Show(message, caption);
            }).Task;
        }
    }

    public interface IMemoryService
    {
        string GetMemory();
    }

    public class MemoryService : IMemoryService
    {
        public string GetMemory()
        {
            double memoryInMB = Process.GetCurrentProcess().PrivateMemorySize64 / 1024.0 / 1024.0;
            return $"{memoryInMB.ToString("#,##0.00")} MB";
        }
    }

    #endregion

    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 Summary { get; set; }
        public string Title { get; set; }
        public string Topic { get; set; }
        public string ISBN { get; set; }
        public ImageSource ImgSource { get; set; }
    }


    public class DelCommand : ICommand
    {
        private Action<object?> execute;
        private Predicate<object?> canExecute;
        public DelCommand(Action<object?> executeValue, Predicate<object?> canExecuteValue = null)
        {
            execute = executeValue;
            canExecute = canExecuteValue;
        }

        public event EventHandler? CanExecuteChanged
        {
            add
            {
                CommandManager.RequerySuggested += value;
            }
            remove
            {
                CommandManager.RequerySuggested -= value;
            }
        }

        public bool CanExecute(object? parameter)
        {
            return canExecute==null ? true : canExecute(parameter);
        }

        public void Execute(object? parameter)
        {
            execute(parameter);
        }
    }
}

 

 

 

 

image

 

 

 

 

image

 

 

 

 

 

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


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

namespace WpfApp18
{
    /// <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(IServiceCollection services)
        {
            services.AddSingleton<IIDService, IDService>();
            services.AddSingleton<INameService, NameService>();
            services.AddSingleton<IISBNService, ISBNService>();
            services.AddSingleton<IDialogService, DialogService>();
            services.AddSingleton<IMemoryService, MemoryService>();
            services.AddSingleton<MainWindow>();
            services.AddSingleton<MainVM>();
        }

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

}



//MainWindow.xaml
<Window x:Class="WpfApp18.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:behavior="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:local="clr-namespace:WpfApp18"
        WindowState="Maximized"
        mc:Ignorable="d"
        Title="{Binding StatusMsg,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
        Height="450" Width="800">
    <behavior:Interaction.Triggers>
        <behavior:EventTrigger EventName="MouseRightButtonDown">
            <behavior:InvokeCommandAction Command="{Binding RightMouseDownCommand}"/>
        </behavior:EventTrigger>
    </behavior:Interaction.Triggers>
    <Grid>
        <DataGrid ItemsSource="{Binding BooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                  VirtualizingPanel.IsVirtualizing="True"
                  VirtualizingPanel.VirtualizationMode="Recycling"
                  ScrollViewer.IsDeferredScrollingEnabled="True"
                  AutoGenerateColumns="False"
                  EnableColumnVirtualization="True"
                  EnableRowVirtualization="True">
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <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="HorizontalAlignment" Value="Center"/>
                                        <Setter Property="VerticalAlignment" Value="Center"/>
                                        <Setter Property="FontSize" Value="30"/>
                                        <Style.Triggers>
                                            <Trigger Property="IsMouseOver" Value="True">
                                                <Setter Property="FontSize" Value="50"/>
                                                <Setter Property="Foreground" Value="Red"/>
                                            </Trigger>
                                        </Style.Triggers>
                                    </Style>
                                </Grid.Resources>
                                <Grid.RowDefinitions>
                                    <RowDefinition/>
                                    <RowDefinition/>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition/>
                                    <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 Summary}"/>
                                <TextBlock Grid.Row="0" Grid.Column="5" Text="{Binding Title}"/>
                                <TextBlock Grid.Row="0" Grid.Column="6" Text="{Binding Topic}"/>
                                <TextBlock Grid.Row="1" Grid.Column="0" 
                                           Grid.ColumnSpan="7" Text="{Binding ISBN}"/>
                            </Grid>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>



//MainWindow.cs
using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace WpfApp18
{
    /// <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;
            Loaded+= async (s, e) => await LoadedAsync(s, e);
        }

        private async Task LoadedAsync(object s, RoutedEventArgs e)
        {
            if (DataContext is MainVM vm)
            {
                await vm.InitializeAsyc();
            }
        }

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

    public partial class MainVM : ObservableObject, IDisposable
    {
        IIDService idService;
        INameService nameService;
        IISBNService isbnService;
        IDialogService dialogService;
        IMemoryService memoryService;
        CancellationTokenSource cts;
        // Use caching or load images on demand
        private static readonly Dictionary<string, BitmapImage> imgDicCache = new Dictionary<string, BitmapImage>();
        public MainVM(IIDService idServiceValue,
            INameService nameServiceValue,
            IISBNService isbnServiceValue,
            IDialogService dialogServiceValue,
            IMemoryService memoryServiceValue)
        {
            idService=idServiceValue;
            nameService=nameServiceValue;
            isbnService=isbnServiceValue;
            dialogService=dialogServiceValue;
            memoryService=memoryServiceValue;
            cts=new CancellationTokenSource();
            RightMouseDownCommand=new DelCommand(RightMouseDownCommandExecuted);
        }

        private void RightMouseDownCommandExecuted(object? obj)
        {
            cts?.Cancel();
        }

        public async Task InitializeAsyc()
        {
            await InitData(cts.Token);
        }         

        private async Task InitData(CancellationToken token)
        {
            try
            {
                string 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 = 1; i<20000001; i++)
                    {
                        booksList.Add(new Book()
                        {
                            Id=idService.GetID(),
                            Name=nameService.GetName(),
                            ISBN=isbnService.GetISBN(),
                            Comment=$"Comment_{i}",
                            Author=$"Author_{i}",
                            Summary=$"Summary_{i}",
                            Title=$"Title_{i}",
                            Topic=$"Topic_{i}",
                            ImgSource=GetImgSourceViaUrl(imgs[i%imgsCount])
                        });

                        if (token.IsCancellationRequested)
                        {
                            await dialogService.ShowMessage("Cancelled initialization", "Cancelled!");
                            return;
                        }

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

                    if (booksList.Count>0)
                    {
                        await PopulateBooksCollection(booksList);
                    }
                });

                if(!cts.IsCancellationRequested)
                {
                    await dialogService.ShowMessage($"Initialization completely,loaded {BooksCollection.Count} items,memory:{memoryService.GetMemory()}");
                }
            }
            catch (Exception ex)
            {
                StatusMsg=$"Loaded failed";
                await dialogService.ShowMessage($"{ex.Message}\n{ex.StackTrace}");
            }
        }

        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:{memoryService.GetMemory()}";
                // Use lower priority to keep UI responsive
            }, System.Windows.Threading.DispatcherPriority.Background);
        }

        private ImageSource GetImgSourceViaUrl(string imgUrl)
        {
            if (imgDicCache.TryGetValue(imgUrl, out var cacheBmi))
            {
                return cacheBmi;
            }

            BitmapImage bmi = new BitmapImage();
            bmi.BeginInit();
            bmi.UriSource=new Uri(imgUrl, UriKind.RelativeOrAbsolute);
            bmi.CacheOption = BitmapCacheOption.OnDemand;
            bmi.EndInit();
            bmi.Freeze();
            imgDicCache[imgUrl] = bmi;
            return bmi;
        }

        public void Dispose()
        {
            cts?.Cancel();
            cts?.Dispose();
            cts=null;
        }

        [ObservableProperty]
        private double gridWidth;

        [ObservableProperty]
        private double gridHeight;

        [ObservableProperty]
        private ObservableCollection<Book> booksCollection;

        [ObservableProperty]
        private string statusMsg;
        public ICommand RightMouseDownCommand { get; set; }
    }


    #region Services

    public interface IIDService
    {
        int GetID();
    }

    //make service thread-safe with Interlocked for better performance.
    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
    {
        Task ShowMessage(string message = "", string caption = "");
    }

    public class DialogService : IDialogService
    {
        public Task ShowMessage(string message, string caption)
        {
            return Application.Current.Dispatcher.InvokeAsync(() =>
            {
                MessageBox.Show(message, caption);
            }).Task;
        }
    }

    public interface IMemoryService
    {
        string GetMemory();
    }

    public class MemoryService : IMemoryService
    {
        public string GetMemory()
        {
            double memoryInMB = Process.GetCurrentProcess().PrivateMemorySize64 / 1024.0 / 1024.0;
            return $"{memoryInMB.ToString("#,##0.00")} MB";
        }
    }

    #endregion

    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 Summary { get; set; }
        public string Title { get; set; }
        public string Topic { get; set; }
        public string ISBN { get; set; }
        public ImageSource ImgSource { get; set; }
    }


    public class DelCommand : ICommand
    {
        private Action<object?> execute;
        private Predicate<object?> canExecute;
        public DelCommand(Action<object?> executeValue, Predicate<object?> canExecuteValue = null)
        {
            execute = executeValue;
            canExecute = canExecuteValue;
        }

        public event EventHandler? CanExecuteChanged
        {
            add
            {
                CommandManager.RequerySuggested += value;
            }
            remove
            {
                CommandManager.RequerySuggested -= value;
            }
        }

        public bool CanExecute(object? parameter)
        {
            return canExecute==null ? true : canExecute(parameter);
        }

        public void Execute(object? parameter)
        {
            execute(parameter);
        }
    }
}

 

posted @ 2025-09-18 11:24  FredGrit  阅读(13)  评论(0)    收藏  举报