简体   繁体   中英

Best way to fill list from WS Async in WP7

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 !

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.

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);
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM