


























<ItemsControl ItemsSource="{Binding BooksCollection}" ScrollViewer.CanContentScroll="True"> <ItemsControl.Template> <ControlTemplate TargetType="ItemsControl"> <ScrollViewer CanContentScroll="True"> <ItemsPresenter/> </ScrollViewer> </ControlTemplate> </ItemsControl.Template> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel IsItemsHost="True" VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling" VirtualizingPanel.CacheLength="5,5" VirtualizingPanel.CacheLengthUnit="Item"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <Border BorderBrush="LightBlue" BorderThickness="2" Margin="5"> <Grid> <Grid.Resources> <Style TargetType="TextBlock"> <Setter Property="FontSize" Value="30"/> </Style> </Grid.Resources> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition Width="Auto"/> <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="0" Grid.Column="2"/> <TextBlock Text="{Binding Author}" Grid.Row="0" Grid.Column="3"/> <TextBlock Text="{Binding Comment}" Grid.Row="1" Grid.Column="0"/> <TextBlock Text="{Binding Content}" Grid.Row="1" Grid.Column="1"/> <TextBlock Text="{Binding Summary}" Grid.Row="1" Grid.Column="2"/> <TextBlock Text="{Binding Topic}" Grid.Row="1" Grid.Column="3"/> </Grid> </Border> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>
Install-Package Communitytoolkit.mvvm
Install-Package Newtonsoft.json
<Window x:Class="WpfApp14.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:WpfApp14" mc:Ignorable="d" Title="{Binding MainTitle}" WindowState="Maximized"> <Window.DataContext> <local:MainVM/> </Window.DataContext> <Grid> <ItemsControl ItemsSource="{Binding BooksCollection}" ScrollViewer.CanContentScroll="True"> <ItemsControl.Template> <ControlTemplate TargetType="ItemsControl"> <ScrollViewer CanContentScroll="True"> <ItemsPresenter/> </ScrollViewer> </ControlTemplate> </ItemsControl.Template> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel IsItemsHost="True" VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling" VirtualizingPanel.CacheLength="5,5" VirtualizingPanel.CacheLengthUnit="Item"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <Border BorderBrush="LightBlue" BorderThickness="2" Margin="5"> <Grid> <Grid.Resources> <Style TargetType="TextBlock"> <Setter Property="FontSize" Value="30"/> </Style> </Grid.Resources> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition Width="Auto"/> <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="0" Grid.Column="2"/> <TextBlock Text="{Binding Author}" Grid.Row="0" Grid.Column="3"/> <TextBlock Text="{Binding Comment}" Grid.Row="1" Grid.Column="0"/> <TextBlock Text="{Binding Content}" Grid.Row="1" Grid.Column="1"/> <TextBlock Text="{Binding Summary}" Grid.Row="1" Grid.Column="2"/> <TextBlock Text="{Binding Topic}" Grid.Row="1" Grid.Column="3"/> </Grid> </Border> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </Grid> </Window> using CommunityToolkit.Mvvm.ComponentModel; using Newtonsoft.Json; using System.Collections.ObjectModel; using System.ComponentModel; using System.Net.Http; using System.Runtime.Serialization; 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 WpfApp14 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } } [ObservableObject] public partial class MainVM { string originUrl = "http://localhost:64769/BookService.svc/getbooks?cnt="; static HttpClient client = new HttpClient(); public MainVM() { if(!DesignerProperties.GetIsInDesignMode(new DependencyObject())) { _ = InitBooksCollectionAsync(1000000); } } private async Task InitBooksCollectionAsync(int cnt = 1000000) { MainTitle = $"{DateTime.Now},begining loading {cnt} items"; string url = $"{originUrl}{cnt}"; await Task.Run(async() => { string jsonStr = await client.GetStringAsync(url); List<Book>? tempBks = JsonConvert.DeserializeObject<List<Book>>(jsonStr); if (tempBks != null && tempBks.Any()) { Application.Current?.Dispatcher?.InvokeAsync(() => { MainTitle = $"{DateTime.Now},downloaded {cnt} items"; BooksCollection = new ObservableCollection<Book>(tempBks); MainTitle = $"{DateTime.Now},loaded {BooksCollection.Count} items," + $"First Id:{BooksCollection.FirstOrDefault()?.Id}," + $"Last Id:{BooksCollection.LastOrDefault()?.Id}"; }, System.Windows.Threading.DispatcherPriority.Background); } }); } [ObservableProperty] private ObservableCollection<Book> booksCollection; [ObservableProperty] private string mainTitle = $"{DateTime.Now},loading..."; } [DataContract] public class Book { [DataMember] public long Id { get; set; } [DataMember] public string Name { get; set; } [DataMember] public string ISBN { get; set; } [DataMember] public string Author { get; set; } [DataMember] public string Comment { get; set; } [DataMember] public string Content { get; set; } [DataMember] public string Summary { get; set; } [DataMember] public string Topic { get; set; } } }
//WCF
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Web; using System.Text; namespace WcfService7 { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IBookService" in both code and config file together. [ServiceContract] public interface IBookService { [OperationContract] [WebGet(UriTemplate = "/getbooks?cnt={cnt}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] List<Book> GetBooks(long cnt); } [DataContract] public class Book { [DataMember] public long Id { get; set; } [DataMember] public string Name { get; set; } [DataMember] public string ISBN { get; set; } [DataMember] public string Author { get; set; } [DataMember] public string Comment { get; set; } [DataMember] public string Content { get; set; } [DataMember] public string Summary { get; set; } [DataMember] public string Topic { get; set; } } } using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; using System.Threading; namespace WcfService7 { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "BookService" in code, svc and config file together. // NOTE: In order to launch WCF Test Client for testing this service, please select BookService.svc or BookService.svc.cs at the Solution Explorer and start debugging. public class BookService : IBookService { private static long id = 1; private (long, long) GetStartEnd(long interval) { long end = Interlocked.Add(ref id,interval); long start = end - interval; return (start, end); } public List<Book> GetBooks(long interval) { var (start, end) = GetStartEnd(interval); List<Book> bksList = new List<Book>(); for (long i = start; i <= end; i++) { bksList.Add(new Book() { Id = i, Name = $"Name_{i}", ISBN = $"ISBN_{i}_{Guid.NewGuid():N}", Author = $"Author_{i}", Comment = $"Comment_{i}", Content = $"Content_{i}", Summary = $"Summary_{i}", Topic = $"Topic_{i}" }); } return bksList; } } } <?xml version="1.0"?> <configuration> <appSettings> <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> </appSettings> <system.web> <compilation debug="true" targetFramework="4.8" /> <httpRuntime targetFramework="4.8"/> </system.web> <system.serviceModel> <bindings> <webHttpBinding> <binding name="BookServiceWebHttpBinding" openTimeout="01:00:00" closeTimeout="01:00:00" sendTimeout="01:00:00" receiveTimeout="01:00:00" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" transferMode="Buffered"> <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/> <security mode="None"/> </binding> </webHttpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="BookServiceBehavior"> <!-- To avoid disclosing metadata information, set the values below to false before deployment --> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="BookServiceEndPointBehavior"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> <protocolMapping> <add binding="basicHttpsBinding" scheme="https" /> </protocolMapping> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> <services> <service name="WcfService7.BookService" behaviorConfiguration="BookServiceBehavior"> <endpoint address="" binding="webHttpBinding" contract="WcfService7.IBookService" bindingConfiguration="BookServiceWebHttpBinding" behaviorConfiguration="BookServiceEndPointBehavior"/> </service> </services> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> <!-- To browse web app root directory during debugging, set the value below to true. Set to false before deployment to avoid disclosing web app folder information. --> <directoryBrowse enabled="true"/> </system.webServer> </configuration>


此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。