Xamarin的Telerik UI?下载30天免费试用

按需负载

按需加载特性允许你延迟RadTreeView的填充,只在它们被请求时加载子项,从而节省计算资源,提高应用程序的初始性能。

按需加载机制的实现方式是这样的:一旦终端用户试图展开一个项,树视图就会收到一个通知,即请求按需加载操作,并为相应的项显示一个繁忙指示器。当加载请求的项,并且操作被标记为已完成时,忙碌指示器将被隐藏,并且这些项将在TreeView中可视化。

为了启用随需加载特性,您将需要利用LoadOnDemand命令,如下例所示:

首先,让我们创建一个样本Category类,它表示TreeView的根项,并在它的Children属性中保存它的子项:

类别:NotifyPropertyChangedBase{公共字符串名称{获取;设置;} ObservableCollection children;public ObservableCollection Children {get{返回this.children;} set {if (this。= value) {this。Children = value;this.OnPropertyChanged ();} } } }

然后,创建一个包含TreeView根项的示例Source属性的ViewModel。您还需要创建一个Command来处理随需应变的负载场景。

公共类ViewModel: NotifyPropertyChangedBase {public ObservableCollection Source {get;设置;} public ICommand LoadOnDemandCommand {get;设置;} public ViewModel() {this。Source = new ObservableCollection(){新Category {Name = "Products"},新Category {Name = "Purchase"},新Category {Name = "Support"},新Category {Name = "Community"}};这一点。LoadOnDemandCommand = new Command(async (p) => await this.LoadOnDemandExecute(p), (p) => this.IsLoadOnDemandEnabled(p));} private bool IsLoadOnDemandEnabled(对象p) {var context = (TreeViewLoadOnDemandCommandContext)p;返回上下文。Item is Category; } async private Task LoadOnDemandExecute(object p) { var context = (TreeViewLoadOnDemandCommandContext)p; var category = context.Item as Category; if (category != null) { ObservableCollection children = await Task.Run(() => this.LoadChildren(category)); category.Children = children; context.Finish(); } } private ObservableCollection LoadChildren(Category category) { Task.Delay(1000).Wait(); Dictionary> allItems = new Dictionary>(); allItems.Add("Products", new ObservableCollection() { "Telerik UI for Xamarin", "Telerik UI for WPF", "Telerik Reporting" }); allItems.Add("Purchase", new ObservableCollection() { "Buy now", "License Agreement", "Policies" }); allItems.Add("Support", new ObservableCollection() { "Support Center", "Knowledge Base", "Demos", "Tutorials" }); allItems.Add("Community", new ObservableCollection() { "Learning Resources", "Blogs", "Forums" }); var result = new ObservableCollection(); bool hasChildren = allItems.TryGetValue(category.Name, out result); return result; }

在初始化每个项时调用IsLoadOnDemandEnabled方法,并定义是否为该项启用按需加载特性。

最后,用需要的TreeViewDescriptors和LoadOnDemandCommand定义RadTreeView:

      

剩下的就是将BindingContext设置为ViewModel:

这一点。BindingContext = new ViewModel();

在哪里telerikTreeView命名空间如下:

xmlns: telerikTreeView = " clr-namespace: Telerik.XamarinForms.DataControls.TreeView;装配= Telerik.XamarinForms.DataControls”

下面是请求load- demand时TreeView的样子:

TreeView LoadOnDemand

这里的示例只演示了一级层次结构,但是,在实际场景中,您可以毫无问题地使用带有惰性加载的多级层次结构。

中检查可运行的演示特性部份RadTreeView组件中的浏览器应用程序(可以在您本地的Examples文件夹中找到Xamarin的Telerik UI安装)

另请参阅

在本文中
Baidu
map