问题描述
因此,在这个特殊的MVVM实现我在干什么,我需要一些命令。我真的累了实现的ICommand类一个接一个,所以我想出了一个解决办法,但我不知道它有多好,所以任何WPF专家在这里的投入将大大AP preciated。如果你可以提供一个更好的解决方案,甚至更好!
我所做的是一个单一的ICommand类和两个代表它需要一个对象作为参数,一位代表是无效(对于OnExecute),其他的布尔(对于OnCanExecute)。所以在我的ICommand(这就是所谓的ViewModel类)的构造我送这两种方法,并在每个ICommand的方法,我调用了代表们的方法。
它的作品真的很好,但我不知道这是一个糟糕的方式做到这一点,或者如果有一个更好的办法。下面是完整的code,任何输入将大大AP preciated,甚至是负的,但请有建设性的。
谢谢!
视图模型:
公共类TestViewModel:DependencyObject的
{
公众的ICommand COMMAND1 {获得;组; }
公共ICommand的命令2 {获得;组; }
公共ICommand的命令3 {获得;组; }
公共TestViewModel()
{
this.Command1 =新TestCommand(ExecuteCommand1,CanExecuteCommand1);
this.Command2 =新TestCommand(ExecuteCommand2,CanExecuteCommand2);
this.Command3 =新TestCommand(ExecuteCommand3,CanExecuteCommand3);
}
公共BOOL CanExecuteCommand1(对象参数)
{
返回true;
}
公共无效ExecuteCommand1(对象参数)
{
的MessageBox.show(执行命令1);
}
公共BOOL CanExecuteCommand2(对象参数)
{
返回true;
}
公共无效ExecuteCommand2(对象参数)
{
的MessageBox.show(执行命令2);
}
公共BOOL CanExecuteCommand3(对象参数)
{
返回true;
}
公共无效ExecuteCommand3(对象参数)
{
的MessageBox.show(执行命令3);
}
}
ICommand的:
公共类TestCommand:ICommand的
{
公共委托无效ICommandOnExecute(对象参数);
公共委托布尔ICommandOnCanExecute(对象参数);
私人ICommandOnExecute _execute;
私人ICommandOnCanExecute _canExecute;
公共TestCommand(ICommandOnExecute onExecuteMethod,ICommandOnCanExecute onCanExecuteMethod)
{
_execute = onExecuteMethod;
_canExecute = onCanExecuteMethod;
}
#地区的ICommand成员
公共事件的EventHandler CanExecuteChanged
{
添加{CommandManager.RequerySuggested + =价值; }
除去{CommandManager.RequerySuggested - =价值; }
}
公共BOOL CanExecute(对象参数)
{
返回_canExecute.Invoke(参数);
}
公共无效执行(对象参数)
{
_execute.Invoke(参数);
}
#endregion
}
这是几乎相同的卡尔Shifflet如何表现出了 RelayCommand
,其中执行
触发一个predetermined 动作< T>
。顶尖的解决方案,如果你问我。
公共类RelayCommand:ICommand的
{
公共RelayCommand(predicate<对象> canExecute,动作<对象>执行)
{
// ...我想你能看到这正好...
}
// ... 等等 ...
公共无效执行(对象参数)
{
_execute(参数);
}
}
此可随后被用作...
公共类MyViewModel
{
私人ICommand的_doSomething;
公众的ICommand DoSomethingCommand
{
得到
{
如果(_doSomething == NULL)
{
_doSomething =新RelayCommand(
P => this.CanDoSomething,
P => this.DoSomeImportantMethod()
}
返回_doSomething;
}
}
}
So in this particular MVVM implementation I'm doing, I need several commands. I really got tired of implementing the ICommand classes one by one, so I came up with a solution, but I don't know how good it is, so the input of any WPF expert here will be greatly appreciated. And if you could provide a better solution, even better!
What I did is a single ICommand class and two delegates which take an object as a parameter, one delegate is void (for OnExecute), the other bool (for OnCanExecute). So in the constructor of my ICommand (which is called by the ViewModel class) I send the two methods, and on each ICommand method I invoke the delegates' methods.
It works really good, but I'm not sure if this is a bad way to do it, or if there's a better way. Below is the complete code, any input will be greatly appreciated, even negative, but please be constructive.
Thanks!!
ViewModel:
public class TestViewModel : DependencyObject
{
public ICommand Command1 { get; set; }
public ICommand Command2 { get; set; }
public ICommand Command3 { get; set; }
public TestViewModel()
{
this.Command1 = new TestCommand(ExecuteCommand1, CanExecuteCommand1);
this.Command2 = new TestCommand(ExecuteCommand2, CanExecuteCommand2);
this.Command3 = new TestCommand(ExecuteCommand3, CanExecuteCommand3);
}
public bool CanExecuteCommand1(object parameter)
{
return true;
}
public void ExecuteCommand1(object parameter)
{
MessageBox.Show("Executing command 1");
}
public bool CanExecuteCommand2(object parameter)
{
return true;
}
public void ExecuteCommand2(object parameter)
{
MessageBox.Show("Executing command 2");
}
public bool CanExecuteCommand3(object parameter)
{
return true;
}
public void ExecuteCommand3(object parameter)
{
MessageBox.Show("Executing command 3");
}
}
ICommand:
public class TestCommand : ICommand
{
public delegate void ICommandOnExecute(object parameter);
public delegate bool ICommandOnCanExecute(object parameter);
private ICommandOnExecute _execute;
private ICommandOnCanExecute _canExecute;
public TestCommand(ICommandOnExecute onExecuteMethod, ICommandOnCanExecute onCanExecuteMethod)
{
_execute = onExecuteMethod;
_canExecute = onCanExecuteMethod;
}
#region ICommand Members
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object parameter)
{
return _canExecute.Invoke(parameter);
}
public void Execute(object parameter)
{
_execute.Invoke(parameter);
}
#endregion
}
This is almost identical to how Karl Shifflet demonstrated a RelayCommand
, where Execute
fires a predetermined Action<T>
. A top-notch solution, if you ask me.
public class RelayCommand : ICommand
{
public RelayCommand(Predicate<object> canExecute, Action<object> execute)
{
// ... I think you can see where this goes ...
}
// ... etc ...
public void Execute(object parameter)
{
_execute(parameter);
}
}
This could then be used as...
public class MyViewModel
{
private ICommand _doSomething;
public ICommand DoSomethingCommand
{
get
{
if (_doSomething == null)
{
_doSomething = new RelayCommand(
p => this.CanDoSomething,
p => this.DoSomeImportantMethod()
}
return _doSomething;
}
}
}
这篇关于WPF的ICommand MVVM实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!