WindowsPhone7开发简单豆瓣网应用程序之主页面功能实现

本文涉及的产品
全局流量管理 GTM,标准版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
云解析 DNS,旗舰版 1个月
简介:
WindowsPhone7开发简单豆瓣网应用程序之主页面功能实现
   在上一篇博文当中介绍了豆瓣应用程序的界面设计,那么这些界面是如何实现功能呢?下面我讲代码分享给大家。
    主页面图:
 
大家可以看到主界面我们需要实现三种功能的搜索(搜书,搜乐,搜影)。由于这三种搜索的后台实现代码雷同,这里我以搜书为例。
1) 首先我们需要实例化WebClient对象,这里由于三种类型的搜索调用WebClient对象方法基本上一致,所有我把这些封装到一个通用类当中(MyWebClient.cs)。MyWebClient.cs中代码如下:
 WebClient client =  new WebClient();


                 public  delegate  bool MyWebClientDe( string xmlFile);

                MyWebClientDe _myDelegete;
                 /// <summary>
                 /// 回调函数设置获得返回的字符串
                 /// </summary>
                 /// 
                 public  bool IsBusy()
                {
                         return client.IsBusy;
                }
                 public MyWebClientDe myDelegete
                {
                        get {  return _myDelegete; }
                        set { _myDelegete = value; }
                }

                 public MyWebClient()
                {
                        client.Encoding = Encoding.UTF8;

                        client.DownloadStringCompleted +=  new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);

                }

                 void client_DownloadStringCompleted( object sender, DownloadStringCompletedEventArgs e)
                {
                         if (myDelegete !=  null)
                        {
                                 if (e.Error ==  null)
                                {
                                        myDelegete(e.Result);
                                }
                                 else
                                {
                                        MessageBox.Show(e.Error.Message.ToString());
                                }
                        }
                         else
                                MessageBox.Show( "未指定代理函数!");

                }

                 public  bool DownloadStringAsync( string Api)
                {
                     
                         try
                        {
                                 if (!client.IsBusy)
                                {
                                        client.DownloadStringAsync( new Uri(Api));

                                         return  true;
                                }
                                 else
                                {
                                         return  false;
                                }
                        }
                         catch
                        {
                                 return  false;
                        }


                }


2) 随后我们需要在MainPage.xaml.cs中添加如下代码:
 
绑定书籍信息
 
    鼠标点击事件
 
根据选择某一项进行跳转并传递id值。
3) 在MainPage.xaml.cs中还需要调用:DoubanDAL.cs;DouBanBook.cs及Navigation.cs。
 
4) 在DoubanDAL.cs中我们封装了搜索书籍,音乐,视频的通用属性信息搜索方法。代码如下:
MyWebClient myclinet =  new MyWebClient();

                 public List<DouBanBook> GetBook( string xmlFile)
                {
                         try
                        {
                                 string ns1 =  "{http://www.w3.org/2005/Atom}";
                                var xml1 = XDocument.Parse(xmlFile);
                                var slist = from one in xml1.Descendants(ns1 + "entry")
                                                        select new DouBanBook()
                                                        {
                                                                Titile = one.Element(ns1 + "title").Value,
                                                                Images = (from cone in one.Elements(ns1 + "link")
                                                                                    where cone.Attribute("rel").Value == "image"
                                                                                    select cone.Attribute("href").Value).First<string>(),
                                                                Id = one.Element(ns1 + "id").Value
                                                        };

                                return slist.ToList<DouBanBook>();

                        }
                        catch
                        {
                                return null;
                        }
                }

                public List<DouBanMusic> GetMusic(string xmlFile)
                {
                        try
                        {
解析查询出来的xml文件#region 解析查询出来的xml文件
                                string ns1 = "{http://www.w3.org/2005/Atom}";
                                var xml1 = XDocument.Parse(xmlFile);
                                //MessageBox.Show(xml1.ToString());

                         
                                                     


                                var slist = from one in xml1.Descendants(ns1 + "entry")
                                                        select new DouBanMusic()
                                                        {
                                                                Titile = one.Element(ns1 + "title").Value,
                                                                Images = (from cone in one.Elements(ns1 + "link")
                                                                                    where cone.Attribute("rel").Value == "image"
                                                                                    select cone.Attribute("href").Value).First<string>(),
                                                                Id = one.Element(ns1 + "id").Value
                                                        };
                                #endregion
                                return slist.ToList<DouBanMusic>();
                        }
                        catch 
                        {
                                return null;
                        }
                }

                public List<DouBanVideo> GetVideo(string xmlFile)
                {
                        try
                        {
解析查询出来的xml文件#region 解析查询出来的xml文件
                                string ns1 = "{http://www.w3.org/2005/Atom}";
                                var xml1 = XDocument.Parse(xmlFile);
                                var slist = from one in xml1.Descendants(ns1 + "entry")
                                                        select new DouBanVideo()
                                                        {
                                                                Titile = one.Element(ns1 + "title").Value,
                                                                Images = (from cone in one.Elements(ns1 + "link")
                                                                                    where cone.Attribute("rel").Value == "image"
                                                                                    select cone.Attribute("href").Value).First<string>(),
                                                                Id = one.Element(ns1 + "id").Value
                                                        };
                                #endregion
                             return    slist.ToList<DouBanVideo>();
                        }

                        catch
                        {
                                return null;
                        }
                }


5) 在DouBanBook.cs中封装了我们需要查询的一些书籍信息的属性。代码如下:
//图片路径
                 public  string Images { get; set; }
                 //标题
                 public  string Titile { get; set; }
                 //ID
                 public  string Id { get; set; }
                 //作者
                 public  string author { get; set; }
                 //简介
                 public  string suammary { get; set; }
                 //价格
                 public  string price { get; set; }
                 //出版人
                 public  string publisher { get; set; }

                 public  string authorInfo { get; set; }


6) 在Navigation.cs中我们利用枚举实现页面跳转。代码如下:
public  enum ApplicationPages
        {
                Book,
                Music,
                Video
        }
         public  static  class Navigation
        {
                 public  static  void GoToPage( this PhoneApplicationPage phoneApplicationPage, ApplicationPages applicationPage, string Id)
                {
                         switch (applicationPage)
                        { 
                                 case ApplicationPages.Book:
                                        phoneApplicationPage.NavigationService.Navigate( new Uri( "/Views/BookPage.xaml?id="+Id ,UriKind.Relative));
                                         break;
                                 case ApplicationPages.Music:
                                        phoneApplicationPage.NavigationService.Navigate( new Uri( "/Views/MusicPage.xaml?id=" + Id, UriKind.Relative));
                                         break;
                                 case ApplicationPages.Video:
                                        phoneApplicationPage.NavigationService.Navigate( new Uri( "/Views/VideoPage.xaml?id=" + Id, UriKind.Relative));
                                         break;                             

                        }
                }
        }


这样我们就实现了主页面的搜索及跳转功能。




 本文转自 王祖康 51CTO博客,原文链接:http://blog.51cto.com/wzk89/582045 ,如需转载请自行联系原作者
相关文章
|
4月前
|
前端开发
HTML+CSS基础知识(4)简单的广告界面
这篇文章通过代码示例演示了如何使用CSS样式来美化网页布局,包括清除默认样式、设置页面字体、调整标题和内容的样式,以及为超链接添加悬停效果。
HTML+CSS基础知识(4)简单的广告界面
|
7月前
|
数据库
一款挺不错网站维护页面源码
一款挺不错网站维护页面源码,单HTML不需要数据库,上传到你的虚拟机就可以用做维护页面还不错,用处多。。
53 2
一款挺不错网站维护页面源码
|
Web App开发 搜索推荐 NoSQL
如何搭建一个集成导航与在线工具的个性化浏览器私有书签(附详细搭建教程)
在这个信息爆炸的时代,我们都希望拥有一个能够轻松解决多端、多浏览器的收藏和笔记同步问题的神奇工具。Mtab书签正是为此而设计的顶级应用。它将基础导航、记事本、在线小工具和多端同步集于一身,为用户提供了更便利的网络浏览体验,并解决了多端同步的烦恼。
270 0
如何搭建一个集成导航与在线工具的个性化浏览器私有书签(附详细搭建教程)
|
缓存 小程序 前端开发
【易售小程序项目】请求包创建+登录功能实现【基于若依管理系统开发】
【易售小程序项目】请求包创建+登录功能实现【基于若依管理系统开发】
144 0
|
前端开发
前端代码简洁之路,后台系统之详情页设计
前端业务开发中,为了脱离舒适区,也为了解放重复功能开发的劳动力,会将一些功能进行改造,本期改造千篇一律的详情页。
4014 26
前端代码简洁之路,后台系统之详情页设计
|
存储 小程序 JavaScript
借助云开发实现小程序朋友圈的发布与展示
借助云开发实现小程序朋友圈的发布与展示
125 0
|
小程序
视频号主页,实现一键添加个人微信功能,留客更方便,真香
在视频号主页放置添加微信的按钮,其实微信已经支持了,只不过只支持企业微信,不支持个人微信,那怎么办,只能自己实现了。
458 0
视频号主页,实现一键添加个人微信功能,留客更方便,真香
|
内存技术
简单系统后台页面开发分享【2020网页综合笔记01】
简单系统后台页面开发分享【2020网页综合笔记01】
117 0
简单系统后台页面开发分享【2020网页综合笔记01】
|
前端开发 JavaScript 测试技术
接口测试平台代码实现27: 项目详情页的导航功能
接口测试平台代码实现27: 项目详情页的导航功能
接口测试平台代码实现27: 项目详情页的导航功能
|
开发工具 Android开发
XQRCode 一个非常方便实用的二维码扫描、解析、生成库
XQRCode 一个非常方便实用的二维码扫描、解析、生成库
618 0
XQRCode 一个非常方便实用的二维码扫描、解析、生成库