在 Xamarin 中的"大纲详细信息"视图之前登录屏幕



我正在尝试编写一个 xamarin 应用程序,该应用程序将在母版详细信息页面之前显示登录页面,但我遇到了问题。 现在我让我的 app.xaml 调用一个 appbootstrapper,如下所示:

public App()
{
this.InitializeComponent();
RxApp.SuspensionHost.CreateNewAppState = () => new AppBootstrapper();
RxApp.SuspensionHost.SetupDefaultSuspendResume();
this.MainPage = RxApp.SuspensionHost
.GetAppState<AppBootstrapper>()
.CreateMainPage();
}
/// <summary>Gets the Router associated with this Screen.</summary>
public RoutingState Router { get; } = new RoutingState();

使用应用引导程序,如下所示:

public class AppBootstrapper : ReactiveObject, IScreen
{
public AppBootstrapper(IMutableDependencyResolver dependencyResolver = null)
{
SetupLogging();
this.RegisterParts(dependencyResolver ?? Locator.CurrentMutable);
this.Router.Navigate.Execute(new LoginPageViewModel(this));
}
/// <summary>Gets the Router associated with this Screen.</summary>
public RoutingState Router { get; } = new RoutingState();
public Page CreateMainPage()
{
return new RoutedViewHost();
}
private static void SetupLogging()
{
var logger = new Logger { Level = LogLevel.Debug };
Locator.CurrentMutable.RegisterConstant(logger, typeof(ILogger));
}
private void RegisterParts(IMutableDependencyResolver dependencyResolver)
{
dependencyResolver.RegisterConstant(this, typeof(IScreen));
dependencyResolver.Register(() => new LoginPage(), typeof(IViewFor<LoginPageViewModel>));
dependencyResolver.RegisterConstant(new LoginService(), typeof(ILoginService));
}
}

这使我进入登录屏幕没有问题,我可以执行登录操作。然后,一旦登录成功,我尝试导航到母版详细信息页面,但这是我遇到问题的地方。

public LoginPageViewModel(IScreen screen)
{
this.loginService = Locator.Current.GetService<ILoginService>();
this.HostScreen = screen ?? Locator.Current.GetService<IScreen>();
this.PrepareObservables();
}
........................................................
private void PrepareObservables()
{
...
this.LoginCommand = ReactiveCommand.CreateFromTask(
async execute =>
{
var loginSuccessful = await this.loginService.Login(this.Username, this.Password);
if (loginSuccessful)
{
this.HostScreen.Router.NavigateBack.Execute().Subscribe();
}
}, canExecuteLogin);
...

您可以看到我的登录命令正在尝试执行导航和重置以转到主页(这是我的母版详细信息页面(。这不起作用,并导致未处理的异常,指出:

实现 IHandleObservableErrors 的对象出错,从而中断了其可观察管道。为了防止这种情况,>...

有谁知道该怎么做?我需要一种良好的模式来处理使用 ReactiveUI 的 Xamarin 窗体中的登录 -> 主详细信息页面的用例。谢谢。

this.LoginCommand = ReactiveCommand.CreateFromTask(
async execute =>
{
var loginSuccessful = await this.loginService.Login(this.Username, this.Password);
if (loginSuccessful)
{
this.HostScreen.Router.NavigateBack.Execute().Subscribe();
}
}, canExecuteLogin);

上面的代码在成功登录后导航回来。 我想你的意思是使用Router.NavigateAndReset.Execute(new MainPageViewModel()).Subscribe();

相关内容

  • 没有找到相关文章

最新更新