silverlight 服务端分页

silverlight 分页服务端分页就是在服务里面写分页的方法,silverlight 调用的时候传参数过来就行了。先看服务代码!

我这边使用的是wcf服务!类图如下:

 

 

服务写了两个方法,PageCount这个方法是获取数据总条数,PageFilter用于分页。该方法接受三个参数,PageSize、PageIndex、Filter,当然分页的前两个参数是必不可少的,其他的参数可以根据实际情况来定义;如果你要根据用户名来过滤的话,加个string UserName就行了;废话不多说。直接看该方法的代码。

 

PageFilter方法很简单,就通过LInq分下页,你也可以通过其他方式分页,调用存储过程等!这个服务写好了。现在要做的就是silverlight 程序里面引用这个服务;

现在来看silverlight页面的代码。

 

复制代码
View Code
<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="Silverlight.MainPage"
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"
mc:Ignorable
="d"
d:DesignHeight
="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White">
<StackPanel>
<sdk:DataGrid x:Name="dataGrid" Height="200"/>
<sdk:DataPager x:Name="pager1" PageSize="2" PageIndexChanged="pager1_PageIndexChanged"></sdk:DataPager>
</StackPanel>
</Grid>
</UserControl>
复制代码

这个页面就放了一个DataGrid和DataPager;简单说下这个分页控件,点击分页的时候他会触发PageIndexChanged事件,还有在设置分页控件的Source属性的时候他也会触发PageIndexChanged事件!

我的做法就是当服务器返回的数据总数和分页控件的数据总数不一致的时候在从新设置分页控件的Source;贴代码!

复制代码
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Silverlight.Test.PageService;
using System.Windows.Data;

namespace Silverlight
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
Filter();
}


private void pager1_PageIndexChanged(object sender, EventArgs e)
{

Filter();
}

private void Filter()
{

WebPageServiceClient client = new WebPageServiceClient();
client.PageCountCompleted += new EventHandler<PageCountCompletedEventArgs>(client_PageCountCompleted);
client.PageCountAsync(string.Empty);
}

void client_PageCountCompleted(object sender, PageCountCompletedEventArgs e)
{
if (e.Result < 1)
return;
SetPageSource(e.Result);

var pageIndex = pager1.PageIndex + 1;
WebPageServiceClient client = sender as WebPageServiceClient;
client.PageFilterCompleted += new EventHandler<PageFilterCompletedEventArgs>(client_PageFilterCompleted);
client.PageFilterAsync(pager1.PageSize, pageIndex, string.Empty);
}


void client_PageFilterCompleted(object sender, PageFilterCompletedEventArgs e)
{
dataGrid.ItemsSource = e.Result;
}

private void SetPageSource(int resultCount)
{
if (pager1.Source != null && (pager1.Source as PagedCollectionView).TotalItemCount == resultCount)
return;
List<int> source = new List<int>();
for (int i = 0; i < resultCount; i++)
{
source.Add(i);
}

PagedCollectionView pagedCollection = new PagedCollectionView(source);
pager1.Source = pagedCollection;
}
}
}
复制代码

服务端分页差不多完成!还有其他的方式大家拿出来分享下!

 

 

 

 

 

代码下载

posted @   _hqh  阅读(1170)  评论(7编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示