Reputation: 166
I have a Panorama control with 8 PanoramaItems, each containing a LongListSelector. Using MVVM the listbox is bound to a ObservableCollection property.
I need to fill each list with data (a lot of data photo , text ..) from a webserver.
How do I do this, the best way, without blocking the UI thread ?
I tried something like this
System.Threading.ThreadPool.QueueUserWorkItem(x => LoadList1());
System.Threading.ThreadPool.QueueUserWorkItem(x => LoadList2());
...
Where the Load function looks like this:
[EDIT] using Rx
private IDisposable _disp;
private void LoadList1()
{
_disp = Observable.FromEvent<PhotoEventArgs>(_webServer, "GetPhotosCompleted")
.Select(a => from l in a.EventArgs.Result
where l.Name.Length > 1
group l by l.Name.ToLower()[0] into c
orderby c.Key
select new Group<Photo>(c.Key, c))
.ObserveOnDispatcher()
.Subscribe(a =>
{
List1Items = new ObservableCollection<Group<Photo>>(a);
_disp.Dispose();
});
_webServer.GetPhotosAsync();
}
It’s working, but the UI is still frozen a part of the time.
How can I make this perform better ?
I guess the problem is that I uses LongListSelector, so I need to add all the data at once to the List1Items for the LongListSelector to group correct.
EDIT: there is a bug in the Windows Phone Toolkit - Nov 2011 (7.1 SDK) coursing the LongListSelector to group wrong !
Upvotes: 1
Views: 483
Reputation: 26346
You should consider looking into Reactive Extensions for Windows Phone
Your code would end up looking somewhat like this:
private void LoadList1()
{
Observable.FromEvent<PhotoEventArgs>(
e => new EventHandler(e),
e => _webServer.GetPhotosCompleted += e,
e => _webServer.GetPhotosCompleted -= e
).Select(e =>
{
return
from l in e.Result
where l.Name.Length > 1
group l by l.Name.ToLower()[0]
into c
orderby c.Key
select new Group<Photo>(c.Key, c);
})
.SubscribeOnDispatcher()
.Subscribe(result =>
{
foreach (var item in result)
List1Items.Add(item);
});
}
Upvotes: 0
Reputation: 216
The part of this that blocks the UI thread is where you set the List1Items to be the entire result set, try breaking that up in multiple insertions, say 5 or 10 at a time. I'm writing this from my phone so it is hard to give you a code example, but it looks like you have the coding part under control and just needed a lille kick in the right direction.
Upvotes: 1