【MAUI】Sextant

发布于:2024-08-22 ⋅ 阅读:(18) ⋅ 点赞:(0)


在.NET MAUI(.NET Multi-platform App UI)的上下文中,Sextant 是一个用于处理导航的库,特别是针对 Xamarin.Forms 和 MAUI 应用程序。尽管 MAUI 提供了自己的导航机制,但 Sextant 提供了一种更加灵活和声明式的方式来定义和管理页面之间的导航。

Sextant 的特点

  1. 声明式导航:你可以使用 Sextant 来定义应用程序中的导航路由,并将这些路由与特定的页面类型相关联。这使得导航逻辑与视图代码分离,从而提高了代码的可维护性和可读性。

  2. 跨平台支持:Sextant 设计为与 Xamarin.Forms 和 MAUI 兼容,因此它可以在这些平台上工作,为你提供一致的导航体验。

  3. 灵活的导航参数:Sextant 允许你在导航时传递参数,这些参数可以是简单的数据类型,也可以是复杂的对象。这有助于在不同页面之间共享数据和状态。

  4. 易于集成:将 Sextant 集成到你的 MAUI 或 Xamarin.Forms 应用程序中通常相对简单,只需几个步骤即可完成。

如何使用 Sextant

  1. 安装 Sextant:首先,你需要在你的 MAUI 或 Xamarin.Forms 项目中安装 Sextant NuGet 包。

  2. 配置导航:在你的应用程序中配置 Sextant 导航。这通常涉及到定义一个或多个导航容器(如 INavigationService),并注册你的页面类型及其对应的路由。

  3. 使用导航:在你的应用程序代码中,你可以使用 Sextant 提供的 API 来导航到不同的页面,并传递必要的参数。

注意事项

  • 版本兼容性:确保你安装的 Sextant 版本与你的 MAUI 或 Xamarin.Forms 版本兼容。
  • 文档和社区支持:查阅 Sextant 的官方文档和社区论坛,以获取更多关于如何使用和故障排除的信息。
  • 与 MAUI 导航的对比:虽然 MAUI 提供了自己的导航机制,但 Sextant 提供了额外的灵活性和功能。在决定使用哪种导航方式时,请考虑你的应用程序的具体需求和目标。

Sextant 是一个有用的库,可以帮助你更轻松地管理 MAUI 或 Xamarin.Forms 应用程序中的导航。然而,它并不是 MAUI 框架的官方部分,而是一个由社区开发的第三方库。因此,在使用它之前,请确保你了解它的工作原理,并准备好处理可能出现的任何问题。

官方示例

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);
        }
}

网站公告

今日签到

点亮在社区的每一天
去签到