C# Linq到twitter wp8的实现
我试图让Linq to twitter在wp8应用程序上工作,我查阅了所有可用的文档,但没有弄清楚这一点,当尝试(作者说适用于wp8)时,我发现了错误。 我使用ApplicationOnlyAuthorizer,我的目的只是能够阅读公共推文(不能登录和发送推文) MainPage.csC# Linq到twitter wp8的实现,c#,linq,twitter,windows-phone-8,linq-to-twitter,C#,Linq,Twitter,Windows Phone 8,Linq To Twitter,我试图让Linq to twitter在wp8应用程序上工作,我查阅了所有可用的文档,但没有弄清楚这一点,当尝试(作者说适用于wp8)时,我发现了错误。 我使用ApplicationOnlyAuthorizer,我的目的只是能够阅读公共推文(不能登录和发送推文) MainPage.cs public MainPage() { this.InitializeComponent(); UserTweetsWidget = new UserTweetsViewModel("xxxx
public MainPage()
{
this.InitializeComponent();
UserTweetsWidget = new UserTweetsViewModel("xxxxxxxx", 20);
this.DataContext = this;
}
TweetModel.cs
public class TweetModel
{
public string ScreenName { get; set; }
public string UserName { get; set; }
public string Image { get; set; }
public string Text { get; set; }
public string PublicationDate { get; set; }
}
UserTweetsViewModel.cs
public class UserTweetsViewModel
{
public string Label { get; set; }
public ObservableCollection<TweetModel> Tweets { get; set; }
private const string consumerKey = “xxxxxxx”;
private const string consumerSecret = “xxxxxx”;
private const string twitterAccessToken = “xxxxxxxxxxxxxxxxx”;
private const string twitterAccessTokenSecret = “xxxxxxxxxx”;
public UserTweetsViewModel(string userName, int count)
{
String _n= userName;
int _c= count;
InitializeAsync(_n, _c);
}
private async Task InitializeAsync(string userName, int count)
{
this.Label = string.Format("Tweets by @{0}", userName);
Tweets = await GetTwitterUserTimeLine(userName, count);
}
private async Task<ObservableCollection<TweetModel>> GetTwitterUserTimeLine(string userName, int count)
{
ObservableCollection<TweetModel> result = new ObservableCollection<TweetModel>();
var auth = new ApplicationOnlyAuthorizer
{
CredentialStore = new InMemoryCredentialStore
{
ConsumerKey = consumerKey,
ConsumerSecret = consumerSecret,
OAuthToken= twitterAccessToken,
OAuthTokenSecret= twitterAccessTokenSecret
}
};
await auth.AuthorizeAsync();
TwitterContext twitterCtx = new TwitterContext(auth);
var tweets = twitterCtx.Status.Where(tweet => tweet.ScreenName == userName && tweet.Type == StatusType.Home).Take(count).ToList();
foreach (var item in tweets)
{
TweetModel tweet = new TweetModel()
{
Text = item.Text,
ScreenName = item.User.Name,
UserName = "@" + item.ScreenName,
PublicationDate = Convert.ToString(item.CreatedAt),
Image = item.User.ProfileImageUrl
};
result.Add(tweet);
}
return result;
}
}
public类用户tweetsviewmodel
{
公共字符串标签{get;set;}
公共ObservableCollection推文{get;set;}
私有常量字符串consumerKey=“xxxxxxx”;
私有常量字符串consumerSecret=“xxxxxx”;
私有常量字符串twitterAccessToken=“XXXXXXXXXXXXXXXXXX”;
私有常量字符串twitterAccessTokenSecret=“xxxxxxxxx”;
public UserTweetsViewModel(字符串用户名,整数计数)
{
字符串_n=用户名;
int _c=计数;
初始化同步(_n,_c);
}
专用异步任务InitializeAsync(字符串用户名,整数计数)
{
this.Label=string.Format(“Tweets by@{0}”,用户名);
Tweets=等待GetWitterUserTimeline(用户名、计数);
}
私有异步任务GetTwitterUserTimeLine(字符串用户名,整数计数)
{
ObservableCollection结果=新的ObservableCollection();
var auth=新应用程序onlyauthorizer
{
CredentialStore=新的InMemoryCredentialStore
{
ConsumerKey=ConsumerKey,
ConsumerCret=ConsumerCret,
OAuthToken=twitterAccessToken,
OAuthTokenSecret=twitterAccessTokenSecret
}
};
等待auth.AuthorizeAsync();
TwitterContext twitterCtx=新TwitterContext(auth);
var tweets=twitterCtx.Status.Where(tweet=>tweet.ScreenName==userName&&tweet.Type==StatusType.Home).Take(count.ToList();
foreach(tweets中的var项)
{
TweetModel tweet=新TweetModel()
{
Text=项目。Text,
ScreenName=item.User.Name,
UserName=“@”+item.ScreenName,
PublicationDate=Convert.ToString(item.CreatedAt),
Image=item.User.ProfileImageUrl
};
结果。添加(tweet);
}
返回结果;
}
}
谢谢,
Bob我认为,问题在于您没有等待AuthorizeAsync方法 首先,您需要将
getWitterUserTimeline
转换为async
方法:
private async Task<ObservableCollection<TweetModel>> GetTwitterUserTimeLine(string userName, int count)
这样,该方法将等待授权完成,然后继续执行
更新:
要从viewmodel调用此方法,可以在viewmodel中创建一个名为InitializeAsync的新方法:
private async Task InitializeAsync()
在该方法中,通过以下方式调用getWitterUserTimeline:
Tweets = await GetTwitterUserTimeLine(username, count);
最后,您需要从viewmodel构造函数中调用InitializeAsync方法,它将正常工作
希望这能有所帮助。我刚刚尝试过,但出现了一个错误
错误1无法将类型“System.Threading.Tasks.Task”隐式转换为“System.Collections.ObjectModel.ObservableCollection”
您还需要等待对GetWitterUserTimeline的调用,以接收ObservableCollection而不是任务。您能告诉我如何修改吗?这就是我如何调用公共用户tweetsviewmodel(string userName,int count){This.Label=string.Format(“Tweets by@{0}”,userName);This.Tweets=getWitterUserTimeline(userName,count);}调用正在进行,但现在我被卡在下一行,这应该是响应数据var tweets=twitterCtx.Status.Where(tweet=>tweet.ScreenName==userName&&tweet.Type==StatusType.Home)
在构造函数上我有public UserTweetsViewModel(string userName,int count){string\u n=userName;int\u c=count;InitializeAsync(_n,_c);}
您现在遇到了什么问题?
Tweets = await GetTwitterUserTimeLine(username, count);