ViewModel Property getting set to many times and with null value

asked 2021-11-08T14:22:59.863+00:00
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.

147462-image.png

    public CreditCardListItem SelectedCreditCardListItem  
    {  
        get => selectedCreditCardListItem;  
        set  
        {  
            if (Expense == null ||   
                selectedCreditCardListItem == value)  
            {  
                return;  
            }  

            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;  
                    }  
                }  
                else  
                {  
                    Expense.ExpenseType = ExpenseType.Standard;  
                    Expense.CreditCardId = null;  
                }  

                selectedCreditCardListItem = value;  
                RaisePropertiesChanged();  
            }  

            if (selectedCreditCardListItem == null)  
            {  
                SetNoCreditCard();  
            }  

            RaisePropertyChanged(nameof(propertiesDependingOnExpense));  
            RaisePropertiesChanged(propertiesDependingOnCreditCardChange);  
        }  
    }  
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
4,803 questions
No comments
{count} votes

Accepted answer
  1. answered 2021-11-09T02:02:31.663+00:00
    JarvanZhang 23,836 Reputation points

    Hello,​

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

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