我们目前正在创建一个Windows Store应用程序,它从RSS feed获取信息并将该信息输入到ObservableCollection中。我们遇到的问题是,当获取信息时,应用程序UI变得没有响应。
为了解决这个问题,我考虑创建一个新线程并在其中调用该方法。然而,在一些研究之后,我们意识到这在Windows应用商店应用中不再可能。我们如何绕过这个问题呢?
收集信息的方法如下所示。
public void getFeed()
{
setupImages();
string[] feedUrls = new string[] {
"http://www.igadgetos.co.uk/blog/category/gadget-news/feed/",
"http://www.igadgetos.co.uk/blog/category/gadget-reviews/feed/",
"http://www.igadgetos.co.uk/blog/category/videos/feed/",
"http://www.igadgetos.co.uk/blog/category/gaming/feed/",
"http://www.igadgetos.co.uk/blog/category/jailbreak-2/feed/",
"http://www.igadgetos.co.uk/blog/category/kickstarter/feed/",
"http://www.igadgetos.co.uk/blog/category/cars-2/feed/",
"http://www.igadgetos.co.uk/blog/category/software/feed/",
"http://www.igadgetos.co.uk/blog/category/updates/feed/"
};
{
try
{
XNamespace dc = "http://purl.org/dc/elements/1.1/";
XNamespace content = "http://purl.org/rss/1.0/modules/content/";
foreach (var feedUrl in feedUrls)
{
var doc = XDocument.Load(feedUrl);
var feed = doc.Descendants("item").Select(c => new ArticleItem() //Creates a copy of the ArticleItem Class.
{
Title = c.Element("title").Value,
//There are another 4 of these.
Post = stripTags(c.Element(content + "encoded").Value) }
).OrderByDescending(c => c.PubDate);
this.moveItems = feed.ToList();
foreach (var item in moveItems)
{
item.ID = feedItems.Count;
feedItems.Add(item);
}
}
lastUpdated = DateTime.Now;
}
catch
{
MessageDialog popup = new MessageDialog("An error has occured downloading the feed, please try again later.");
popup.Commands.Add(new UICommand("Okay"));
popup.Title = "ERROR";
popup.ShowAsync();
}
}
}我们如何才能使应用程序在获得此信息时不会冻结,因为在Windows应用商店应用程序中不可能进行线程处理。
例如-我们计划使用;
Thread newThread = new Thread(getFeed);
newThread.Start发布于 2013-06-07 21:17:02
对于发生在UI线程上的操作,您需要使用文档化良好的异步模式。Paul-Jan在评论中给出的链接是您需要开始的地方。http://msdn.microsoft.com/en-us/library/windows/apps/hh994635.aspx
https://stackoverflow.com/questions/16984349
复制相似问题