Install-Package Prism.Wpf;
Install-Package Prism.DryIOC;
using System.Configuration;
using System.Data;
using System.Windows;
namespace WpfApp42
{
/// <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.RegisterSingleton<IIDService, IDService>();
containerRegistry.RegisterSingleton<IISBNService, ISBNService>();
containerRegistry.RegisterSingleton<INameService, NameService>();
containerRegistry.RegisterSingleton<MainWindow>();
containerRegistry.RegisterSingleton<MainWindowViewModel>();
}
}
public interface IIDService
{
int GetId();
}
public class IDService : IIDService
{
int id = 0;
public int GetId()
{
return Interlocked.Increment(ref id);
}
}
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 INameService
{
string GetName();
}
public class NameService : INameService
{
int idx = 0;
public string GetName()
{
return $"Name_{Interlocked.Increment(ref idx)}";
}
}
}
public MainWindowViewModel(IIDService idServiceValue, INameService nameServiceValue,
IISBNService isbnServiceValue,IEventAggregator eventAggregatorValue)
{
idService = idServiceValue;
nameService = nameServiceValue;
isbnService = isbnServiceValue;
eventAggregator = eventAggregatorValue;
eventAggregator.GetEvent<MsgPayLoad>().Subscribe(OnMsgPayloadReceived);
}
private void OnMsgPayloadReceived(MsgLoad msgLoad)
{
MainTitle = msgLoad.ToString();
}
MsgLoad msg = new MsgLoad()
{
Idx = ++batchIdx,
Msg = $"Loaded {BooksCollection.Count} books!",
DTNow = DateTime.Now
};
eventAggregator.GetEvent<MsgPayLoad>().Publish(msg);
![image]()
![image]()
![image]()
<prism:PrismApplication x:Class="WpfApp42.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp42"
xmlns:prism="http://prismlibrary.com/">
</prism:PrismApplication>
using System.Configuration;
using System.Data;
using System.Windows;
namespace WpfApp42
{
/// <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.RegisterSingleton<IIDService, IDService>();
containerRegistry.RegisterSingleton<IISBNService, ISBNService>();
containerRegistry.RegisterSingleton<INameService, NameService>();
containerRegistry.RegisterSingleton<MainWindow>();
containerRegistry.RegisterSingleton<MainWindowViewModel>();
}
}
public interface IIDService
{
int GetId();
}
public class IDService : IIDService
{
int id = 0;
public int GetId()
{
return Interlocked.Increment(ref id);
}
}
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 INameService
{
string GetName();
}
public class NameService : INameService
{
int idx = 0;
public string GetName()
{
return $"Name_{Interlocked.Increment(ref idx)}";
}
}
}
<Window x:Class="WpfApp42.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:WpfApp42"
mc:Ignorable="d"
xmlns:prism="http://prismlibrary.com/"
WindowState="Maximized"
prism:ViewModelLocator.AutoWireViewModel="True"
Title="{Binding MainTitle,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Height="450" Width="800">
<Grid>
<DataGrid ItemsSource="{Binding BooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
VirtualizingPanel.IsVirtualizing="True"
VirtualizingPanel.CacheLength="2,2"
VirtualizingPanel.CacheLengthUnit="Item"
CanUserAddRows="False"
AutoGenerateColumns="False"
ScrollViewer.CanContentScroll="True"
ScrollViewer.IsDeferredScrollingEnabled="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="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/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<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 ISBN}"/>
<TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding Author}"/>
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Comment}"/>
<TextBlock Grid.Row="1" Grid.Column="2" Text="{Binding Content}"/>
<TextBlock Grid.Row="2" Grid.Column="0" Text="{Binding Summary}"/>
<TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Title}"/>
<TextBlock Grid.Row="2" Grid.Column="2" Text="{Binding Topic}"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
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 WpfApp42
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += async (s, e) =>
{
if(this.DataContext is MainWindowViewModel vm)
{
vm.GridWidth=this.ActualWidth;
vm.GridHeight = this.ActualHeight / 2;
await vm.InitBooksCollection();
}
};
}
}
public class MainWindowViewModel : BindableBase
{
IIDService idService;
INameService nameService;
IISBNService isbnService;
IEventAggregator eventAggregator;
Dictionary<string, ImageSource> imgCacheDic = new Dictionary<string, ImageSource>();
int batchIdx = 0;
public MainWindowViewModel(IIDService idServiceValue, INameService nameServiceValue,
IISBNService isbnServiceValue,IEventAggregator eventAggregatorValue)
{
idService = idServiceValue;
nameService = nameServiceValue;
isbnService = isbnServiceValue;
eventAggregator = eventAggregatorValue;
eventAggregator.GetEvent<MsgPayLoad>().Subscribe(OnMsgPayloadReceived);
}
private void OnMsgPayloadReceived(MsgLoad msgLoad)
{
MainTitle = msgLoad.ToString();
}
public async Task InitBooksCollection()
{
BooksCollection = new ObservableCollection<Book>();
List<Book> booksList = new List<Book>();
var imgs = Directory.GetFiles(@"../../../Images");
if (imgs != null && !imgs.Any())
{
return;
}
int imgsCount = imgs.Count();
for (int i = 1; i < 50000001; i++)
{
booksList.Add(new Book()
{
Id = idService.GetId(),
Name = nameService.GetName(),
ISBN = isbnService.GetISBN(),
Author = $"ISBN_{i}",
Comment = $"Comment_{i}",
Content = $"Content_{i}",
Summary = $"Summary_{i}",
Title = $"Title_{i}",
Topic = $"Topic_{i}",
ImgSource = GetImgSource(imgs[i % imgsCount])
});
if (i % 100000 == 0)
{
await PopulateBooksCollectionAsync(booksList);
}
}
MainTitle = $"Loaded {BooksCollection.Count} books completed!";
}
private async Task PopulateBooksCollectionAsync(List<Book> booksList)
{
await Application.Current.Dispatcher.InvokeAsync(() =>
{
foreach (var bk in booksList)
{
BooksCollection.Add(bk);
}
booksList.Clear();
//MainTitle = $"Loaded {BooksCollection.Count} books!";
MsgLoad msg = new MsgLoad()
{
Idx = ++batchIdx,
Msg = $"Loaded {BooksCollection.Count} books!",
DTNow = DateTime.Now
};
eventAggregator.GetEvent<MsgPayLoad>().Publish(msg);
}, System.Windows.Threading.DispatcherPriority.Background);
}
private ImageSource GetImgSource(string imgUrl)
{
if (!File.Exists(imgUrl))
{
return null;
}
if (imgCacheDic.ContainsKey(imgUrl))
{
return imgCacheDic[imgUrl];
}
BitmapImage bmi = new BitmapImage();
bmi.BeginInit();
bmi.UriSource = new Uri(imgUrl, UriKind.RelativeOrAbsolute);
bmi.EndInit();
if (bmi.CanFreeze)
{
bmi.Freeze();
}
imgCacheDic[imgUrl] = bmi;
return bmi;
}
private string mainTitle;
public string MainTitle
{
get
{
return mainTitle;
}
set
{
SetProperty(ref mainTitle, 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);
}
}
private ObservableCollection<Book> booksCollection;
public ObservableCollection<Book> BooksCollection
{
get
{
return booksCollection;
}
set
{
SetProperty(ref booksCollection, value);
}
}
}
public class Book
{
public int Id { get; set; }
public string Name { get; set; }
public string ISBN { get; set; }
public string Author { get; set; }
public string Comment { get; set; }
public string Content { get; set; }
public string Summary { get; set; }
public string Title { get; set; }
public string Topic { get; set; }
public ImageSource ImgSource { get; set; }
}
public class MsgLoad
{
public int Idx { get; set; }
public DateTime DTNow { get; set; }
public string Msg { get; set; }
public override string ToString()
{
return $"Idx:{Idx},Now:{DateTime.Now.ToString("yyyyMMddHHmmssffff")},Msg:{Msg}";
}
}
public class MsgPayLoad:PubSubEvent<MsgLoad>
{
}
}