MVVM Light RelayCommand 不工作
MVVM Light RelayCommand not working
我刚开始使用命令,正在尝试使用 CanExecute 根据某些因素启用和禁用我的按钮。但我做错了什么,无法弄清楚。加载时它工作正常。 CanExecuteGenerate 函数被命中,模型为空,因此返回 false。 UI 上的按钮已禁用。但是从那里它再也不会点击 CanExecuteGenerate,导致我的按钮保持禁用状态。谁能看到我遗漏或做错了什么?
public class MainWindowViewModel: PropertyChangedNotification{
public RelayCommand GenerateCommand { get; set; }
public MainWindowViewModel( ) {
GenerateCommand = new RelayCommand( OnGenerateClicked, CanExecuteGenerate( ) );
Model = new MainModel( );
}
private Func<bool> CanExecuteGenerate( ) {
if( Model != null ) {
return ( ) => ( Model.Name != "" && Model.Title != "" ) ? true : false;
}
return ( ) => false;
}
public void someothermethod(){
Model.Name = "James"
Model.Title = "Doctor"
GenerateCommand.RaiseCanExecuteChanged();
}
public void OnGenerateClicked(){
//Do some other stuff
}
}
当您创建 RelayCommand
时,您总是传递 returns false 的方法。
您不应为模型为空的情况创建单独的方法,而应在传递给 RelayCommand
的方法中处理它。
试试这个方法:
private bool CanExecuteGenerate( ) {
if( Model != null ) {
return Model.Name != "" && Model.Title != "";
}
return false;
}
并将 RelayCommand
的结构更改为
GenerateCommand = new RelayCommand(OnGenerateClicked, CanExecuteGenerate);
因为您的 CanExecuteGenerate 方法 returns 将调用一个委托。试试这个:
public class MainWindowViewModel: PropertyChangedNotification{
public RelayCommand GenerateCommand { get; set; }
public MainWindowViewModel( ) {
GenerateCommand = new RelayCommand( OnGenerateClicked, CanExecuteGenerate);
Model = new MainModel( );
}
private bool CanExecuteGenerate( ) {
if( Model != null )
return ( Model.Name != "" && Model.Title != "" ) ? true : false;
return false;
}
public void someothermethod(){
Model.Name = "James"
Model.Title = "Doctor"
GenerateCommand.RaiseCanExecuteChanged();
}
public void OnGenerateClicked(){
//Do some other stuff
}
}
我刚开始使用命令,正在尝试使用 CanExecute 根据某些因素启用和禁用我的按钮。但我做错了什么,无法弄清楚。加载时它工作正常。 CanExecuteGenerate 函数被命中,模型为空,因此返回 false。 UI 上的按钮已禁用。但是从那里它再也不会点击 CanExecuteGenerate,导致我的按钮保持禁用状态。谁能看到我遗漏或做错了什么?
public class MainWindowViewModel: PropertyChangedNotification{
public RelayCommand GenerateCommand { get; set; }
public MainWindowViewModel( ) {
GenerateCommand = new RelayCommand( OnGenerateClicked, CanExecuteGenerate( ) );
Model = new MainModel( );
}
private Func<bool> CanExecuteGenerate( ) {
if( Model != null ) {
return ( ) => ( Model.Name != "" && Model.Title != "" ) ? true : false;
}
return ( ) => false;
}
public void someothermethod(){
Model.Name = "James"
Model.Title = "Doctor"
GenerateCommand.RaiseCanExecuteChanged();
}
public void OnGenerateClicked(){
//Do some other stuff
}
}
当您创建 RelayCommand
时,您总是传递 returns false 的方法。
您不应为模型为空的情况创建单独的方法,而应在传递给 RelayCommand
的方法中处理它。
试试这个方法:
private bool CanExecuteGenerate( ) {
if( Model != null ) {
return Model.Name != "" && Model.Title != "";
}
return false;
}
并将 RelayCommand
的结构更改为
GenerateCommand = new RelayCommand(OnGenerateClicked, CanExecuteGenerate);
因为您的 CanExecuteGenerate 方法 returns 将调用一个委托。试试这个:
public class MainWindowViewModel: PropertyChangedNotification{
public RelayCommand GenerateCommand { get; set; }
public MainWindowViewModel( ) {
GenerateCommand = new RelayCommand( OnGenerateClicked, CanExecuteGenerate);
Model = new MainModel( );
}
private bool CanExecuteGenerate( ) {
if( Model != null )
return ( Model.Name != "" && Model.Title != "" ) ? true : false;
return false;
}
public void someothermethod(){
Model.Name = "James"
Model.Title = "Doctor"
GenerateCommand.RaiseCanExecuteChanged();
}
public void OnGenerateClicked(){
//Do some other stuff
}
}