Install-Package Prism.Wpf
Install-Package Prism.DryIOC
//App.xaml
<prism:PrismApplication x:Class="WpfApp46.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp46"
xmlns:prism="http://prismlibrary.com/">
</prism:PrismApplication>
//App.xaml.cs
using System.Configuration;
using System.Data;
using System.Security.Cryptography.Xml;
using System.Windows;
using Prism.Ioc;
namespace WpfApp46
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : PrismApplication
{
protected override Window CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.Register<IIDService, IDService>();
containerRegistry.Register<IISBNService, ISBNService>();
containerRegistry.Register<MainWindow>();
containerRegistry.Register<MainWindowViewModel>();
}
}
public interface IIDService
{
int GetID();
}
public class IDService : IIDService
{
int cnt = 0;
public int GetID()
{
return Interlocked.Increment(ref cnt);
}
}
public interface IISBNService
{
string GetISBN();
}
public class ISBNService : IISBNService
{
int idx = 0;
public string GetISBN()
{
return $"ISBN_{Interlocked.Increment(ref idx)}_{Guid.NewGuid():N}";
}
}
}
//MainWindow.xaml
<Window x:Class="WpfApp46.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:WpfApp46"
mc:Ignorable="d"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
WindowState="Maximized"
Title="{Binding MainTitle,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
<Grid>
<DataGrid ItemsSource="{Binding BooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
VirtualizingPanel.IsVirtualizing="True"
VirtualizingPanel.CacheLengthUnit="Item"
VirtualizingPanel.CacheLength="2,2"
AutoGenerateColumns="False"
CanUserAddRows="False"
ScrollViewer.CanContentScroll="True"
ScrollViewer.IsDeferredScrollingEnabled="True">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid Width="{Binding DataContext.GridWidth,RelativeSource={RelativeSource
Mode=FindAncestor,AncestorType={x:Type Window}}}"
Height="{Binding DataContext.GridHeight,RelativeSource={RelativeSource
Mode=FindAncestor,AncestorType={x:Type Window}}}">
<Grid.Background>
<ImageBrush ImageSource="{Binding ImgSource}"/>
</Grid.Background>
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="60"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="FontSize" Value="100"/>
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<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.ColumnSpan="2"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
//MainWindow.cs
using Prism.Common;
using System.Collections.Concurrent;
using System.Collections.ObjectModel;
using System.Diagnostics;
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;
using System.Xml.Linq;
namespace WpfApp46
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += async (s, e) =>
{
var vm = this.DataContext as MainWindowViewModel;
if (vm != null)
{
vm.GridWidth = this.ActualWidth;
vm.GridHeight = this.ActualHeight;
await vm.InitBooksCollectionAsync();
}
};
}
}
public class MainWindowViewModel : BindableBase
{
IIDService idService;
IISBNService isbnService;
private ConcurrentDictionary<string, ImageSource> imgDicCache = new ConcurrentDictionary<string, ImageSource>();
public MainWindowViewModel(IIDService idServiceValue, IISBNService isbnServiceValue)
{
idService = idServiceValue;
isbnService = isbnServiceValue;
}
public async Task InitBooksCollectionAsync()
{
BooksCollection = new ObservableCollection<Book>();
List<Book> booksList = new List<Book>();
var imgsList = Directory.GetFiles("../../../Images");
int imgsCount = imgsList.Count();
for (int i = 1; i < 10000001; i++)
{
booksList.Add(new Book()
{
Id = i,
Name = $"Name_{i}",
ISBN = $"ISBN_{i}_{Guid.NewGuid():N}",
ImgSource = GetImgSource(imgsList[i % imgsCount])
});
if (i % 1000000 == 0)
{
var tempList = booksList.ToList();
booksList.Clear();
await Application.Current.Dispatcher.InvokeAsync(() =>
{
foreach(var bk in tempList)
{
BooksCollection.Add(bk);
}
},System.Windows.Threading.DispatcherPriority.Background);
MainTitle=$"{DateTime.Now.ToString("yyyyMMddHHmmssffff")}," +
$"loaded {BooksCollection.Count()} items," +
$"Memory,{GetMemory()}";
}
}
}
private string GetMemory()
{
var proc=Process.GetCurrentProcess();
return $"{proc.PrivateMemorySize64 / 1024 / 1024} M";
}
private ImageSource GetImgSource(string imgUrl)
{
if (!File.Exists(imgUrl))
{
return null;
}
if (imgDicCache.TryGetValue(imgUrl, out ImageSource imgSource))
{
return imgSource;
}
BitmapImage bmi = new BitmapImage();
bmi.BeginInit();
bmi.UriSource = new Uri(imgUrl, UriKind.RelativeOrAbsolute);
bmi.EndInit();
if (bmi.CanFreeze)
{
bmi.Freeze();
}
imgDicCache[imgUrl] = bmi;
return bmi;
}
private string mainTitle;
public string MainTitle
{
get
{
return mainTitle;
}
set
{
SetProperty(ref mainTitle, value);
}
}
private ObservableCollection<Book> booksCollection;
public ObservableCollection<Book> BooksCollection
{
get
{
return booksCollection;
}
set
{
SetProperty(ref booksCollection, value);
}
}
private double gridWidth;
public double GridWidth
{
get
{
return gridWidth;
}
set
{
SetProperty(ref gridWidth, value);
}
}
private double gridHeight;
public double GridHeight
{
get
{
return gridHeight;
}
set
{
SetProperty(ref gridHeight, value);
}
}
}
public class Book
{
public int Id { get; set; }
public string Name { get; set; }
public string ISBN { get; set; }
public ImageSource ImgSource { get; set; }
}
}
![image]()
![image]()