TinyStuff / TinyNavigationHelper

TinyNavigationHelper is a library that is created for you that want to abstract the navigation without installing a bigger MVVM framework.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build status

Build status

TinyNavigationHelper

TinyNavigationHelper is a library that is created for you that want to abstract the navigation without installing a bigger MVVM framework.

Today there are only an implementation for Xamarin.Forms, but it created in a way that it will be possible to create implementatons for other platforms as well.

How to install

For your Xamarin.Forms project install the package from NuGet:

Install-Package TinyNavigationHelper.Forms

If you want to use navigation from a project that not has references to Xamarin.Forms (for example if you have your ViewModels in a separete project for use on other platforms), install the abstraction package for that project.

Install-Package TinyNavigationHelper.Abstraction

How to configure navigation for Xamarin.Forms

// Option 1:
var navigationHelper = new NavigationHelper(this);
navigationHelper.RegisterView<MainView>("MainView");
		
// Option 2: Register single views		
var navigationHelper = new FormsNavigationHelper(this);		
navigationHelper.RegisterView("MainView", typeof(MainView));		
		
// Option 3: Register all views (pages) that is inherited from Page		
// The class name will be the key. To use this, you need to add using System.Reflection;		
var asm = typeof(App).GetTypeInfo().Assembly;		
navigationHelper.RegisterViewsInAssembly(asm);

If you want to use it with an IoC instance you need to register the specific instance in the IoC container. The example below is how to register in Autofac, but you can use the container you prefer.

var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterInstance<INavigationHelper>(navigationHelper);

var container = containerBuilder.Build();

How to use TinyNavigationHelper

There are two ways to use the navigation helper. To resolve the INavigationHelper interface via foreample constructor inbjection, or to access it via the Current property on the NavigationHelper class in the TinyNavigationHelper.Abstraction namespace.

var navigationHelper = NavigationHelper.Current;

Navigate

To navigate to a page use the NavigateTo method and the key you registered for the page.

await navigationHelper.NavigateToAsync("MainView");

Navigate with parameter

The NavigateTo method can take a argument if you want to pass a data to the view you're navigating to.

await navigationHelper.NavigateToAsync("MainView", "Parameter");

If your view has a NavigationParameter property it will be set.

public class MainView
{
     public object NavigationParameter {get;set;}

     public MainView()
     {
          
     }
}

Otherwise the parameter will be sent to the constructor.

public class MainView
{
     public MainView(object parameter)
     {
          var data = parameter as string;
     }
}

Navigate back

To navigate back, use the Back method.

await navigationHelper.BackAsync();

Forms specific extensions

On Xamarin Forms there are a set of extension methods that allows for navigation to an instantiated page. Note that this will only work on the project that references TinyNavigationHelper.Forms.

WARNING: This will only work in Xamarin Forms since it references Xamarin.Forms.

// Step 1 - Add a using to reference the extension method
using TinyNavigationHelper.Forms;

// Step 2 - Navigate
var page = new NewsView();
await NavigationHelper.Current.NavigateToAsync(page);

Modal

To open a modal, use the OpenModal method.

await navigationHelper.OpenModalAsync("MainView");

You can send a parameter to a modal in the same way as with the NavigateTo method.

await navigationHelper.OpenModalAsync("MainView", "parameter");

If you want the modal to have it's own navigation stack you can pass pass true the withNavigation argument.

//without parameter
await navigationHelper.OpenModalAsync("MainView", true);

//with parameter
await navigationHelper.OpenModalAsync("MainView", "parameter", true);

Set as root page

If you want to navigate to a page and reset the history in the navigation stack you can use the SetRootView method.

//without parameter and navigation stack
navigationHelper.SetRootView("MainView");

//with parameter, but without navigation stack
navigationHelper.SetRootView("MainView", "parameter");

//without parameter, but with navigation stack
navigationHelper.SetRootView("MainView", true);

//with parameter and navigation stack
navigationHelper.SetRootView("MainView", "parameter", true);

Handle view keys

The recommendation is to not use the string directly in your code, but instead create a class with view constants.

public class ViewConstants
{
     public const string MainView = "MainView";
}
var navigationHelper = new NavigationHelper(this);
navigationHelper.RegisterView<MainView>(ViewConstants.MainView);
await navigationHelper.NavigateToAsync(ViewConstants.MainView);

About

TinyNavigationHelper is a library that is created for you that want to abstract the navigation without installing a bigger MVVM framework.

License:MIT License


Languages

Language:C# 100.0%