轉(zhuǎn)帖|其它|編輯:郝浩|2010-12-09 11:35:38.000|閱讀 985 次
概述:在Windows或者ASP.NET Web應(yīng)用程序中,我們經(jīng)常可以看到在Grid控件上通過Load-on-demand的方式來提高系統(tǒng)性能,提升用戶體驗。所謂Load-on-demand就是在最初表格數(shù)據(jù)加載時只加載當前表格中用戶可以看到的行數(shù),當用戶向下滾動或拖拽縱向滾動條時,再將需要顯示的數(shù)據(jù)通過某種方式動態(tài)加載進來。 本文主要介紹Silverlight DataGrid使用WCF RIA Service實現(xiàn)Load-on-demand的數(shù)據(jù)加載,希望對大家有幫助。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
在Windows或者ASP.NET Web應(yīng)用程序中,我們經(jīng)常可以看到在Grid控件上通過Load-on-demand的方式來提高系統(tǒng)性能,提升用戶體驗。
所謂Load-on-demand就是在最初表格數(shù)據(jù)加載時只加載當前表格中用戶可以看到的行數(shù),當用戶向下滾動或拖拽縱向滾動條時,再將需要顯示的數(shù)據(jù)通過某種方式動態(tài)加載進來。
那么對于Silverlight,我們可以使用DataGrid通過WCF RIA Service來實現(xiàn)這個功能。
1. WCF RIA Service
我們將會使用WCF Service來提供數(shù)據(jù),并且將這個WCF Service host到ASP.Net應(yīng)用程序中。
- 定義數(shù)據(jù)對象
[DataContract]
public class Employee
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Department { get; set; }
[DataMember]
public double Salary { get; set; }
}
使用DataContract和DataMember來標識數(shù)據(jù)對象以及對象屬性,這樣就可以通過WCF Service來傳遞這個數(shù)據(jù)結(jié)構(gòu)了,注意需要添加System.Runtime.Serialization.dll。
添加Silverlight enabled WCF Service
在WebApplciation工程中添加一個新的Item,選取"Silverlight enabled WCF Service",命名為"EmployeeService.svn".
在PepoleService.svn.cs中添加如下代碼:
[OperationContract]
public List<Employee> GetEmployeeData(int startRow, int endRow)
{
List<Employee> employees = new List<Employee>();
for (int i = startRow; i < endRow; i++)
{
employees.Add(new Employee()
{
ID = i,
Name = string.Format("Name {0}", i),
Department = string.Format("Department {0}", i),
Salary = i * 100.0
});
}
return employees;
}
注意在上面一步添加完WCF Service后,會在Web.config文件中添加關(guān)于Service的配置信息:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="Silverlight.Web.EmployeeServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<services>
<service behaviorConfiguration="Silverlight.Web.EmployeeServiceBehavior" name="Silverlight.Web.EmployeeService">
<endpoint address="" binding="basicHttpBinding" contract="Silverlight.Web.EmployeeService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
2. Load-on-demand數(shù)據(jù)加載
在Silverlight工程中添加Service引用
如下圖通過給Silverlight工程添加Service reference來操作WCF Service:
從WCFService中獲取數(shù)據(jù)
將WCF Service引入后,IDE會自動生成EmployeeServiceClient類,通過這個代理我們就可以使用Service上的方法了。
通過如下代碼可以從WCF Service獲得Employee數(shù)據(jù):
public partial class Page : UserControl
{
private ObservableCollection<Employee> _employees;
private void GetData(int startRow, int endRow)
{
EmployeeServiceClient proxy = new EmployeeServiceClient();
proxy.GetEmployeeDataCompleted += new EventHandler<GetEmployeeDataCompletedEventArgs>(proxy_GetDataCompleted);
proxy.GetEmployeeDataAsync(startRow, endRow);
}
private void proxy_GetDataCompleted(object sender, GetEmployeeDataCompletedEventArgs e)
{
foreach (Employee employee in e.Result)
{
this._employees.Add(employee);
}
}
}
在DataGrid上實現(xiàn)數(shù)據(jù)的Load-on-demand
Silverlight DataGrid提供了一個事件:LoadingRow,該事件會在某一個Row第一次被顯示的時候被觸發(fā)。通過這個事件我們就可以實現(xiàn)數(shù)據(jù)的按需加載,在這個事件中我們可以拿到該Row的RowIndex,如果發(fā)現(xiàn)當前將要顯示的Row已經(jīng)接近末尾(當前定義為距離末尾小于5),那么就需要向服務(wù)器端請求數(shù)據(jù)。
示例代碼:
private void Page_Loaded(object sender, RoutedEventArgs e)
{
this._startRowIndex = 0;
this._employees = new ObservableCollection<Employee>();
this.peopleDataGrid.ItemsSource = _employees;
GetData(this._startRowIndex, this._pageSize);
}
private void peopleDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
if (this._isLoading || this._employees.Count < _pageSize)
{
return;
}
if (this._employees.Count - 5 < e.Row.GetIndex())
{
this.GetData(this._startRowIndex, this._startRowIndex + this._pageSize);
}
}
運行程序,拖動ScrollBar到底部,你會發(fā)現(xiàn)DataGrid會自動加載數(shù)據(jù)。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:博客轉(zhuǎn)載