WindowsPhone7开发简单豆瓣网应用程序之主页面功能实现-阿里云开发者社区

开发者社区> 技术小胖子> 正文

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

简介:
+关注继续查看
WindowsPhone7开发简单豆瓣网应用程序之主页面功能实现
   在上一篇博文当中介绍了豆瓣应用程序的界面设计,那么这些界面是如何实现功能呢?下面我讲代码分享给大家。
    主页面图:
 2011060622201877.jpg
大家可以看到主界面我们需要实现三种功能的搜索(搜书,搜乐,搜影)。由于这三种搜索的后台实现代码雷同,这里我以搜书为例。
1) 首先我们需要实例化WebClient对象,这里由于三种类型的搜索调用WebClient对象方法基本上一致,所有我把这些封装到一个通用类当中(MyWebClient.cs)。MyWebClient.cs中代码如下:
InBlock.gif WebClient client = new WebClient();
InBlock.gif

InBlock.gif                public delegate bool MyWebClientDe(string xmlFile);
InBlock.gif
                MyWebClientDe _myDelegete;
InBlock.gif                /// <summary>
InBlock.gif                /// 回调函数设置获得返回的字符串
InBlock.gif                /// </summary>
InBlock.gif                /// 
InBlock.gif                public bool IsBusy()
InBlock.gif                {
InBlock.gif                        return client.IsBusy;
InBlock.gif                }
InBlock.gif                public MyWebClientDe myDelegete
InBlock.gif                {
InBlock.gif                        get { return _myDelegete; }
InBlock.gif                        set { _myDelegete = value; }
InBlock.gif                }
InBlock.gif
                public MyWebClient()
InBlock.gif                {
InBlock.gif                        client.Encoding = Encoding.UTF8;
InBlock.gif
                        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
InBlock.gif
                }
InBlock.gif
                void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
InBlock.gif                {
InBlock.gif                        if (myDelegete != null)
InBlock.gif                        {
InBlock.gif                                if (e.Error == null)
InBlock.gif                                {
InBlock.gif                                        myDelegete(e.Result);
InBlock.gif                                }
InBlock.gif                                else
InBlock.gif                                {
InBlock.gif                                        MessageBox.Show(e.Error.Message.ToString());
InBlock.gif                                }
InBlock.gif                        }
InBlock.gif                        else
InBlock.gif                                MessageBox.Show("未指定代理函数!");
InBlock.gif
                }
InBlock.gif
                public bool DownloadStringAsync(string Api)
InBlock.gif                {
InBlock.gif                     
InBlock.gif                        try
InBlock.gif                        {
InBlock.gif                                if (!client.IsBusy)
InBlock.gif                                {
InBlock.gif                                        client.DownloadStringAsync(new Uri(Api));
InBlock.gif
                                        return true;
InBlock.gif                                }
InBlock.gif                                else
InBlock.gif                                {
InBlock.gif                                        return false;
InBlock.gif                                }
InBlock.gif                        }
InBlock.gif                        catch
InBlock.gif                        {
InBlock.gif                                return false;
InBlock.gif                        }
InBlock.gif

InBlock.gif                }


2) 随后我们需要在MainPage.xaml.cs中添加如下代码:
 2011060622235435.jpg
绑定书籍信息
 2011060622261322.jpg
    鼠标点击事件
 2011060622263644.jpg
根据选择某一项进行跳转并传递id值。
3) 在MainPage.xaml.cs中还需要调用:DoubanDAL.cs;DouBanBook.cs及Navigation.cs。
 2011060622265671.jpg
4) 在DoubanDAL.cs中我们封装了搜索书籍,音乐,视频的通用属性信息搜索方法。代码如下:
InBlock.gifMyWebClient myclinet = new MyWebClient();
InBlock.gif
                public List<DouBanBook> GetBook(string xmlFile)
InBlock.gif                {
InBlock.gif                        try
InBlock.gif                        {
InBlock.gif                                string ns1 = "{http://www.w3.org/2005/Atom}";
InBlock.gif                                var xml1 = XDocument.Parse(xmlFile);
InBlock.gif                                var slist = from one in xml1.Descendants(ns1 + "entry")
InBlock.gif                                                        select new DouBanBook()
InBlock.gif                                                        {
InBlock.gif                                                                Titile = one.Element(ns1 + "title").Value,
InBlock.gif                                                                Images = (from cone in one.Elements(ns1 + "link")
InBlock.gif                                                                                    where cone.Attribute("rel").Value == "image"
InBlock.gif                                                                                    select cone.Attribute("href").Value).First<string>(),
InBlock.gif                                                                Id = one.Element(ns1 + "id").Value
InBlock.gif                                                        };
InBlock.gif
                                return slist.ToList<DouBanBook>();
InBlock.gif
                        }
InBlock.gif                        catch
InBlock.gif                        {
InBlock.gif                                return null;
InBlock.gif                        }
InBlock.gif                }
InBlock.gif
                public List<DouBanMusic> GetMusic(string xmlFile)
InBlock.gif                {
InBlock.gif                        try
InBlock.gif                        {
InBlock.gif解析查询出来的xml文件#region 解析查询出来的xml文件
InBlock.gif                                string ns1 = "{http://www.w3.org/2005/Atom}";
InBlock.gif                                var xml1 = XDocument.Parse(xmlFile);
InBlock.gif                                //MessageBox.Show(xml1.ToString());
InBlock.gif
                         
InBlock.gif                                                     
InBlock.gif

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


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


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


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




 本文转自 王祖康 51CTO博客,原文链接:http://blog.51cto.com/wzk89/582045,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
使用 Chrome 开发者工具的 lighthouse 功能分析 web 应用的性能问题
使用 Chrome 开发者工具的 lighthouse 功能分析 web 应用的性能问题
20 0
Windows Phone 8初学者开发—第3部分:编写第一个Windows Phone 8应用程序
原文 Windows Phone 8初学者开发—第3部分:编写第一个Windows Phone 8应用程序 原文地址: http://channel9.msdn.com/Series/Windows-Phone-8-Development-for-Absolute-Beginners/P...
920 0
21119
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载