Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it okay to use ICommand in view-model

Tags:

c#

mvvm

wpf

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?

like image 900
Chamika Sandamal Avatar asked Mar 12 '13 19:03

Chamika Sandamal


People also ask

Which of the following class is used for implementing ICommand object?

RoutedCommand and RoutedUICommand are two implementations of the ICommand interface in Windows Presentation Foundation (WPF).

What are the methods of ICommand?

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.

Which method of ICommand will let you know if the command can be executed or not?

CanExecute will determine whether the command can be executed or not. If it returns false the button will be disabled on the interface.

What is RelayCommand WPF?

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.


1 Answers

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.

like image 127
Reed Copsey Avatar answered Oct 12 '22 16:10

Reed Copsey