Most of the WPF mvvm applications, we are using ICommand
in the view-model. But it is referring to System.Windows.Input
. so the view-model is now tightly couple with System.Windows.Input
namespace. according to my understanding view-model should be able to use in normal C# winform application or asp.net application.
Normally we are using following code lines to the command with RelayCommand
implementation.
private RelayCommand testCommand;// or private ICommand testCommand;
public ICommand TestCommand
{
get
{
return testCommand ??
(testCommand = new RelayCommand(param => Test()));
}
}
public void Test()
{
}
What i feel is we need to remove all the ICommand
and use RelayCommand
instead. So we can eliminate the System.Windows
namespace from the view-model. so final code will looks like this,
private RelayCommand testCommand;
public RelayCommand TestCommand
{
get
{
return testCommand ??
(testCommand = new RelayCommand(param => Test()));
}
}
public void Test()
{
}
Any suggestions on this approach? or is there any way to eliminate the System.Windows
namespace from the view-model?
RoutedCommand and RoutedUICommand are two implementations of the ICommand interface in Windows Presentation Foundation (WPF).
ICommand is an interface between the Presentation & the BusinessLogic layer. Whenever any button is pressed on the screen, XAML has its code-behind ButtonClick event. But in MVVM architecture, there is no room for code-behind to keep the application loosely coupled, so ICommand was introduced.
CanExecute will determine whether the command can be executed or not. If it returns false the button will be disabled on the interface.
The RelayCommand and RelayCommand<T> are ICommand implementations that can expose a method or delegate to the view. These types act as a way to bind commands between the viewmodel and UI elements.
Any suggestions on this approach?
This still doesn't decouple you from System.Windows.Input
as RelayCommand
still must implement ICommand
, even if it's indirectly implementing it.
Implementing ICommand
within the ViewModel is one of those things that tends to be required in order to be pragmatic. Ideally, ICommand
(or a similar interface) would have been implemented in a namespace that wasn't XAML specific. That being said, it is supported directly within the Portable Class Libraries, so it is not tied to a specific framework (WPF, Silverlight, Phone, etc) as much as XAML in general.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With