博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WindowsPhone7开发简单豆瓣网应用程序之主页面功能实现
阅读量:5734 次
发布时间:2019-06-18

本文共 4250 字,大约阅读时间需要 14 分钟。

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.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        }
这样我们就实现了主页面的搜索及跳转功能。
 本文转自 王祖康 51CTO博客,原文链接:http://blog.51cto.com/wzk89/582045
,如需转载请自行联系原作者
你可能感兴趣的文章
Javascript String类的属性及方法
查看>>
vim编辑器如何添加或删除多行注释
查看>>
[LeetCode] Merge Intervals
查看>>
iOS开发-按钮的基本使用
查看>>
在QT和SDL搭建的框架中使用OPENGL在SDL窗口上进行绘图
查看>>
REST技术第三步 @BeanParam的使用
查看>>
模板 读入挂!
查看>>
SharePoint 读取 Site Columns 的数据并绑定到DropdownList
查看>>
Python中的对象行为与特殊方法(二)类型检查与抽象基类
查看>>
使用 axios 详解
查看>>
通信基站(dfs回溯,思维)
查看>>
nginx web加密访问
查看>>
iOS - Regex 正则表达式
查看>>
SYS_CONTEXT函数返回IP地址的一些误解
查看>>
第 68 章 Logical Volume Manager (LVM)
查看>>
膝盖中了一箭之康复篇-第八个月暨2月份目标总结
查看>>
IPA提交APPStore问题记录(一)
查看>>
有利于seo优化的网站地图不能取巧
查看>>
快照产品体验优化
查看>>
ASCII
查看>>