Manoj
Manoj

Reputation: 2442

Lazy loading list box, in a panorama page

I wanted to add a lazy loading list box(load content when swipe) in a panorama page in one of my windows phone 7 applications. I could however do it using a pivot page. I referred this link

But this is not working with panorama page.
Can anyone please help me?

Upvotes: 1

Views: 782

Answers (2)

Nate Diamond
Nate Diamond

Reputation: 5575

Okay, you're going to need to do one of two things: use the BCL Async package (basically adds async Tasks and such to WP7) or use a background worker. I highly suggest the BCL Async package, it's easy to get on Nuget.

Now, in your ViewModel (you are using MVVM, yes?) the property that it's bound to, let's call it Items should return an ObservableCollection of the item type you need. Now, here's where the magic happens. In the Getter of that property, return a new collection and use a task to fill it. Something like this:

public ObservableCollection<object> Items
{
    get
    {
        ObservableCollection<object> retCollection = new ObservableCollection<object>();
        FillCollection(retCollection);
        return retCollection;
    }
}

public async void FillCollection(ObservableCollection<object> collectionToFill)
{
    Task.Factory.StartNew(() =>
    {
        foreach(object objectToAdd in collectionImGettingThisDataFrom)
        {
            // We do this using the Dispatcher to 
            // be sure to pop back into the UI thread.
            Deployment.Current.Dispatcher.BeginInvoke(
                () => collectionToFill.Add(objectToAdd));
        }
    }
}

Because FillCollection is async, the Get method will continue and return the current collection. On another thread, the Task that's created will find the data to add, then push it to the UI thread to add it into the collection. This way, you'll be able to lazy load the data only when you ask for it, without completely blocking your UI thread. If it turns out that it's still making your UI slow, you can add the line:

await TaskEx.Delay(25); // Some time in milliseconds. Too much and it will 
                        // take a long time to load the list,
                        // too little and it will still bog down your UI.

At the end of the foreach block, but not in the Dispatcher invokation.

Happy coding!

Upvotes: 1

DotNetRussell
DotNetRussell

Reputation: 9867

Have you looked at the Telerik Rad Controls yet? They have all types of pull to refresh controls. I used them in a recent app I released called "Rad Libs". You can see the controls here http://www.telerik.com/products/windows-phone.aspx and you can also download an app that demos all of their controls. (Disclaimer: I have no affiliation with telerik. I stand to gain nothing from promoting them here)

Upvotes: 1

Related Questions