ViewModel Property getting set to many times and with null value

Evertjan van den Brink 21 Reputation points

My Xamarin Forms app has a view with a Xamarin forms picker. The picker's ItemsSource is filled with a list. That list is filled by an observable which loads its data from an akavache cache and overrides that data after a service call is completet. The picker's selected item is filled based on that list.

the selected item is correctly filled from cache, and also the service call fills the selected item correctly. But after that the setter from the selected item is called again with a null value.


    public CreditCardListItem SelectedCreditCardListItem  
        get => selectedCreditCardListItem;  
            if (Expense == null ||   
                selectedCreditCardListItem == value)  

            var valueContainsACreditCardListItem = value != null;  
            if (valueContainsACreditCardListItem)  
                var valueContainsARealCreditCard = value.Id != null;  
                if (valueContainsARealCreditCard)  
                    Expense.ExpenseType = ExpenseType.CreditCard;  
                    Expense.CreditCardId = value.Id;  

                    if (Expense.BundleId != null)  
                        Expense.BundleId = null;  
                        SelectedExpenseBundleListItem = null;  
                    Expense.ExpenseType = ExpenseType.Standard;  
                    Expense.CreditCardId = null;  

                selectedCreditCardListItem = value;  

            if (selectedCreditCardListItem == null)  

A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
4,921 questions
No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,866 Reputation points


    Welcome to our Microsoft Q&A platform!

    Is there any error occured? I tested the code in basic demo, and encountered the following exception when selecting an item of the picker.
    Java.Lang.NullPointerException: 'Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference'

    The problem is due to the 'ItemDisplayBinding' line code. Please try to set binding for ItemDisplayBinding to the parameter name instead of 'ModelClass.ParameterName'.

           ItemDisplayBinding="{Binding Path=CardNumber}"  

    Best Regards,

    Jarvan Zhang

    If the response is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

0 additional answers

Sort by: Most helpful