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 == "p_w_picpath"
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 == "p_w_picpath"
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 == "p_w_picpath"
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.gif public 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        }


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