ReactiveUI Xamarin iOS 路由
ReactiveUI Xamarin iOS routing
我开始在 Xamarin iOS 中尝试 Reactive 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(控制器):
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);
});
这有效地绑定了那些 UITextFields 和登录按钮启用状态的值 + OnTouchUpInside
现在,在 de documentation 中,您可以找到以下有关 vm-routing 的信息:Android、iOS Native:非常难以工作
那么我的选择是什么?
公开一个 DidLogIn 属性 (bool) 并听取(在视图中):
this.WhenAnyValue(x => x.ViewModel.DidLogIn)
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(() => {
//Routing logic here
});
是否有其他方法来处理视图路由(不是 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 iOS 中尝试 Reactive 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(控制器):
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);
});
这有效地绑定了那些 UITextFields 和登录按钮启用状态的值 + OnTouchUpInside
现在,在 de documentation 中,您可以找到以下有关 vm-routing 的信息:Android、iOS Native:非常难以工作
那么我的选择是什么?
公开一个 DidLogIn 属性 (bool) 并听取(在视图中):
this.WhenAnyValue(x => x.ViewModel.DidLogIn)
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(() => {
//Routing logic here
});
是否有其他方法来处理视图路由(不是 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
此致