A ReactiveUI navigation library for Xamarin.Forms
Перейти к файлу
renovate[bot] 38cf5c5298
chore(deps): update dependency microsoft.extensions.logging.debug to 8.0.1
2024-10-18 13:58:57 +00:00
.github Housekeeping Update ReactiveUI Version (#815) 2024-09-21 21:46:20 +01:00
Archive Housekeeping Retire Xamarin (#788) 2024-05-27 16:58:41 +10:00
Samples/SextantSample.Maui chore(deps): update dependency microsoft.extensions.logging.debug to 8.0.1 2024-10-18 13:58:57 +00:00
src chore(deps): update dependency microsoft.extensions.logging.debug to 8.0.1 2024-10-18 13:58:57 +00:00
.editorconfig Housekeeping Retire Xamarin (#788) 2024-05-27 16:58:41 +10:00
.gitattributes housekeeping: Update to match ReactiveUI Style and build system. (#36) 2019-03-16 23:27:19 -07:00
.gitignore housekeeping: Removed Resource.Designer.cs (#173) 2019-12-09 22:54:24 -06:00
LICENSE housekeeping: Update to match ReactiveUI Style and build system. (#36) 2019-03-16 23:27:19 -07:00
README.md update README (#601) 2022-02-12 07:00:11 +11:00
nuget.config fix: Fix Nullability Issues (#370) 2021-04-10 23:46:32 +10:00
version.json Housekeeping Retire Xamarin (#788) 2024-05-27 16:58:41 +10:00

README.md

Sextant

NuGet Stats Build Code Coverage

Sextant

A ReactiveUI view model based navigation library

Sextant was born from a fork of Xamvvm which is nice and simple MVVM Framework with a good navigation system. The problem is, I just wanted a simple navigation system to use with ReactiveUI without all the things that come along an MVVM framework. Plus, I wanted to make it more "Reactive Friendly".

Then a wild Rodney Littles appears, and with him an implementation of this AMAZING POST by Kent

Sextant is in a very initial stage and in constant change, so please be pantience with use... because we will break things.

This library is nothing more than me "standing on the shoulders of giants": Kent for been the original and true creator of this, I pretty much just copied and pasted it :) Geoffrey Huntley maintainer on ReactiveUI.

NuGet Installation

Install the nuget package on your Forms project and ViewModels project.

GitHub

Pre release packages are available at https://nuget.pkg.github.com/reactiveui/index.json

NuGet

Platform Sextant Package NuGet
UWP Sextant CoreBadge
Xamarin.Forms Sextant.XamForms XamBadge
Xamarin.Forms Sextant.Plugins.Popup PopupBadge
Xamarin.iOS Sextant CoreBadge
Avalonia Sextant.Avalonia CoreBadge

Target Platform Versions

Verify you have the minimum version for your target platform (i.e. Android, iOS, Tizen).

Register Components

Views

Version 2.0 added new extensions methods for the IMutableDepedencyResolver that allow you to register an IViewFor to a View Model.

Locator
    .CurrentMutable
    .RegisterNavigationView(() => new NavigationView(RxApp.MainThreadScheduler, RxApp.TaskpoolScheduler, ViewLocator.Current))
    .RegisterParameterViewStackService()
    .RegisterViewForNavigation(() => new PassPage(), () => new PassViewModel())
    .RegisterViewForNavigation(() => new ReceivedPage(), () => new ReceivedViewModel());

Set the initial page:

Locator
    .Current
    .GetService<IParameterViewStackService>()
    .PushPage<PassViewModel>()
    .Subscribe();

MainPage = Locator.Current.GetNavigationView("NavigationView");

Use the Navigation Service

After that all you have to do is call one of the methods inside your ViewModels:

/// <summary>
/// Pops the <see cref="IPageViewModel"/> off the stack.
/// </summary>
/// <param name="animate">if set to <c>true</c> [animate].</param>
/// <returns></returns>
IObservable<Unit> PopModal(bool animate = true);

/// <summary>
/// Pops the <see cref="IPageViewModel"/> off the stack.
/// </summary>
/// <param name="animate">if set to <c>true</c> [animate].</param>
/// <returns></returns>
IObservable<Unit> PopPage(bool animate = true);

/// <summary>
/// Pushes the <see cref="IPageViewModel"/> onto the stack.
/// </summary>
/// <param name="modal">The modal.</param>
/// <param name="contract">The contract.</param>
/// <returns></returns>
IObservable<Unit> PushModal(IPageViewModel modal, string contract = null);

/// <summary>
/// Pushes the <see cref="IPageViewModel"/> onto the stack.
/// </summary>
/// <param name="page">The page.</param>
/// <param name="contract">The contract.</param>
/// <param name="resetStack">if set to <c>true</c> [reset stack].</param>
/// <param name="animate">if set to <c>true</c> [animate].</param>
/// <returns></returns>
IObservable<Unit> PushPage(IPageViewModel page, string contract = null, bool resetStack = false, bool animate = true);

Example

public class ViewModel
{
    private readonly IViewStackServicen _viewStackService; // or IParameterViewStackServicen

    public ViewModel(IViewStackServicen viewStackService)
    {
        _viewStackService = viewStackService;

        OpenModal = ReactiveCommand
            // FirstModalViewModel must implement IViewModel or INavigable
            .CreateFromObservable(() => viewStackService.PushModal<FirstModalViewModel>(),
                outputScheduler: RxApp.MainThreadScheduler);
    }

    public ReactiveCommand<Unit, Unit> OpenModal { get; }
}

Pass Parameters

Version 2.0 added support for passing parameters when navigating.

Example

public class ViewModel
{
    private readonly IParameterViewStackServicen _viewStackService;

    public ViewModel(IParameterViewStackServicen viewStackService)
    {
        _viewStackService = viewStackService;

        Navigate = ReactiveCommand
            // NavigableViewModel must implement INavigable
            .CreateFromObservable(() => viewStackService.PushModal<NavigableViewModel>(new NavigationParameter { { "parameter", parameter } }),
                outputScheduler: RxApp.MainThreadScheduler);
    }

    public ReactiveCommand<Unit, Unit> Navigate { get; }
}

The INavigable interface exposes view model lifecycle methods that can be subscribed to. These methods unbox your parameter object. Implementing the interface allows you to assign values to the View Model during Navigation.

public class NavigableViewModel : INavigable
{
        public string? _parameter;

        public IObservable<Unit> WhenNavigatedFrom(INavigationParameter parameter)
        {
            return Observable.Return(Unit.Default)
        }

        public IObservable<Unit> WhenNavigatedTo(INavigationParameter parameter)
        {
            parameter.TryGetValue("parameter", out _parameter);
            return Observable.Return(Unit.Default);
        }

        public IObservable<Unit> WhenNavigatingTo(INavigationParameter parameter)
        {
            return Observable.Return(Unit.Default);
        }
}

Samples

Contribute

Sextant is developed under an OSI-approved open source license, making it freely usable and distributable, even for commercial use. We ❤ the people who are involved in this project, and wed love to have you on board, especially if you are just getting started or have never contributed to open-source before.

So here's to you, lovely person who wants to join us — this is how you can support us: