I am developing a Windows Metro application and I am having a problem with the user interface becoming unresponsive. As far as I can tell, the reason is this:
<ListView ... SelectionChanged="ItemListView_SelectionChanged" ...
This event is handled here:
async void ItemListView_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (this.UsingLogicalPageNavigation()) this.InvalidateVisualState(); MyDataItem dataItem = e.AddedItems[0] as MyDataItem; await LoadMyPage(dataItem); } private async Task LoadMyPage(MyDataItem dataItem) { SyndicationClient client = new SyndicationClient(); SyndicationFeed feed = await client.RetrieveFeedAsync(new Uri(FEED_URI)); string html = ConvertRSSToHtml(feed) myWebView.NavigateToString(html, true); }
LoadMyPage takes time to complete because it receives data from the web service and loads it onto the screen. However, the user interface seems to be waiting for him: I think, until the above event completes.
So my question is: what can I do about this? Is there a better event that I can connect to, or is there another way to handle this? I thought about starting a background job, but for me it seems like an overkill.
EDIT:
To clarify the scope of this problem, I am talking about a maximum of 3-4 seconds that do not respond. This is by no means lengthy work.
EDIT:
I tried some suggestion below, however the whole call stack from SelectionChanged function uses async / await. I tracked this statement:
myFeed = await client.RetrieveFeedAsync(uri);
Which does not seem to continue processing until its completion.
EDIT:
I understand that this is turning into "War and Peace," but below is a replication of the problem using an empty metro application and button:
XAML:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <StackPanel> <Button Click="Button_Click_1" Width="200" Height="200">test</Button> <TextBlock x:Name="test"/> </StackPanel> </Grid>
Code behind:
private async void Button_Click_1(object sender, RoutedEventArgs e) { SyndicationFeed feed = null; SyndicationClient client = new SyndicationClient(); Uri feedUri = new Uri(myUri); try { feed = await client.RetrieveFeedAsync(feedUri); foreach (var item in feed.Items) { test.Text += item.Summary.Text + Environment.NewLine; } } catch { test.Text += "Connection failed\n"; } }