latest posts

I've been working on a small-ish scale project for the last week now off and on, I'll be announcing it officially probably next week. But anyway, I was running into a problem the main function of the app taking quite a while to process. Given that the performance of the app is entirely dependent on the 3G, 4G, LTE or WiFi connection strength of the phone I wanted to assume the worst when it gets released. Therefore I started looking around for a way to as the the results were returned have the Listbox update asynchronously. Previously with the Windows Phone 7 apps I have developed I simply have had a "Loading" Indicator and a second or two later it would go away, so there really wasn't a need for a "better" solution until now. Hunting around the WP7 SDK, I came across exactly what I was looking for: Observable Collections. In the "default" solution I had noticed this collection type previously, but always removed it in lieu of a Generic List Collection of whatever object I was using. The only caveat I found when using this approach is that you have to use the Dispatcher to update the collection on the UI Thread. That being said here's a quick code sample: In your Button or Page_Load Event set your Listbox ItemSource to the ObservableCollection you're going to populate:
lstBxResults.ItemsSource = App.ViewModel.Results; ]]>
Inside your ViewModel loop or asynchronously task (like a WCF service call for instance), wrap the addition of the result inside a Dispatcher Invoke:
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => {
     Results.Add(new YourObject(e.Result)); }
); ]]>
With those 2 blocks you'll have your Listbox updated as they get added to the ObservableCollection from whatever time consuming task your phone is doing. Hope that helps someone out.