Skip to content

Extends the Xamarin.Forms.NavigationPage. Easily register navigation actions and automatically dispose views and viewmodels.

License

Notifications You must be signed in to change notification settings

manuelmeisen/Xamarin-Autonavigation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Xamarin-Autonavigation

Register NavigationActions and automatically dispose any IDisposable Page/BindingContext with AutoNavigationPage. Extends the NavigationPage and is completely compatible with it's standard functionality.

Getting started

Install

Xamarin.Forms (.NET Standard 2.0)

Nuget

Quick setup

The quick and dirty way. For a clean MVVM version, look here.

1) Create the AutoNavigationPage

public App()
{
    InitializeComponent();
    var autoNavigation = new AutoNavigationPage(new MainPage());
    MainPage = autoNavigation;
}
References:

2) Register NavigationActions

public App()
{
    InitializeComponent();
    var autoNavigation = new AutoNavigationPage(new Page());
    MainPage = autoNavigation;

    autoNavigation.RegisterAction("back")
        .PopMultiple(2);

    autoNavigation.RegisterAction("backToSelect")
        .PopUnto<SelectionPage>();
}
References:

2) Use them in your Page and BindingContext

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;
    }
}
References:

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.

Drawbacks

  • Does not support multiple AutoNavigationPages
  • So far, no extension functionality for the ModalStack (but planned). You can still use the standard functionality.

How to

About

Extends the Xamarin.Forms.NavigationPage. Easily register navigation actions and automatically dispose views and viewmodels.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages