Search code examples
c#windows-phone-7asynchronousstackpanel

Windows phone 7 add items asynchronously to stackpanel


I'm wondering if it is possible add children asynchronously to stackpanel (or any other element). It seems that all items are rendered at once.

it doesn't matter if i use background worker or dispatcher. Always the result is the same - when all items have been added then they are shown.

What i would like to have is something like this:

Deployment.Current.Dispatcher.BeginInvoke(() => {
    foreach (var item in items.Skip(x).Take(pageSize))
    {
        // when this row is executed new item should be visible
        ItemsList.Children.Add(new _ListItem(item));
    }
});

regards max


Solution

  • This snippet adds new TextBlock into stackPanel every 500 ms without freezing UI. Item is visible in UI right after it has been added:

    ThreadPool.QueueUserWorkItem(_ => {
                    foreach (int item in Enumerable.Range(1,50)) {
                      Thread.Sleep(500);//simulate some calculations here
                      int item1 = item;
                      Deployment.Current.Dispatcher.BeginInvoke(() => {
                          stackPanel.Children.Add(new TextBlock(){Text = "Text "+item1});
                      });
                    }
                });