gpt4 book ai didi

windows-phone-7 - Windows 手机 MVVM : Button Command Can Execute and Command Paramtere

转载 作者:行者123 更新时间:2023-12-04 18:10:59 25 4
gpt4 key购买 nike

我想将 MVVM 模式实现到这样的注册页面:

该页面有用于用户名、电子邮件和密码的文本框。

我想使用 将注册按钮绑定(bind)到命令指挥委托(delegate)指挥图案。

问题是如果文本框为空,我希望按钮被禁用,如果它们有文本则启用。

我的 型号 是:

public class User
{
public string UserName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}

我的 查看型号 :
public class UserViewModel:INotifyPropertyChanged
{
private User user;
public UserViewModel()
{
user = new User();
}
#region Properties
.
.
.
#endregion
public ICommand RegisterCommand
{
get
{
return new DelegateCommand(Register,CanRegister);
}
}

private void Register(object parameter)
{
//TODO call the web service
}

private bool CanRegister(object parameter)
{
return (!string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password));
}
}

我的 委托(delegate)指挥执行:
public class DelegateCommand:ICommand
{
//Delegate to the action that the command executes
private Action<object> _executeAction;
//Delegate to the function that check if the command can be executed or not
private Func<object, bool> _canExecute;

public bool canExecuteCache;

public DelegateCommand(Action<object> executeAction):this(executeAction,null)
{

}

public DelegateCommand(Action<object> action, Func<object, bool> canExecute)
{
this._executeAction = action;
this._canExecute = canExecute;
}

//interface method, called when CanExecuteChanged event handler is fired
public bool CanExecute(object parameter)
{
//true by default (in case _canExecute is null)
bool result = true;
Func<object, bool> canExecuteHandler = this._canExecute;
if (canExecuteHandler != null)
{
result = canExecuteHandler(parameter);
}

return result;
}

//Event handler that the controld subscribe to
public event EventHandler CanExecuteChanged;


//interface method
public void Execute(object parameter)
{
_executeAction(parameter);
}


//rause the CanExecuteChanged event handler manually
public void RaiseCanExecuteChanged()
{
EventHandler handler = this.CanExecuteChanged;
if (handler != null)
{
handler(this, EventArgs.Empty);
}

}



}

和我的 查看 :
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Username:"/>
<TextBox Grid.Row="1" Name="txtUserName" Text="{Binding UserName, Mode=TwoWay}" HorizontalAlignment="Stretch"/>
<TextBlock Grid.Row="2" Text="Password:" HorizontalAlignment="Stretch" />
<TextBox Grid.Row="3" Name="txtPassword" Text="{Binding Password, Mode=TwoWay}"/>
<Button Grid.Row="4" Content="Register" Command="{Binding RegisterCommand }" />
</Grid>

我想要实现的是禁用按钮,直到用户在每个 中输入信息。文本框

如何才能做到这一点 ?

谢谢

最佳答案

第一件事:返回一个新的DelegateCommand每次访问该属性(property)都会限制您调用 RaiseCanExecuteChanged() 的能力。方法,因为您不会引用与绑定(bind)相同的命令。

因此,将您的 ViewModel 更改为如下所示:

public class UserViewModel : INotifyPropertyChanged
{
private User user;
public UserViewModel()
{
user = new User();
RegisterCommand = new DelegateCommand(Register,CanRegister);
}

public DelegateCommand RegisterCommand {get; private set;}

private void Register(object parameter)
{
//TODO call the web service
}

private bool CanRegister(object parameter)
{
return (!string.IsNullOrEmpty(UserName) &&
!string.IsNullOrEmpty(Password));
}
}

您可以拥有 RegisterCommand 的原因属性为 private set没有 PropertyChanged 调用,因为它将在绑定(bind)发生之前被实例化并且不需要更改。

假设属性 UserName 的形式和 Password触发 PropertyChanged事件,您可以调用 RaiseCanExecuteChanged() RegisterCommand 上的方法当他们改变时。

例如。
private string _userName;
public string UserName
{
get { return _userName; }
set
{
if(_userName == value)
return;

_userName = value;
RaisePropertyChanged("UserName");

RegisterCommand.RaiseCanExecuteChanged();
}
}

这将强制 CanExecute 方法重新评估。

关于windows-phone-7 - Windows 手机 MVVM : Button Command Can Execute and Command Paramtere,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14423847/

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