I have a Xamarin.Forms application with freshmvvm framework. According to the documentation, I can use PreviousPageModel property of FreshBasePageModel base class to access data of the PageModel I navigated from. I navigate like this:
public FirstPageModel()
{
_validator = new CalculatorValidator();
CalculateCommand = new Command(execute: () =>
{
ValidationResult = _validator.Validate(this);
RaisePropertyChanged(nameof(ValidationResult));
if (ValidationResult.IsValid)
{
CoreMethods.PushPageModel<SecondPageModel>();
}
});
}
The navigation happens, but in the SecondPageModel constructor the PreviousPageModel is null:
public SecondPageModel()
{
_previousModel = (FirstPageModel)PreviousPageModel;
}
What am I doing wrong?
Thank you.
EDIT:
I also tried:
public FirstPageModel()
{
_validator = new CalculatorValidator();
CalculateCommand = new Command(Calculate);
}
private void Calculate()
{
ValidationResult = _validator.Validate(this);
RaisePropertyChanged(nameof(ValidationResult));
if(ValidationResult.IsValid)
{
CoreMethods.PushPageModel<SecondPageModel>(this);
}
}
I got the answer here:
https://forums.xamarin.com/discussion/comment/365262/#Comment_365262
The PreviousPageModel is null because it hasn't been set in the constructor. Place your code in the ViewIsAppearing lifecycle event, then you will get the correct previous model:
protected override void ViewIsAppearing(object sender, EventArgs e)
{
base.ViewIsAppearing(sender, e);
_previousModel = (FirstPageModel)PreviousPageModel;
}