Hola estoy usando Xamarin Forms Mvvm y en mi view model tengo un methodo que me gustaria llamar cuando mi vista se cree, Alguien sabe la forma correcta de hacer esto? Entonces lo que tengo es lo siguiente:
public partial class StudentsPage : ContentPage
{
public StudentsPage()
{
InitializeComponent();
BindingContext = new StudentsViewModel
(
RestService.For<IJMServices>("https://miappi.herokuapp.com")
);
}
protected override void OnAppearing()
{
base.OnAppearing();
}
}
y este es mi view model donde quiero ejecutar LoadStudents
public class StudentsViewModel : INotifyPropertyChanged
{
private string _id;
public string Id
{
get
{
return _id;
}
set
{
_id = value;
OnPropertyChanged("Id");
}
}
public ObservableCollection<StudentData> students { get; set; }
private IJMServices _iJMService { get; set; }
public StudentsViewModel(IJMServices JMServices)
{
Id = Preferences.Get("Id", string.Empty);
_iJMService = JMServices;
students = new ObservableCollection<StudentData>();
// _ = LoadStudents();
}
private async Task LoadStudents()
{
UserDialogs.Instance.ShowLoading();
var st = await _iJMService.GetStudents(Id);
students.Clear();
foreach (StudentData s in st)
{
students.Add(s);
}
UserDialogs.Instance.HideLoading();
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}