我正在尝试在mvvmcross中使用UIActivityIndicatorView的自定义绑定,但我无法使其正常工作。 我遵循了建议here。所以我创建了自定义绑定类:
public class ActivityIndicatorViewHiddenTargetBinding : MvxConvertingTargetBinding
{
public ActivityIndicatorViewHiddenTargetBinding(UIActivityIndicatorView target)
: base(target)
{
if (target == null)
{
MvxBindingTrace.Trace(
MvxTraceLevel.Error,
"Error - UIActivityIndicatorView is null in ActivityIndicatorViewHiddenTargetBinding");
}
}
public override MvxBindingMode DefaultMode
{
get { return MvxBindingMode.OneWay; }
}
public override System.Type TargetType
{
get { return typeof(bool); }
}
protected UIActivityIndicatorView View
{
get { return Target as UIActivityIndicatorView; }
}
protected override void SetValueImpl(object target, object value)
{
var view = (UIActivityIndicatorView)target;
if (view == null)
{
return;
}
view.Hidden = (bool)value;
if (view.Hidden)
{
view.StopAnimating();
}
else
{
view.StartAnimating();
}
}
}
我试着注册。
public class Setup : MvxIosSetup
{
// ...
protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
registry.RegisterPropertyInfoBindingFactory(
typeof(ActivityIndicatorViewHiddenTargetBinding),
typeof(UIActivityIndicatorView), "Hidden");
base.FillTargetFactories(registry);
}
// ...
}
设置绑定的代码:
public override void ViewDidLoad()
{
base.ViewDidLoad();
var bindingSet = this.CreateBindingSet<LoginView, LoginViewModel>();
bindingSet.Bind(Username).To(vm => vm.Username);
bindingSet.Bind(Password).To(vm => vm.Password);
bindingSet.Bind(LoginButton).To(vm => vm.LoginCommand);
//Progress is UIActivityIndicator
bindingSet.Bind(Progress)
.For(p => p.Hidden)
.To(vm => vm.IsLoading);
.WithConversion("Inverse");
bindingSet.Apply();
}
正在调用服务器的PCL库中的代码:
public class LoginViewModel : MvxViewModel
{
// ...
private bool _isLoading;
public bool IsLoading
{
get { return _isLoading; }
set { SetProperty(ref _isLoading, value);}
}
private IMvxCommand _loginCommand;
public virtual IMvxCommand LoginCommand
{
get
{
_loginCommand = _loginCommand ?? new MvxAsyncCommand(AttemptLogin, CanExecuteLogin);
return _loginCommand;
}
}
private async Task AttemptLogin()
{
IsLoading = true;
try
{
await Task.Run(async () =>
{
await _loginService.LogIn(Username, Password);
});
ShowViewModel<MainViewModel>();
}
catch (SecurityClientException exc)
{
_dialogService.Alert(exc.Message, "Login Failed", "OK");
}
catch (Exception exc)
{
_dialogService.Alert($"Unknown error: {exc.Message}", "Login Failed", "OK");
}
finally
{
IsLoading = false;
}
}
// ...
}
但是我遇到了以下无法解决的问题:
.WithConversion("Inverse");
的代码版本我是mvvmcross和Xamarin的新手。有没有人知道代码有什么问题?