question

EvertjanvandenBrink-7931 avatar image
0 Votes"
EvertjanvandenBrink-7931 asked EvertjanvandenBrink-7931 commented

ViewModel Property getting set to many times and with null value

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);
         }
     }
dotnet-xamarin
image.png (62.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered EvertjanvandenBrink-7931 commented

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.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,

There is no error occurred. The binding is set to the parameter name. Resharper displays the full path buth that's not really there.

0 Votes 0 ·