我在我处理按钮单击的 Windows 应用商店应用程序中有这段代码。但不知何故,当我在极少数情况下单击此按钮时,UI 会冻结。当我连接到比我通常连接的网络更远的 Wi-Fi 网络时,通常会发生这种情况。考虑到我是从 Internet 下载 RSS 提要,这很可能与我使用 async/await 关键字有关。如果我正确理解了 async/await 的机制,那么 UI 线程根本不应该阻塞。我在这里错过了什么吗?
private async void Add_Click(object sender, RoutedEventArgs e)
{
AddButton.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
DownloadProgressRing.Visibility = Windows.UI.Xaml.Visibility.Visible;
DownloadProgressRing.IsActive = true;
RssData rssData = App.Current.Resources["rssData"] as RssData;
Uri uri = FixUrl(RssUrl.Text);
if (uri != null)
{
newRssFeed = new RssFeed(uri.ToString());
Task<bool> success = newRssFeed.RetrieveFeed();
if (await success)
{
SubmitButtonPanel.Visibility = Windows.UI.Xaml.Visibility.Visible;
FeedTitleTextBox.Text = newRssFeed.Title == null ? String.Empty : newRssFeed.Title;
}
}
}
RssFeed 的构造函数:
public RssFeed(string url)
{
Url = url;
}
RetrieveFeed 函数:
public async Task<bool> RetrieveFeed()
{
if (Url != null)
{
try
{
SyndicationClient sc = new SyndicationClient();
SyndicationFeed sf = await sc.RetrieveFeedAsync(new Uri(Url, UriKind.Absolute));
if (sf != null)
{
ValidateAndInitFeed(sf);
return true;
}
}
catch (Exception ex)
{
}
}
return false;
}
private void ValidateAndInitFeed(SyndicationFeed feed)
{
if (feed.Title != null)
Title = feed.Title.Text;
if (feed.ImageUri != null)
Image = feed.ImageUri.ToString();
if (feed.Subtitle != null)
Subtitle = feed.Subtitle.Text;
}