I built an app using an MVVM architecture like used in the MeetupManager example.
Everything is working well but I must integrate with some legacy WCF web services with async calls.
In the constructor of the view model I make the call to the web service and then populate the list from the web service callback..... so far so good
Unfortunately the data does not show up in the bound listview .. if I navigate to a modal form then back it's fine ....
How do I force a rebind using an MVVM architecture?
I tried manually calling property changed and even ForceLayout to no avail...
I also upgraded to the latest version of Xamarin Forms(2.3.2.127) and no change in behavior...
The system type List is not an ObservableCollection.
The object on your ViewModel should be an ObservableCollection if you want collection updates to trickle down to the listview.
Answers
@StephenHauck,
Have you tried doing your update in
Device.BeginInvokeOnMainThread(...)
?Not MVVM friendly, but we can figure that out if it works.
John, what do you mean.. I am calling a WCF async web service and subscribing to the complete event.
I bind to a new instance of the view model in the code behind....
In the view model constructor I subscribe to the web service complete event and then call the web service.....
I need a way to let the view know.. "the data has been received and you can rebind"... which I thought was being handled by the databinging in the XAML....
You probably have an error in the bindings, either not using an observable collection as your data source or just something wrong.
Could you post some code?
@StephenHauck,
Sorry, I was confused. Can you share some code of what you're doing and what type of collection is being used? It would also be good to see the INPC implementation for your backing collection.
Adam, the fact that I navigate to a modal form and when I come back the data shows kinda rules out binding errors....right?
How do I log, monitor, capture binding errors?
The XAML is binding to a List, is this not an observable collection?
The other thing is that it works from time to time just fine.....
The system type List is not an ObservableCollection.
The object on your ViewModel should be an ObservableCollection if you want collection updates to trickle down to the listview.
OK, so I changed it and it seems to work but it was working intermittently before .... so I will have to wait and see for sure.
Could this be because the iOS "emulator" is a simulator and not an emulator ?
Also, is there a way to trap binding issues / errors?
@StephenHauck,
I don't think the simulator has anything to do with it. My guess is that you were seeing updates if you replaced the entire
List<T>
instead of adding something to it. Are you adding items to the list now and expecting it to update?I ask because that is a difference between using a
List<T>
and anObservableCollection<T>
. The later will send INPC events when things are added/removed. In contrast,List<T>
does not do that. I assumed you had something likeIf so, the INPC events would have only changed if you did something like
MyItems = new List<T>(...);
and not like
MyItems.Add(...);
OK, thanks for clarifying that point.
I was not expecting to see changes in the data I was simply wanting to show the list to allow selections to be made when I experienced the issue but what you are saying about the INPC may have prevented the proper rendering as well??????