I am working on xamarin.form cross-platform application , i want to navigate from one page to another on button click. As i cannot do Navigation.PushAsync(new Page2());
decided to add two ways to pass Page instance to viewmodel which you can use later for navigation, displaying alerts. closing page and so on.
1. if you can pass it with command parameter
in view model:
public ICommand cmdAddRecord { get; set; }
viewmodel constructor
cmdAddRecord = new Command(AddRecord);
somewhere in viewmodel
void AddRecord(ContentPage parent)
{
parent.Navigation.Whatever
}
XAML
header
x:Name="thisPage"
usage
2. started using this in my base class for viewmodels
viewmodel
public class cMyBaseVm : BindableObject
...
public static BindableProperty ParentProperty = BindableProperty.Create("Parent", typeof(ContentPage), typeof(cMyBaseVm), null, BindingMode.OneWay);
...
public ContentPage Parent
{
get => (ContentPage)GetValue(ParentProperty);
set => SetValue(ParentProperty, value);
}
XAML
xmlns:viewModels="clr-namespace:yournamespace.ViewModels"
x:Name="thisPage"
and here we go
child viewmodel
public class cPetEventsListVm : cMyBaseVm
and now, all around child view model we can use Page like Parent.DisplayAlert , or Parent.Navigation.PushAsync etc we may even close page now from view model with Parent.PopAsync ();