我看过一些关于MVVW的教程,但我仍然不确定。
在这个网站。因此,我实现了它提供的方法:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace OCLMEditor
{
public class DelegateCommand<T> : System.Windows.Input.ICommand
{
private readonly Predicate<T> _canExecute;
private readonly Action<T> _execute;
public DelegateCommand(Action<T> execute)
: this(execute, null)
{
}
public DelegateCommand(Action<T> execute, Predicate<T> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
if (_canExecute == null)
return true;
return _canExecute((parameter == null) ? default(T) : (T)Convert.ChangeType(parameter, typeof(T)));
}
public void Execute(object parameter)
{
_execute((parameter == null) ? default(T) : (T)Convert.ChangeType(parameter, typeof(T)));
}
public event EventHandler CanExecuteChanged;
public void RaiseCanExecuteChanged()
{
if (CanExecuteChanged != null)
CanExecuteChanged(this, EventArgs.Empty);
}
}
}
现在,在我的ViewModel中我有这个:
public class OCLMEditorModelView
{
private readonly DelegateCommand<string> _DeleteStudentButtonClickCommand;
public ObservableCollection<Student> Students
{
get
{
return _Model.Students;
}
}
private Student _SelectedStudentItem;
public Student SelectedStudentItem
{
get { return _SelectedStudentItem; }
set
{
_SelectedStudentItem = value;
_DeleteStudentButtonClickCommand.RaiseCanExecuteChanged();
}
}
private OCLMEditorModel _Model;
public List<StudyPointItem> ReadingStudyPointsList { get; set; }
public List<StudyPointItem> StudentStudyPointsList { get; set; }
public List<String> TreasureItemMethodsList { get; set; }
public OCLMEditorModelView()
{
InitTreasureItemMethodsList();
InitReadingStudyPointsList();
InitStudentStudyPointsList();
_Model = new OCLMEditorModel();
_DeleteStudentButtonClickCommand = new DelegateCommand<string>(
(s) => { _Model.Students.Remove(_SelectedStudentItem); _Model.Serialize(); MessageBox.Show("Student deleted!"); }, //Execute
(s) => { return _SelectedStudentItem != null; } //CanExecute
);
}
public DelegateCommand<string> DeleteStudentButtonClickCommand
{
get { return _DeleteStudentButtonClickCommand; }
}
// Code snipped
}
为完整起见,按钮的XAML:
<Button x:Name="buttonDeleteStudent"
Content="Delete"
Margin="2"
Command="{Binding DeleteStudentButtonClickCommand}"/>
一切正常。但是我发现这段代码很难读(LINQ):
_DeleteStudentButtonClickCommand = new DelegateCommand<string>(
(s) => { _Model.Students.Remove(_SelectedStudentItem); _Model.Serialize(); MessageBox.Show("Student deleted!"); }, //Execute
(s) => { return _SelectedStudentItem != null; } //CanExecute
);
我知道它在干什么。但是,如果我的代码变得更复杂,我更喜欢一个专门的处理程序。如何定义这个处理程序?
谢谢。
澄清一下,这不是linq,它只是作为参数传递的lambda方法。
如果你想坚持使用DelegateCommand类,那么你可以定义方法,而不是通过替换:
public OCLMEditorModelView()
{
InitTreasureItemMethodsList();
InitReadingStudyPointsList();
InitStudentStudyPointsList();
_Model = new OCLMEditorModel();
_DeleteStudentButtonClickCommand = new DelegateCommand<string>(
(s) => { _Model.Students.Remove(_SelectedStudentItem); _Model.Serialize(); MessageBox.Show("Student deleted!"); }, //Execute
(s) => { return _SelectedStudentItem != null; } //CanExecute
);
}
public OCLMEditorModelView()
{
InitTreasureItemMethodsList();
InitReadingStudyPointsList();
InitStudentStudyPointsList();
_Model = new OCLMEditorModel();
_DeleteStudentButtonClickCommand = new DelegateCommand<string>(
ExecuteMethod, //Execute
CanExecuteMethod//CanExecute
);
}
void ExecuteMethod(string s)
{
_Model.Students.Remove(_SelectedStudentItem);
_Model.Serialize();
MessageBox.Show("Student deleted!");
}
bool CanExecuteMethod(string s)
{
return _SelectedStudentItem != null;
}
请注意,这真的没有增加任何可维护性,相反,如果你在其他地方重用这些方法代码,这是一件好事,但是用匿名方法或lambda语法定义内联方法的目的是让代码显示在使用它的地方,这对于小的不可重用的代码片段来说是很好的。还要注意,在lambda语法版本中,CanExecute可以简化(单个语句,可以删除括号并返回),并且没有理由将所有内容写在一行中(因此在Execute中每个语句可以在自己的行中)。我将把它重写为:
public OCLMEditorModelView()
{
InitTreasureItemMethodsList();
InitReadingStudyPointsList();
InitStudentStudyPointsList();
_Model = new OCLMEditorModel();
_DeleteStudentButtonClickCommand = new DelegateCommand<string>(
(s) =>
{
_Model.Students.Remove(_SelectedStudentItem);
_Model.Serialize();
MessageBox.Show("Student deleted!");
},
(s) => _SelectedStudentItem != null);
}