gpt4 book ai didi

xamarin - ReactiveUI Xamarin iOS 路由

转载 作者:行者123 更新时间:2023-12-01 11:16:22 27 4
gpt4 key购买 nike

我开始在 Xamarin iOS 中尝试响应式 UI,我对应该如何处理路由感到困惑。

让我们以典型的“LoginViewModel”为例:

public class LoginViewModel : ReactiveObject
{
private readonly ReactiveCommand loginCommand;
public ReactiveCommand LoginCommand => this.loginCommand;

string username;
public string Username
{
get { return username; }
set { this.RaiseAndSetIfChanged(ref username, value); }
}

string password;
public string Password
{
get { return password; }
set { this.RaiseAndSetIfChanged(ref password, value); }
}

public LoginViewModel()
{
var canLogin = this.WhenAnyValue(
x => x.Username,
x => x.Password,
(u, p) => !string.IsNullOrEmpty(u) && !string.IsNullOrEmpty(p)
);

this.loginCommand = ReactiveCommand.CreateFromTask(async () =>
{
//Simulate login
await Task.Delay(2000);
return true;
}, canLogin);
}
}

和 ViewDidLoad( Controller ):

this.WhenActivated(d =>
{
this.Bind(this.ViewModel, x => x.Username, x => x.Username.Text).DisposeWith(d);
this.Bind(this.ViewModel, x => x.Password, x => x.Password.Text).DisposeWith(d);
this.BindCommand(this.ViewModel, x => x.LoginCommand, x => x.LoginButton).DisposeWith(d);
});

这有效地绑定(bind)了那些 UITextFields 和登录按钮启用状态的值 + OnTouchUpInside

现在,在德documentation你可以找到以下关于 vm-routing 的内容:Android, iOS Native: Very difficult to make work

那么我的选择是什么?

公开一个 DidLogIn 属性(bool)并监听(在 View 中):

this.WhenAnyValue(x => x.ViewModel.DidLogIn)
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(() => {
//Routing logic here
});

是否有其他方法来处理 View 路由(不是 vm 路由),我能找到的相关信息很少

最佳答案

ReactiveUI 在 Xamarin Forms 上的路由非常简单,Xamarin Android/iOS 是不同的历史,但你可以尝试 ReactiveUI 的交互,这里是一个例子:

public class LoginViewModel : ReactiveObject
{

private readonly Interaction<Unit, Unit> _navigate;
public Interaction<Unit, Unit> Navigate => Navigate;

public ReactiveCommand<Unit,bool> LoginCommand { get; set; }


public LoginViewModel()
{
var canLogin = this.WhenAnyValue(
x => x.Username,
x => x.Password,
(u, p) => !string.IsNullOrEmpty(u) && !string.IsNullOrEmpty(p));

_navigate = new Interaction<Unit, Unit>();

LoginCommand = ReactiveCommand.CreateFromTask<Unit, bool>(async _ =>
{
/*Logic here*/
return true;
}, canLogin);

LoginCommand.Subscribe(async result =>
{
if (result)//this logic gets executed on your view by registering a handler :D
await await _navigate.Handle(Unit.Default) ;
else
{}
});
}
}

所以在你看来

this.WhenActivated(disposables =>
{
//bindings...
//Register a handler:
ViewModel.Navigate.RegisterHandler(async interaction =>
{
await NavigationLogic();
interaction.SetOutput(Unit.Default);
}).DisposeWith(disposables);
});

该示例并不完美,但它是一种实现方式。

希望这对您有所帮助,您可以在以下位置找到有关交互的更多信息:https://reactiveui.net/docs/handbook/interactions/

在 Xamarin Android + ReactiveUI 中还有一个示例:https://github.com/bl8/ReactivePhoneword

问候

关于xamarin - ReactiveUI Xamarin iOS 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50527773/

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