如何使用共享模块构建多个 Prism Xamarin.Forms 应用程序

How to structure multiple Prism Xamarin.Forms apps with shared modules

我现在开始一个项目,我想使用棱镜模块。 想象一下这个场景:

项目应该如何构建?

"How your app" 的细节应该在架构上远远超出 Whosebug 上可以或应该回答的范围。但是,我将为您提供一些有关使用 Prism Modularity 的一般信息,我相信这些信息会有所帮助。

您列出的场景以 Uber Passenger 应用程序和 Uber Driver 应用程序为例,每个应用程序都使用相同的用户身份验证流程,这是拥有通用身份验证模块的绝佳用例您的组织能够跨应用重复使用。

您选择如何再次实施该模块超出了 can/should 此处回答的范围。一种可能的处理方法是包含一个自定义事件,例如:

public class UserAuthenticatedEvent : PubSubEvent<string> { }

从高层次上看,您的代码可能如下所示:

public class LoginPageViewModel : BindableBase
{
    private IEventAggregator _eventAggregator { get; }
    private IAuthService _authService { get; }

    public LoginPageViewModel(IEventAggregator eventAggregator, IAuthService authService)
    {
        _authService = authService;
        _eventAggregator = eventAggregator;
    }

    private string _userName;
    public string UserName
    {
        get => _userName;
        set => SetProperty(ref _userName, value);
    }

    private string _password;
    public string Password
    {
        get => _password;
        set => SetProperty(ref _password, value);
    }

    public DelegateCommand LoginCommand { get; }

    private async void OnLoginCommandExecuted()
    {
        var jwt = await _authService.LoginAsync(UserName, Password);
        _eventAggregator.GetEvent<UserAuthenticatedEvent>().Publish(jwt);
    }
}

public class App : PrismApplication
{
    protected override async void OnInitialized()
    {
        var ea = Container.Resolve<IEventAggregator>();
        ea.GetEvent<UserAuthenticatedEvent>().Subscribe(OnUserAuthencticated);
        await NavigationService.NavigateAsync("LoginPage");
    }

    protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
    {
        moduleCatalog.AddModule<AuthenticationModule>();
    }

    private async void OnUserAuthencticated(string jwt)
    {
        // store jwt for use
        await NavigationService.NavigateAsync("/MainPage");
    }
}

这允许您的用户拥有一个通用的登录流程,而您的应用程序控制发生的事情以及如何存储 jwt 以供重用。这当然可以进一步抽象出来......请记住,在你展示你的 Drivers 和 Passengers 的场景中,将有一个用户配置文件部分和设置,它们可以抽象成模块......无论一个模块是否可以跨应用程序重用,模块也很擅长将代码放入筒仓,通常更容易测试和维护。