Register NavigationActions and automatically dispose any IDisposable Page/BindingContext with AutoNavigationPage. Extends the NavigationPage and is completely compatible with it's standard functionality.
Xamarin.Forms (.NET Standard 2.0)
The quick and dirty way. For a clean MVVM version, look here.
public App()
{
InitializeComponent();
var autoNavigation = new AutoNavigationPage(new MainPage());
MainPage = autoNavigation;
}
public App()
{
InitializeComponent();
var autoNavigation = new AutoNavigationPage(new Page());
MainPage = autoNavigation;
autoNavigation.RegisterAction("back")
.PopMultiple(2);
autoNavigation.RegisterAction("backToSelect")
.PopUnto<SelectionPage>();
}
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new MainViewModel();
}
}
public class MainViewModel : IRequestNavigation, INotifyPropertyChanged, IDisposable
{
public event RequestNavigationEventHandler RequestNavigation;
public event PropertyChangedEventHandler PropertyChanged;
public ICommand GoBack
{
get
{
return new Command(async() =>{
await RequestNavigation?.Invoke(this,new RequestNavigationEventArgs("back"));
});
}
}
public void Dispose()
{
PropertyChanged = null;
}
}
If the MainPage is popped off the navigation stack, the MainViewModel will be disposed. The Page itself doesn't implement IDisposable, but Pages that do will also be disposed. If the ICommand GoBack is executed, 2 pages will be popped off the navigation stack.
- Does not support multiple AutoNavigationPages
- So far, no extension functionality for the ModalStack (but planned). You can still use the standard functionality.
- Clean MVVM implementation
- Available NavigationActions
- Push pages with arguments
- Buffer pages before pushing (reference coming soon)
- Register NavigationActions through Container (reference coming soon)