gpt4 book ai didi

c# - ReactiveUI加载时执行命令

转载 作者:行者123 更新时间:2023-12-03 10:28:07 24 4
gpt4 key购买 nike

我正在尝试实现在应用程序检查用户是否通过身份验证时将显示的 View / View 模型。

下面的代码以对路由器的多次调用结束。 InternalIsAuthenticated中的测试代码是否有问题?我该如何正确实现?

ViewModel:

public class LoadingViewModel : ReactiveObject, IRoutableViewModel
{
public string UrlPathSegment {
get {
return "Loading";
}
}

public IScreen HostScreen { get; protected set; }

public ReactiveCommand<bool> IsAuthenticatedCommand { get; protected set; }

public LoadingViewModel (IScreen screen)
{
HostScreen = screen;
IsAuthenticatedCommand = ReactiveCommand.CreateAsyncTask (async (_, ctx) => await InternalIsAuthenticated ());
IsAuthenticatedCommand.Subscribe(next => {
//called twice. ??
HostScreen.Router.NavigateAndReset.Execute(new OnboardingViewModel(HostScreen));
});
IsAuthenticatedCommand.ThrownExceptions.Subscribe(ex => {
Debug.WriteLine(ex.Message);
UserError.Throw("oops");
});

//Error: nested push animation can result in corrupted navigation bar
//Error: Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
//this.WhenAnyValue(x => x.UrlPathSegment)
// .Throttle(TimeSpan.FromSeconds(1), RxApp.MainThreadScheduler)
// .InvokeCommand(this, x => x.IsAuthenticatedCommand);

//Error: nested push animation can result in corrupted navigation bar
//Error: Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
this.IsAuthenticatedCommand.ExecuteAsyncTask ();

//OnboardingView ends on top of LoadingView in the navigation stack.
//HostScreen.Router.NavigateAndReset.Execute(new OnboardingViewModel(HostScreen));
}

private async Task<bool> InternalIsAuthenticated(){
return await Task.Run(() => {return true;});
}
}

View :
public partial class LoadingView  : ContentPage, IViewFor<LoadingViewModel>
{
public LoadingView ()
{
InitializeComponent ();
this.WhenAnyValue (x => x.ViewModel).BindTo (this, x => x.BindingContext);
}

public LoadingViewModel ViewModel {
get { return (LoadingViewModel)GetValue(ViewModelProperty); }
set { SetValue(ViewModelProperty, value); }
}
public static readonly BindableProperty ViewModelProperty =
BindableProperty.Create<LoadingView, LoadingViewModel>(x => x.ViewModel, default(LoadingViewModel), BindingMode.OneWay);

object IViewFor.ViewModel {
get { return ViewModel; }
set { ViewModel = (LoadingViewModel)value; }
}
}

最佳答案

如果NavigateAndReset被调用两次,可能是因为您要创建两个LoadingViewModels?

关于c# - ReactiveUI加载时执行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27630616/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com