Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/TinyStuff/TinyNavigationHelper
TinyNavigationHelper is a library that is created for you that want to abstract the navigation without installing a bigger MVVM framework.
https://github.com/TinyStuff/TinyNavigationHelper
Last synced: 30 days ago
JSON representation
TinyNavigationHelper is a library that is created for you that want to abstract the navigation without installing a bigger MVVM framework.
- Host: GitHub
- URL: https://github.com/TinyStuff/TinyNavigationHelper
- Owner: TinyStuff
- License: mit
- Created: 2017-08-17T16:00:50.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-04-04T16:23:05.000Z (over 3 years ago)
- Last Synced: 2024-11-05T14:59:36.434Z (about 1 month ago)
- Language: C#
- Homepage:
- Size: 159 KB
- Stars: 17
- Watchers: 6
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-xamarin-forms - TinyNavigationHelper ★15
README
# 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
```cs
// Option 1:
var navigationHelper = new NavigationHelper(this);
navigationHelper.RegisterView("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.```cs
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterInstance(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.```cs
var navigationHelper = NavigationHelper.Current;
```### Navigate
To navigate to a page use the NavigateTo method and the key you registered for the page.```cs
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.```cs
await navigationHelper.NavigateToAsync("MainView", "Parameter");
```
If your view has a NavigationParameter property it will be set.```cs
public class MainView
{
public object NavigationParameter {get;set;}public MainView()
{
}
}
```Otherwise the parameter will be sent to the constructor.
```cs
public class MainView
{
public MainView(object parameter)
{
var data = parameter as string;
}
}
```
#### Navigate back
To navigate back, use the Back method.```cs
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`.
```cs
// 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.
```cs
await navigationHelper.OpenModalAsync("MainView");
```
You can send a parameter to a modal in the same way as with the NavigateTo method.```cs
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.
```cs
//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.```cs
//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.```cs
public class ViewConstants
{
public const string MainView = "MainView";
}
``````cs
var navigationHelper = new NavigationHelper(this);
navigationHelper.RegisterView(ViewConstants.MainView);
``````cs
await navigationHelper.NavigateToAsync(ViewConstants.MainView);
```