How to Binding Result from ViewModel in Xamarin

Issue

I have 1 question here: How to get data from ViewModel in Xamarin, however I still haven’t solved the problem. I created a new post with some changes.

I have:

  1. PageOne.xaml

     <StackLayout>
          <RefreshView x:DataType"locals:ViewCustomerViewModel" Command"{Binding LoadUserinfoCommand}" IsRefreshing"{Binding IsBusy, ModeOneWay}">
               <Label Text"{Binding Customer.Address}" />
          </RefreshView>
     </StackLayout>
    
  2. PageOne.xaml.cs

      ViewCustomerViewModel viewCustomerViewModel;
      public Customer CustomerGet { get; set; }
      public PageOne()
      {
         InitializeComponent();
         BindingContext  viewCustomerViewModel  new ViewCustomerViewModel();
         viewCustomerViewModel.OnAppearing();
      }
    
  3. Class Customer

     public class Customer
     {
        public string Address{ get; set; }
        ........
     }
    
  4. ViewCustomerViewModel

     public class ViewCustomerViewModel:BaseCustomerViewModel
     {
        ApiServiceUserinfo apiServiceUserinfo  new ApiServiceUserinfo();
        public Command LoadUserinfoCommand { get; }
        public ObservableCollection<Customer> CustomerInfos { get; set; }
        public ViewCustomerViewModel()
        {
           LoadUserinfoCommand  new Command(async () > await ExecuteLoadUserinfoCommand());
           CustomerInfos  new ObservableCollection<Customer>();
        }
    public void OnAppearing()
    {
       IsBusy  true;
    }
    
    async Task ExecuteLoadUserinfoCommand()
    {
       string userget  "1";
    
       IsBusy  true;
    
       try
       {
         CustomerInfos.Clear();
         var customerList  await apiServiceUserinfo.GetCustomersInfo(userget);
         CustomerInfos.Add(customerList);
       }
       catch (Exception)
       {
         throw;
       }
       finally
       {
         IsBusy  false;
       }
    }
    

    }

And I got the result CustomerInfos.Add(customerList);

enter image description here

However <Label Text"{Binding Customer.Address}" /> does not get results

Please help me again clearly in the answer. Thank you.

Update

  1. ViewCustomerViewModel

     public class ViewCustomerViewModel:BaseCustomerViewModel
     {
        ApiServiceUserinfo apiServiceUserinfo  new ApiServiceUserinfo();
        public Command LoadUserinfoCommand { get; set;}
    public Customer CustomerGets { get; set;}--&gt; update
    
    public ViewCustomerViewModel()
    {
       LoadUserinfoCommand  new Command(async () &gt; await ExecuteLoadUserinfoCommand());
       //CustomerGets  new Customer();
    }
    
    public void OnAppearing()
    {
       IsBusy  true;
    }
    
    async Task ExecuteLoadUserinfoCommand()
    {
       string userget  "1";
    
       IsBusy  true;
    
       try
       {
         var customerList  await apiServiceUserinfo.GetCustomersInfo(userget);
         CustomerGets  customerList;
       }
       catch (Exception)
       {
         throw;
       }
       finally
       {
         IsBusy  false;
       }
    }
    

    }

enter image description here

  1. PageOne.xaml

    <StackLayout>
       <RefreshView x:DataType"locals:ViewCustomerViewModel" Command"{Binding LoadUserinfoCommand}" IsRefreshing"{Binding IsBusy, ModeOneWay}">
           <Label Text"{Binding CustomerGets.Address}" />
       </RefreshView>
    </StackLayout>
    

Solution

We need to call the OnPropertyChanged method to notify the change in setter method of the property .

private Customer customerGets;
public Customer CustomerGets { 
  get { return customerGets; }
  set { 
          customerGets  value;
          NotifyPropertyChanged();     //the method is declared in BaseCustomerViewModel 
      }
}

Ensure that BaseCustomerViewModel has implemented INotifyPropertyChanged , something like that

 public class BaseCustomerViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged([CallerMemberName] String propertyName  "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

Answered By – ColeX – MSFT

Leave a Comment