Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

MVVM pattern and refreshing bound views, async WCF call in view model constructor delay

StephenHauckStephenHauck USMember ✭✭✭

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...

Tagged:

Best Answer

Answers

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai
    edited October 2016

    @StephenHauck,

    Have you tried doing your update in Device.BeginInvokeOnMainThread(...)?

    Not MVVM friendly, but we can figure that out if it works.

  • StephenHauckStephenHauck USMember ✭✭✭

    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....

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    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?

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai

    @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.

  • StephenHauckStephenHauck USMember ✭✭✭

    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.....

  • StephenHauckStephenHauck USMember ✭✭✭

    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?

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai

    @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 an ObservableCollection<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 like

    public List<T> MyItems
    {
       get { return _myItems;}
       set { 
           _myItems = value;
          OnPropertyChanged("MyItems");
       }
    }
    

    If so, the INPC events would have only changed if you did something like

    MyItems = new List<T>(...);

    and not like

    MyItems.Add(...);

  • StephenHauckStephenHauck USMember ✭✭✭

    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??????

Sign In or Register to comment.