Wednesday, September 26, 2012

Why does binding take so long in WP7? I can see it happening

Why does binding take so long in WP7? I can see it happening

I am populating a list searchResults in the Loaded event of a page.

If this loading takes 50ms, then when I load the page by navigating back to it, I will see the previous searchResults for a fraction of second before the binding completes.

a) In what event can I bind controls before the page becomes visible? (The page is being navigated back to, so the constructor is not called again.)

b) Is there any way to force a binding to happen in the Loaded event code? I notice that list1.ItemSource=x bindings seem to be done after the event function falls out of scope. As if they are done in OnIdle.

More details on b)

If I run the following code:

(ItemsControl) lstSearchResults.ItemSource = searchResults; int iCount = lstSearchResults.Items.Count 

iCount will be zero whether there are items in the searchResults or not.

Is there a way to bind lstSearchResults to searchResults that populates straight away?

Answers & Comments...

Answer: 1
  1. OnNavigatedTo event is being called, you can try doing your work there
  2. I am not completely sure what you mean but if you already did the binding between an observable collection and a listbox, then the changes you make to the observable collection will reflect in the listbox.
by : igralihttp://stackoverflow.com/users/793467

Answer: 2

1) Associate your DataContext to your viewModel and Bind the list on the UI itself using ItemsSource={Binding searchResults}"

2) On the Loaded event do lstSearchResults.ItemSource = searchResults;

Note that it is recommended that if your list is dynamic to have the lis as ObservableCollection

If searchResults are variable height items it may take time for the UI binding depending upon the ItemTemplate. To tackle that problem one should use Virtualizing StackPanel which is already a part of ListBox Control. Hence it is highly unlikely the problem to arise in your case if you are already using a listbox. For further detailed information you may go through this resource. I hope it helps.

by : Milan Aggarwalhttp://stackoverflow.com/users/1378956




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog