首页 文章

使用LINQ to Twitter访问带有C#的twitter

提问于
浏览
0

我试图在VS中创建Winforms应用程序以搜索特定关键字并检索我的应用程序中的推文显示 . 下面是我的代码,试图从我的 Profiles 中获取100条推文并在我的表单构造函数中调用新方法,然后将数据分配给我的表单上的主要推文列表框

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using LinqToTwitter; 

    public partial class Form1 : Form
        {
            private SingleUserAuthorizer authorizer =
             new SingleUserAuthorizer
             {
                 CredentialStore = new
                SingleUserInMemoryCredentialStore
                 {
                     ConsumerKey ="",
                     ConsumerSecret ="",
                     AccessToken ="",
                     AccessTokenSecret =""
                 }
             };

            private List<Status> currentTweets;
            public Form1()
            {
                InitializeComponent();

                GetMostRecent100HomeTimeLine();
                lstTweetList.Items.Clear();
                currentTweets.ForEach(tweet =>
                   lstTweetList.Items.Add(tweet.Text));
            }
            private void GetMostRecent100HomeTimeLine()
            {
                var twitterContext = new TwitterContext(authorizer);

                var tweets = from tweet in twitterContext.Status
                             where tweet.Type == StatusType.Home &&
                             tweet.Count == 100
                             select tweet;

                currentTweets = tweets.ToList();
            }

[mscorlib.dll中发生'System.AggregateException'类型的未处理异常]发生在下面的代码中

currentTweets = tweets.ToList();

我的猜测是LinqToTwitter库试图枚举'tweets'集合,发现没有数据,或者数据无法检索 . 我是这个主题的初学者,这是我的项目 .

希望有人能指导我解决问题

1 回答

  • 0

    LINQ to Twitter是异步的 . 因此,您应该在代码中使用 asyncawait 关键字 . 这也意味着将查询放在构造函数中并不是一个好主意 . 在这种情况下,我会使用 Form.Load 事件 . 假设您将 Form1_Load 处理程序连接到 Form1 Load 事件,您可以重写这样的代码:

    public async void Form1_Load()
            {
                await GetMostRecent100HomeTimeLine();
                lstTweetList.Items.Clear();
                currentTweets.ForEach(tweet =>
                   lstTweetList.Items.Add(tweet.Text));
            }
            private async Task GetMostRecent100HomeTimeLine()
            {
                var twitterContext = new TwitterContext(authorizer);
    
                var tweets = from tweet in twitterContext.Status
                             where tweet.Type == StatusType.Home &&
                             tweet.Count == 100
                             select tweet;
    
                currentTweets = await tweets.ToListAsync();
            }
    

    请注意 Form1_Load 方法是 async . 它还返回 void ,这是必需的,并且只应在顶级异步事件处理程序中使用 . 它由 GetMostRecent100HomeTimeLine 返回的任务 awaits .

    GetMostRecent100HomeTimeLine 方法也是 async ,但返回 Task . 在初始 async void 事件处理程序下面的异步调用链中的所有方法都应返回 TaskTask<T> . 这使您可以 await 他们的任务,以便调用方法在被调用的方法完成之前不返回 .

    最后,请注意代码 awaitsToListAsync 返回的任务 . 虽然编译器允许你使用 ToList (没有等待),但它赢得了't work at runtime because LINQ to Twitter is async and expects you to await it', SingleOrDefaultAsyncFirstOrDefaultAsync 等运算符 .

    您还可以使用 try/catch 处理程序来捕获抛出的任何异常 . LINQ to Twitter包含 TwitterQueryException 中的大多数异常,其中包含来自Twitter API的任何错误消息和相关代码 .

相关问题