windows phone7 练习作品(2)

      新浪微博API的基础应用,这几天公司比较忙,今天终于有时间完成这么题目.作品效果图如下:

 

主要代码:MainPage.xaml

 <Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<!--TitlePanel 包含应用程序的名称和页标题-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="新浪微博API" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="我喜欢的名人" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>

<!--ContentPanel - 在此处放置其他内容-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">


<toolkit:HubTile x:Name="ycHubTile" Height="173" HorizontalAlignment="Left" Margin="12,18,0,0" Title="{Binding Title}"
Notification
="{Binding Notification}"
DisplayNotification
="{Binding DisplayNotification}"
Message
="{Binding Message}"
GroupTag
="{Binding id}"
Source
="{Binding ImageUri}"
VerticalAlignment
="Top" Width="173" Tap="ycHubTile_Tap" />
<toolkit:HubTile x:Name="mzHubTile" Height="173" HorizontalAlignment="Left" Margin="228,18,0,0" Title="{Binding Title}"
Notification
="{Binding Notification}"
DisplayNotification
="{Binding DisplayNotification}"
Message
="{Binding Message}"
GroupTag
="{Binding id}"
Source
="{Binding ImageUri}"
VerticalAlignment
="Top" Width="173" Tap="mzHubTile_Tap" />
</Grid>
</Grid>

 

 

MainPage.xaml.cs

public partial class MainPage : PhoneApplicationPage
{
// 构造函数
public MainPage()
{
InitializeComponent();
}

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
//返回姚晨信息
WebClient clientUser = new WebClient();
clientUser.DownloadStringCompleted += new DownloadStringCompletedEventHandler(userInfo_DownloadStringCompleted);
        //App Key在新浪微博申请
            string urlUser = "http://api.t.sina.com.cn/statuses/user_timeline.xml?source=App Key&user_id=1266321801&feature=1&page=1&count=1";
clientUser.DownloadStringAsync(new Uri(urlUser));

//返回蛮子信息
WebClient clientUserMz = new WebClient();
clientUserMz.DownloadStringCompleted += new DownloadStringCompletedEventHandler(userInfoMz_DownloadStringCompleted);

string urlUserMz = "http://api.t.sina.com.cn/statuses/user_timeline.xml?source=App Key&user_id=1813080181&feature=1&page=1&count=1";

clientUserMz.DownloadStringAsync(new Uri(urlUserMz));

}
public void userInfo_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
var xml = XElement.Parse(e.Result);
var videosTemp = (
from p in xml.Descendants("user")
select new usersInfo()
{
Title = p.Element("screen_name").Value,
Message = p.Element("description").Value,
ImageUri = p.Element("profile_image_url").Value,
id = p.Element("id").Value,

}).ToList();

ycHubTile.DataContext = videosTemp[0];
}
}
public void userInfoMz_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
var xml = XElement.Parse(e.Result);
var videosTemp = (
from p in xml.Descendants("user")
select new usersInfo()
{
Title = p.Element("screen_name").Value,
Message = p.Element("description").Value,
ImageUri = p.Element("profile_image_url").Value,
id = p.Element("id").Value,

}).ToList();

mzHubTile.DataContext = videosTemp[0];
}
}

private void ycHubTile_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
HubTile ht = sender as HubTile;

this.NavigationService.Navigate(new Uri("/Views/weiboList.xaml?user_id=" + ht.GroupTag + "&name=" + ht.Title, UriKind.Relative));
}

private void mzHubTile_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
HubTile ht = sender as HubTile;

this.NavigationService.Navigate(new Uri("/Views/weiboList.xaml?user_id=" + ht.GroupTag+"&name="+ht.Title, UriKind.Relative));
}
}

 

 微博列表页面:weiboList.xaml

<phone:PhoneApplicationPage.Resources>
<utils:DateConverter x:Key="dateConverter"/>

<DataTemplate x:Key="statusTemplate1">
<StackPanel Width="457" Orientation="Horizontal" Height="170">
<StackPanel Width="330" HorizontalAlignment="Left">
<TextBlock Text="{Binding text}" Margin="0" TextWrapping="Wrap" FontSize="24" Height="126"/>
<TextBlock TextWrapping="Wrap" Text="{Binding created_at,Converter={StaticResource dateConverter }}" FontSize="16" Margin="0,10,0,0" Foreground="#FF858585"/>
</StackPanel>
<StackPanel Height="120" Width="120" VerticalAlignment="Top">
<Image Source="{Binding thumbnail_pic}" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>

<!--LayoutRoot 是包含所有页面内容的根网格-->
<Grid x:Name="LayoutRoot" Background="Transparent" DataContext="{Binding Source={StaticResource WeiBoDataSource}}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<!--TitlePanel 包含应用程序的名称和页标题-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="新浪微博API" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="页面名称" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>

<!--ContentPanel - 在此处放置其他内容-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox x:Name="weiboListBox" ItemTemplate="{StaticResource statusTemplate1}" />
</Grid>
</Grid>

</phone:PhoneApplicationPage>

绑定日期字段时用到日期转换,DateConverter.cs

 public class DateConverter : IValueConverter
{
object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
{

CultureInfo provider = CultureInfo.InvariantCulture;
DateTime dt = DateTime.ParseExact(value.ToString(), "ddd MMM dd HH:mm:ss zzz yyyy", provider, DateTimeStyles.AllowWhiteSpaces);

return DateStringFromNow(dt);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}

public string DateStringFromNow(DateTime dt)
{
TimeSpan span = DateTime.Now - dt;

if (span.TotalDays > 1)
{
return dt.ToString("MM月dd日");
}
else
if (span.TotalHours > 1)
{
return string.Format("{0}小时前", (int)Math.Floor(span.TotalHours));
}
else
if (span.TotalMinutes > 1)
{
return
string.Format("{0}分钟前", (int)Math.Floor(span.TotalMinutes));
}
else
if (span.TotalSeconds >= 1)
{
return
string.Format("{0}秒前", (int)Math.Floor(span.TotalSeconds));
}
else
{
return
"1秒前";
}

}

}


weiboList.xaml.cs页面

public partial class weiboList : PhoneApplicationPage
{
public weiboList()
{
InitializeComponent();
}

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
string user_id = Convert.ToString(NavigationContext.QueryString["user_id"]);
PageTitle.Text = Convert.ToString(NavigationContext.QueryString["name"]);
//返回文化传播
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
string url = "http://api.t.sina.com.cn/statuses/user_timeline.xml?source=App Key&user_id=" + user_id + "&feature=1&page=1&count=20";
client.DownloadStringAsync(new Uri(url));
}
public void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
var xml = XElement.Parse(e.Result);
var videosTemp = (
from p in xml.Descendants("status")
select new sinaApi.Models.weiboList()
{
text = p.Element("text").Value,
created_at=p.Element("created_at").Value,
thumbnail_pic = p.Element("thumbnail_pic") == null ? "" : p.Element("thumbnail_pic").Value
}).ToList();

weiboListBox.Items.Clear();
videosTemp.ForEach(p => weiboListBox.Items.Add(p));
}
}

}



 源代码下载:微博Api练习

 注:源码中source处请填写您自己的新浪微博的app key否则无法获取数据。

 

posted @ 2012-03-01 01:23  大眼怪  阅读(2291)  评论(13编辑  收藏  举报