question

SamirAlghazaly-5379 avatar image
0 Votes"
SamirAlghazaly-5379 asked SamirAlghazaly-5379 commented

Profile Page Not Binding Even Data loaded from API

I am new in Xamarin forms
and I am trying to build an app to load data from API and do some operations on this data
when I built the mainpage for showing profile data ,data already loaded from API but not bonded in the view
although when loading a list of items in a listview in the same view or another on it's bounded successfully
I am using MVVM

dotnet-xamarin
· 8
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.

@SamirAlghazaly-5379 It is difficult to find the cause of the problem without code. Can you provide a simple demo? For example, your View and ViewModel files. Note: If you are binding "property", make sure you have implemented interface INotifyPropertyChanged.

 class MainPageViewModel : INotifyPropertyChanged
 {
     // ...
     Student selectedStudent;
     public Student SelectedStudent
     {
         get => selectedStudent;
         set
         {
             selectedStudent = value;
             OnPropertyChanged("SelectedStudent");
         }
     }
    
     public event PropertyChangedEventHandler PropertyChanged;
    
     protected void OnPropertyChanged(string propertyName)
     {
         var handler = PropertyChanged;
         if (handler != null)
             handler(this, new PropertyChangedEventArgs(propertyName));
     }
 }

If you are binding a list, make sure using ObservableCollection.

 public ObservableCollection<Student> Students { get; set; }
0 Votes 0 ·

@KyleXu-MSFT Thanks alot for your kindly reply following are code parts

for ViewModel

  public class ProfileViewModel : BaseViewModel
     {
    
         public class ProfileViewModel : BaseViewModel
         {
             VwStudent _StudenProfileData = new VwStudent();
             public VwStudent StudenProfileData
             {
                 get
                 {
                     return _StudenProfileData;
                 }
                 set
                 {
                     SetProperty(ref _StudenProfileData, value);
                 }
             }
    
             public ICommand LoadProfileCommand { get; }
    
             public ProfileViewModel()
             {
                 LoadProfileCommand = new Command(async () => await GetStudentProfileData());
             }
             public async Task GetStudentProfileData()
             {
                 IsBusy = true;
                    
                 _StudenProfileData = (await new VwStudentService().GetByStudentId(150)).FirstOrDefault();
    
                 SetProperty(ref _StudenProfileData, StudenProfileData);
    
    
                 IsBusy = false;
    
    
             }
    
         }
     }


0 Votes 0 ·

@SamirAlghazaly-5379 When you modify a field, it won't trigger "PropertyChanged". So modify the following code

 _StudenProfileData = (await new VwStudentService().GetByStudentId(150)).FirstOrDefault();

to

 StudenProfileData = (await new VwStudentService().GetByStudentId(150)).FirstOrDefault();
0 Votes 0 ·
Show more comments

and for the ContetntPage


 <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              x:Class="BeneficiarySystem.Views.ProfilePage" 
                 
              >
        
        
     <ContentPage.Content>
         <StackLayout>
             <Label Text="{Binding StudenProfileData.StudentName}"
                 VerticalOptions="CenterAndExpand" 
                 HorizontalOptions="CenterAndExpand" />
         </StackLayout>
     </ContentPage.Content>
 </ContentPage>


for code behind

  public partial class ProfilePage : ContentPage
     {
         public ProfilePage()
         {
             InitializeComponent();
            BindingContext = new ProfileViewModel();
         }
    
         protected override void OnAppearing()
         {
             (BindingContext as ProfileViewModel).LoadProfileCommand.Execute(null);
               
             base.OnAppearing();
         }
     }


0 Votes 0 ·

and for the BaseViewModel Class as follow

  public class BaseViewModel : INotifyPropertyChanged
     {
         public IDataStore<Item> DataStore => DependencyService.Get<IDataStore<Item>>();
    
         bool isBusy = false;
         public bool IsBusy
         {
             get { return isBusy; }
             set { SetProperty(ref isBusy, value); }
         }
    
         string title = string.Empty;
         public string Title
         {
             get { return title; }
             set { SetProperty(ref title, value); }
         }
    
         protected bool SetProperty<T>(ref T backingStore, T value,
             [CallerMemberName] string propertyName = "",
             Action onChanged = null)
         {
             if (EqualityComparer<T>.Default.Equals(backingStore, value))
                 return false;
    
             backingStore = value;
             onChanged?.Invoke();
             OnPropertyChanged(propertyName);
             return true;
         }
    
         #region INotifyPropertyChanged
         public event PropertyChangedEventHandler PropertyChanged;
         protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
         {
             var changed = PropertyChanged;
             if (changed == null)
                 return;
    
             changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
         }
         #endregion
     }
0 Votes 0 ·

0 Answers