使用SilverLight构建插件式应用程序(六)

使用SilverLight构建插件式应用程序(六)

留言板插件的开发-插件留言的显示:

留言板是整个系统第一个可以和用户交互的插件,插件实现了登录用户留言,系统从数据库获取的功能。

首先看服务器提供的数据功能:

/// <summary>

    /// 获取留言的总条数

    /// </summary>

    /// <returns>留言条数</returns>

    [OperationContract]

    public int GetNotesCount()

    {

        NotesBLL bll = new NotesBLL();

        return bll.GetNotesCount();

    }  

    /// <summary>

    /// 获取指定页码和大小的留言

    /// </summary>

    /// <param name="pageNumber">页码</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写如何填写留言。

演示地址:www.cuface.cn

代码正在整理。

原文地址:https://www.cnblogs.com/songsgroup/p/1309463.html