用SilverLight构建插件应用(六)在
终极管理员 知识笔记 43阅读
用SilverLight构建插件应用(VI)开发留言板插件——插件消息的显示:留言板是整个系统中第一个可以与用户交互的插件,插件实现了登录用户消息和从数据库中获取用户消息的功能。先看服务器提供的数据函数://summary//获取消息总数//返回消息数/returns[operation contract]public int get notes count(){ GBA(43,145,175,1)' notes blll=new notes bll();返回bll。getnotes count();}//summary///获取指定页码sum size///summary//param name=' page number ' 页码/param//param name=' pagesize '/param//returns的消息。
言</returns>
[OperationContract]
public List<NotesInfo> GetNotesByPage(int pageNumber,int pageSize)

{
NotesBLL bll = new NotesBLL();
return bll.GetNotesByPage(pageNumber,pageSize);
}
下面看看客户端的代码:
整个客户单和显示有关的有3个对象;
NotesItem:显示的留言项;
留言项基本就是一个XAML的界面,每条留言需要显示的内容。
NotesPage:显示的留言页;
容纳留言项的界面,没有更多的代码。
NotesMain.xaml:显示的留言主界面;
这是整个插件的核心部分,实现了读取数据,生成数据项,显示,翻页等等功能。
1:实现IPlugIn接口,这个是每个插件都要实现的功能;
2:获取整个留言的页数,为分页显示最准备,下面是代码:
private void DataCount()
{
Uri uri = System.Windows.Browser.HtmlPage.Document.DocumentUri;
string host = uri.AbsoluteUri;
host = host.Substring(0, host.Length - uri.LocalPath.Length);
string servicePath = "/Services/WSNotes.svc";
string serviceUri = host + servicePath;
//
WSNotesClient ws = new WSNotesClient(new System.ServiceModel.BasicHttpBinding(), new System.ServiceModel.EndpointAddress(serviceUri));
ws.GetNotesCountCompleted += (o, ev) =>
{
if (ev.Error == null)
{
int tmpCount = ev.Result / PageSize;
if (tmpCount * PageSize == ev.Result)
{
PageCount = tmpCount;
}
else
{
PageCount = tmpCount + 1;
}
this.txtPageCount.Text = "/" + PageCount;
//
DataLoad(1);
}
};
ws.GetNotesCountAsync();
}
3:显示指定页码的内容:
//
Uri uri = System.Windows.Browser.HtmlPage.Document.DocumentUri;
string host = uri.AbsoluteUri;
host = host.Substring(0, host.Length - uri.LocalPath.Length);
string servicePath = "/Services/WSNotes.svc";
string serviceUri = host + servicePath;
//
WSNotesClient ws = new WSNotesClient(new System.ServiceModel.BasicHttpBinding(), new System.ServiceModel.EndpointAddress(serviceUri));
ws.GetNotesByPageCompleted += (o, e) =>
{
if (e.Error == null)
{
ObservableCollection<NotesInfo> notesInfo = e.Result;
int itemCount = notesInfo.Count;
NotesPage page = new NotesPage();
for (int iItem = 0; iItem < itemCount; iItem++)
{
NotesItem item = new NotesItem();
Thickness thicknessi = new Thickness();
thicknessi.Top = 5;
thicknessi.Bottom = 5;
item.Margin = thicknessi;
item.notesTitle.Text = notesInfo[iItem].Title;
item.notesText.Text = notesInfo[iItem].Content;
item.AuthorName.Text = notesInfo[iItem].UserName;
item.AuthorImage.Source = ResourceHelper.GetBitmap("Resources/Images/self.png", this.GetType().Namespace);
page.panelPageItem.Children.Add(item);
}
Thickness thickness = new Thickness();
thickness.Top = 10;
thickness.Bottom = 10;
page.Margin = thickness;
page.PageFooter.Text = pageNumber.ToString();
this.txtPageNumber.Text = pageNumber.ToString();
this.panelPages.Children.Clear();
this.panelPages.Children.Add(page);
}
//如果在xaml界面直接使用这两个属性,程序运行就会报错
//在这个地方设定没有错误
ScrollPages.SetValue(Grid.RowProperty, 1);
ScrollPages.SetValue(Grid.ColumnProperty, 1);
};
ws.GetNotesByPageAsync(pageNumber,PageSize);
4:控制翻页的状态
string tag = ((Button)sender).Tag.ToString();
if (tag == "First")
{
PageNumber = 1;
}
else if (tag == "Prview")
{
PageNumber = PageNumber - 1; ;
}
else if (tag == "Next")
{
PageNumber = PageNumber + 1; ;
}
else if (tag == "Last")
{
PageNumber = PageCount;
}
else if (tag == "Goto")
{
int tmp=PageNumber;
if (int.TryParse(this.txtPageNumber.Text, out tmp))
{
PageNumber = tmp;
}
else
{
MessageBox.Show("你输入的页码不是整数,请重新输入","提示",MessageBoxButton.OK);
}
}
DataLoad(PageNumber);
至此,整个留言显示完成,如果用户需要留言,请先注册登陆。下一个Blog写如何填写留言。
代码正在整理。