1

我刚刚启动了一个 Windows Phone 应用程序,我需要获得所有用户的关注。我试过这个

SharedState.Authorizer = pinAuth;
ITwitterAuthorizer auth = SharedState.Authorizer;
TwitterContext twitterCtx = new TwitterContext(auth);

var friendList =
    (from friend in twitterCtx.SocialGraph
        where friend.Type == SocialGraphType.Friends && friend.ScreenName == "_TDK"
        select friend)
    .SingleOrDefault();

List<String> Followings;
foreach (var id in friendList.ScreenName)
{
    Followings.Add(id.ToString());
}

但是friendlist总是为,显然,foreach不喜欢这样并抛出一个exception.

有人可以帮助我吗?

谢谢。

4

2 回答 2

1

我有同样的问题,我用这种方式解决了(我知道这不是最好的方法)

public void getProfile(MyProgressBar myprogressbar)
{
    var auth = new SingleUserAuthorizer
    {
        Credentials = new InMemoryCredentials
        {
            ConsumerKey = GlobalVariables.ConsumerKey,
            ConsumerSecret = GlobalVariables.ConsumerSecret,
            AccessToken = GlobalVariables.AccessToken,
            OAuthToken = GlobalVariables.AccessTokenSecret
        }
    };

    using (var twitterCtx = new TwitterContext(auth, "https://api.twitter.com/1/", "https://search.twitter.com/"))
    {
        //Log
        twitterCtx.Log = Console.Out;

        var queryResponse = (from tweet in twitterCtx.Status
                             where tweet.Type == StatusType.User && tweet.ScreenName == GlobalVariables.ScreenName
                             select tweet);

        queryResponse.AsyncCallback(tweets =>
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                var publicTweets = (from tweet in tweets
                                    select tweet).FirstOrDefault();
                s.TwitterName = publicTweets.User.Name.ToString();
                s.TwitterScreenName = "@" + GlobalVariables.ScreenName;
                s.TwitterDescription = publicTweets.User.Description.ToString();
                s.TwitterStatus = publicTweets.User.StatusesCount.ToString() + " Tweets / " + publicTweets.User.FriendsCount.ToString() + " Following / " + publicTweets.User.FollowersCount.ToString() + " Followers";
                s.TwitterImage = publicTweets.User.ProfileImageUrl.ToString();

                myprogressbar.ShowProgressBar = false;

            })).SingleOrDefault();
    }
}
于 2013-02-06T13:55:10.010 回答
1

我认为您需要遍历 IDs 集合,如下所示:

foreach (var id in friendList.IDs)
{
    Followings.Add(id.ToString());
}

您需要使用基于 Silverlight 的应用程序(包括 Windows Phone)进行异步调用。以下是如何重构查询的示例:

var twitterCtx = new TwitterContext(auth);

(from social in twitterCtx.SocialGraph
 where social.Type == SocialGraphType.Followers &&
       social.ScreenName == "JoeMayo"
 select social)
.MaterializedAsyncCallback(asyncResponse =>
     Dispatcher.BeginInvoke(() =>
     {
         if (asyncResponse.Status != TwitterErrorStatus.Success)
         {
              MessageBox.Show(
                  "Error during query: " + 
                  asyncResponse.Exception.Message);
              return;
         }

         SocialGraph social = asyncResponse.State.SingleOrDefault();

         SocialListBox.ItemsSource = social.IDs;
     }));

The MaterializedAsyncCallback manages the callback from Twitter. Notice how I use Dispatcher.BeginInvoke to marshal the call back onto the UI thread as the callback is on a worker thread. On the asyncResponse callback parameter, use Status to see if there is an error and use State to get the data if the query is successful.

于 2013-02-06T18:20:49.627 回答