【发布时间】:2018-07-04 17:54:58
【问题描述】:
我有 LoginViewController 类
// This file has been autogenerated from a class added in the UI designer.
using System;
using System.Collections.Generic;
using Foundation;
using UIKit;
using System.Linq;
using System.Reactive;
using ReactiveUI;
using GoBatumi.IOS.ViewModels;
namespace GoBatumi.IOS
{
public partial class LogInViewController : ReactiveViewController,IViewFor<LoginViewModel>
{
UITapGestureRecognizer SingUpGesture;
public LogInViewController (IntPtr handle) : base (handle)
{
this.WhenActivated(d =>
{
d(this.Bind(ViewModel, vm => vm.UserName, vm => vm.userNameTextField.Text));
d(this.Bind(ViewModel, vm => vm.Password, vm => vm.passwordTextField.Text));
d(this.Bind(ViewModel, vm => vm.LoginButton, vm => vm.logInButton));
d(this.Bind(ViewModel, vm => vm.PasswordTextField, vm => vm.passwordTextField));
});
}
private void MakeViewModelBinding(){
}
public override void ViewDidLayoutSubviews(){
base.ViewWillLayoutSubviews();
}
LoginViewModel _viewModel;
public LoginViewModel ViewModel
{
get => _viewModel ?? new LoginViewModel();
set => _viewModel = value;
}
object IViewFor.ViewModel
{
get => ViewModel;
set => ViewModel = (LoginViewModel)value;
}
public override void ViewDidLoad(){
base.ViewDidLoad();
}
private void ShouldChangeViewSettings(bool enable){
passwordTextField.Enabled = enable;
logInButton.Enabled = enable;
if (enable)
logInButton.Alpha = 0.99f;
else
logInButton.Alpha = 0.4f;
}
}
public class TestUser
{
public string UserName{
get;
set;
}
public string Password{
get;
set;
}
}
}
另外,我有 LoginViewModel 类
using System;
using System.Diagnostics;
using ReactiveUI;
using UIKit;
namespace GoBatumi.IOS.ViewModels
{
public class LoginViewModel : ReactiveObject
{
public LoginViewModel()
{
}
private string _userName;
public string UserName
{
get => _userName;
set
{
this.RaiseAndSetIfChanged(ref _userName, value);
var result = string.IsNullOrEmpty(_userName);
ShouldChangeViewSettings(result);
}
}
private string _password;
public string Password
{
get => _password;
set
{
this.RaiseAndSetIfChanged(ref _password, value);
}
}
private UIButton _loginButton;
public UIButton LoginButton
{
get => _loginButton;
set => this.RaiseAndSetIfChanged(ref _loginButton, value);
}
private UITextField _passwordTextField;
public UITextField PasswordTextField
{
get => _passwordTextField;
set => this.RaiseAndSetIfChanged(ref _passwordTextField,
}
}
}
我的问题是字符串用户名和字符串密码可以绑定, usernameTextField.Texts 和 passwordTextField.Text
但它 UIButton 和 UITextField 始终为 null,它们没有绑定。
我的任务是,每当用户在 textField 中键入字符时,我必须启用按钮,并且每当用户删除整个文本字段并且字符串为空时,我必须再次禁用按钮,因此我需要 UiButton 来更改背景颜色从 ViewModel 但 UIButtonProperty 总是返回 null。
问题出在哪里?
如果有人能给我一些建议,我会很高兴。 我对 MVVM 和 ReactiveUi 有点陌生。
谢谢。
【问题讨论】:
-
您的 ViewModel 上不需要 UITextField 和 UIButton。您需要一个 ReactiveCommand,ReactiveCommand 定义了一个名为 CanExecute 的 IObservable
并通过将其与 WhenAnyValue 运算符结合,您可以实现目标 -
但是我怎样才能从 viewmodel 到 ViewController 的对象,比如那些 button 和 uitextfield 呢?你能写一个小例子吗?
标签: c# xamarin xamarin.ios reactive-programming reactiveui