我正在尝试将样式应用于已被命令禁用的按钮。
我假设IsEnabled状态是由canexecutechanged事件触发的属性,但似乎没有。
什么Button属性受到影响,我可以挂钩到这个事件,以便我可以为按钮提供样式吗?
答案 0 :(得分:2)
在您的viewmodel中,您可以添加一个属性,该属性将导致启用或禁用其按钮。下面是一个例子。
public Command FacebookLoginCommand { get; set; }
private bool _IsBusy;
public override bool IsBusy
{
get
{
return _IsBusy;
}
set
{
_IsBusy = value;
OnPropertyChanged();
FacebookLoginCommand?.ChangeCanExecute();
GoogleLoginCommand?.ChangeCanExecute();
}
}
public LoginViewModel(IUserDialogs dialogs) : base(dialogs)
{
FacebookLoginCommand = new Command(async () =>
{
using (Dialogs.Loading("Carregando"))
{
IsBusy = true;
await Task.Run(() => new FacebookLoginService(Dialogs).Logar());
await Task.Run(() => Task.Delay(TimeSpan.FromSeconds(3)));
IsBusy = false;
}
}, CanExecute());
private Func<bool> CanExecute()
{
return new Func<bool>(() => !IsBusy);
}
}
答案 1 :(得分:0)
以下是用户名长度11和密码至少为1时登录的示例。
public class MainViewModel : BaseViewModel
{
public Command LoginIn { get; set; }
public MainViewModel()
{
LoginIn = new Command(async () => await SignIn(), (() => CanExecuteLogin));
}
private string _password;
private string _username;
public string UserName
{
get => _username;
set
{
SetProperty(ref _username, value, nameof(UserName));
SetProperty(ref _canExecuteLogin, IsExecutable(), nameof(CanExecuteLogin));
LoginIn?.ChangeCanExecute();
}
}
public string Password
{
get => _password;
set
{
SetProperty(ref _password, value, nameof(Password));
SetProperty(ref _canExecuteLogin, IsExecutable(), nameof(CanExecuteLogin));
LoginIn?.ChangeCanExecute();
}
}
private bool _canExecuteLogin;
public bool CanExecuteLogin
{
get => _canExecuteLogin;
set => SetProperty(ref _canExecuteLogin, value, nameof(CanExecuteLogin));
}
public bool IsExecutable()
{
if (UserName != null && _password != null)
{
if (UserName.Length == 11 && _password.Length > 0)
return true;
}
return false;
}
private async Task SignIn()
{ //Login Code here }
}