界面控件DevExpress WPF Scheduler控件 - 如何实现数据的按需加载? DevExpress WPF拥有120个控件和库将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpress WPF能创建有着强大互动功能的XAML基础应用程序这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。 无论是Office办公软件的衍伸产品还是以数据为中心的商业智能产品都能通过DevExpress WPF控件来实现。DevExpress新旧版本帮助文档下载可进QQ qun获取169725316在v20.2版本中技术团队为WPF Scheduler控件添加了按需数据加载支持本文将为大家介绍如何使用WPF Scheduler的API在WPF应用程序中配置数据加载逻辑。性能影响按需从源中获取数据是一种常用技术可以缩短具有大型数据集的应用程序的启动时间。 为了帮助您估算收益我们测量了 Scheduler 控件加载计划所需的时间这些测量没有考虑从源加载数据项所花费的时间对于速度较慢的数据库和服务仅加载部分数据的性能提升会更高。按需加载入门指南要启用按需数据加载您必须处理 DataSource.FetchAppointments 事件时间区域有一个单独的 DataSource.FetchTimeRegions 事件。 当您处理这些事件时DevExpress WPF Scheduler仅在其当前可见的时间间隔内加载数据当用户从一个时间段导航到另一个时间段时我们的WPF Scheduler会从数据源中查询更多信息根据需要缓存加载的数据以减少查询频率。为了进一步降低查询频率WPF Scheduler将加载数据的时间间隔扩展到 DataSource.FetchRange 属性值默认FetchRange为1个月。旦用户滚动超出此范围Scheduler只会重新计算此时间间隔实现以下代码示例演示了如何为DbContext数据源引入按需数据加载// Data context public class SchedulingDataContext : DbContext { public SchedulingDataContext() : base(CreateConnection(), true) { } static DbConnection CreateConnection() { //... } // Data items public DbSetAppointmentEntity AppointmentEntities { get; set; } //... } // Event implementation void dataSource_FetchAppointments(object sender, DevExpress.Xpf.Scheduling.FetchDataEventArgs e) { // Pass data objects to the events Result property e.Result dbContext.AppointmentEntities.Where( // The search query. // Use the items QueryStart and QueryEnd properties to calculate the correct interval // as they take recurrence patterns into account. // The events Interval property returns the time interval for which to load data objects. // Its value is the SchedulerControl.VisibleIntervals extended to the DataSource.FetchRange property value in both directions. x x.QueryStart e.Interval.End x.QueryEnd e.Interval.Start) .ToArray(); }此代码段中的 Where 方法的参数是一个基本的搜索查询不涉及过滤您可以改用 FetchDataEventArgs.GetFetchExpression 方法来简化实现void FetchAppointments(FetchDataEventArgs e) { e.Result dbContext.AppointmentEntities .Where(e.GetFetchExpressionAppointmentEntity()).ToArray(); }同步Scheduler和源要同步数据源和Scheduler部分加载数据您需要手动保存更改内置的 CRUD 事件简化了这一要求可以为所有四个事件编写一个处理程序dxsch:SchedulerControl AppointmentAddedProcessChanges AppointmentEditedProcessChanges AppointmentRemovedProcessChanges AppointmentRestoredProcessChanges/ void ProcessChanges(object sender, AppointmentCRUDEventArgs e) { db.Appointments.AddRange(e.AddToSource.Select(x (Appointment)x.SourceObject)); db.Appointments.RemoveRange(e.DeleteFromSource.Select(x (Appointment)x.SourceObject)); db.SaveChanges(); }如果您想了解有关按需数据加载的更多信息并且您的计算机上安装了​​​​​​​DevExpress WPF组件请单击下面的链接以启动我们的示例演示数据按需加载演示dxdemo://Wpf/DXScheduling/MainDemo/OnDemandDataLoading