从Twitter获取2个(或更多)用户的时间表

时间:2011-09-06 08:58:31

标签: c# wpf twitter twitter-feed

我正在尝试在列表中获取2个或更多用户的时间轴,然后按日期排序并在列表视图中显示

目前我有以下内容: -

        Contacts searchTerm1 = cmbContact.SelectedItem as Contacts;

        if (searchTerm1 != null)
        {
            contactList = GetCurrentContacts(searchTerm1.CatID);

            foreach (var contact in contactList)
            {
                GetTweetResults(contact.Username);

                foreach (var contactTweet in _contactTweets)
                {
                    _allContactTweets.Add(contactTweet);
                }

            }

            TweetList1.ItemsSource = _allContactTweets;
        }

我正在收到推文Async

    private void GetTweetResults(string user)
    {

        WebClient twitterAccess = new WebClient();
        twitterAccess.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitterAccess_DownloadStringCompleted);
        twitterAccess.DownloadStringAsync(new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=" + user));

    }

    private void twitterAccess_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs args)
    {
        try
        {
            XElement xmlTweets = XElement.Parse(args.Result);
            var contactTweets = (from tweet in xmlTweets.Descendants("status")
                                                    select new ContactTweet
                                                               {
                                                                   ProfileImage =
                                                                       tweet.Element("user").Element("profile_image_url").Value,
                                                                   TweetText = tweet.Element("text").Value,
                                                                   UserName = tweet.Element("user").Element("screen_name").Value,
                                                                   Created = (tweet.Element("created_at").Value.ParseDateTime())
                                                               }).ToList();


            foreach (var contactTweet in contactTweets)
            {
                _contactTweets.Add(contactTweet);
            }


        }
        catch (Exception exception)
        {
            MessageBox.Show("Error downloading tweets - " + exception.Message);
        }

    }
}

现在我的想法是,在GetTweetResults中,等待twitterAccess_DownloadStringCompleted完成然后继续该过程,但我不知道该怎么做。

谁能告诉我如何实现这个目标?

感谢您的帮助和时间

更新

管理将其归结为

        WebClient twitterAccess = new WebClient();
        twitterAccess.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitterAccess_DownloadStringCompleted);
        twitterAccess.DownloadStringAsync(new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=" + contactList1[0].Username));

        WebClient twitterAccess2 = new WebClient();
        twitterAccess2.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitterAccess_DownloadStringCompleted);
        twitterAccess2.DownloadStringAsync(new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=" + contactList1[1].Username));

现在我需要找到一种在contactList1中循环并动态生成WebClient的方法.....

更新2

问题解决了

        foreach (var contact in contactList)
        {
            WebClient twitterAccess = new WebClient();
            twitterAccess.DownloadStringCompleted += new DownloadStringCompletedEventHandler(twitterAccess_DownloadStringCompleted);

            twitterAccess.DownloadStringAsync(new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=" + contact.Username));
        }

1 个答案:

答案 0 :(得分:0)

foreach(contactList中的var contact)         {             WebClient twitterAccess = new WebClient();             twitterAccess.DownloadStringCompleted + = new DownloadStringCompletedEventHandler(twitterAccess_DownloadStringCompleted);

        twitterAccess.DownloadStringAsync(new Uri("http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=" + contact.Username));
    }