WindowsPhone7开发简单豆瓣网应用程序之主页面功能实现 推荐
2011-06-06 22:34
736 查看
WindowsPhone7开发简单豆瓣网应用程序之主页面功能实现 在上一篇博文当中介绍了豆瓣应用程序的界面设计,那么这些界面是如何实现功能呢?下面我讲代码分享给大家。 主页面图:
大家可以看到主界面我们需要实现三种功能的搜索(搜书,搜乐,搜影)。由于这三种搜索的后台实现代码雷同,这里我以搜书为例。[align=left]1) 首先我们需要实例化WebClient对象,这里由于三种类型的搜索调用WebClient对象方法基本上一致,所有我把这些封装到一个通用类当中(MyWebClient.cs)。MyWebClient.cs中代码如下:[/align]
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;
}
}
[align=left][/align]
[align=left]2) 随后我们需要在MainPage.xaml.cs中添加如下代码:[/align]
[align=left]绑定书籍信息[/align]
鼠标点击事件
根据选择某一项进行跳转并传递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;
}
}
}
这样我们就实现了主页面的搜索及跳转功能。
大家可以看到主界面我们需要实现三种功能的搜索(搜书,搜乐,搜影)。由于这三种搜索的后台实现代码雷同,这里我以搜书为例。[align=left]1) 首先我们需要实例化WebClient对象,这里由于三种类型的搜索调用WebClient对象方法基本上一致,所有我把这些封装到一个通用类当中(MyWebClient.cs)。MyWebClient.cs中代码如下:[/align]
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;
}
}
[align=left][/align]
[align=left]2) 随后我们需要在MainPage.xaml.cs中添加如下代码:[/align]
[align=left]绑定书籍信息[/align]
鼠标点击事件
根据选择某一项进行跳转并传递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;
}
}
}
这样我们就实现了主页面的搜索及跳转功能。
相关文章推荐
- WindowsPhone7开发简单豆瓣网应用程序之主页面功能实现
- WindowsPhone7开发简单豆瓣网应用程序之界面设计 推荐
- WindowsPhone7开发简单豆瓣网应用程序之界面设计
- WindowsPhone7开发简单豆瓣网应用程序之界面设计(续)
- iOS 用其他应用程序打开文件功能如何实现的iOS开发 - CocoaChina CocoaChina_让移动开发更简单
- ASP.NET jQuery 实例12 通过使用jQuery validation插件简单实现用户注册页面验证功能
- 【Android游戏开发十六】Android Gesture之【触摸屏手势识别】操作!利用触摸屏手势实现一个简单切换图片的功能!
- 我的博客网站开发3——博客首页功能实现之各排行版(阅读数,评论数,推荐数)的数据绑定
- Django开发的应用程序中使用admin自带小控件(以实现日历功能的AdminDateWidget为例)
- Android开发学习之闹钟功能的简单实现
- android简单实现页面滑动关闭功能
- cocos2dx游戏开发之利用多点触摸(实现缩放功能或者简单的手势识别)
- ASP.NET jQuery 实例12 通过使用jQuery validation插件简单实现用户注册页面验证功能
- EBS Form开发利用QUERY_FIND来实现最简单的查询功能(2)
- 用MVC开发模式实现servlet简单的登录功能(带图片验证码)
- require.js与bootstrap结合实现简单的页面登录和页面跳转功能
- 首次跟前端搭配开发——活动页面分享功能实现
- PHP简单实现“相关文章推荐”功能的方法
- javascript 简单实现页面上的拖放功能
- 利用Pillow,几行代码实现的最简单的Django页面验证码功能