Relay/ICommand vs DelegateCommand -- Differences - c#

As far as I can tell the below code can be changed from Relay/ICommand Command to Delegate command and still bind the commands the same way! If I am wrong what are the differences and uses of each.
private DelegateCommand something;
public DelegateCommand Something
Here is the full implementation
private RelayCommand something;
public ICommand Something
{
get
{
if (something == null)
something = new RelayCommand(SomethingMethod, CanSomething);
return something;
}
}
private bool CanSomething(object parameter)
{
//just for readability return true
return true;
}
private void SomethingMethod(object parameter)
{
using (DatabaseContext context = new DatabaseContext())
{
try { }
catch(Exception ex)
{
throw new ApplicationException(string.Format("Something {0} to {1}", file, directory), ex);
}
}
}

Neither DelegateCommand nor RelayCommand exist in the framework itself. They are provided by third party libraries.
Both are an implementation of ICommand which works by accepting a delegate and using that to provide the ICommand implementation. As such, both classes have the same intent, and work in basically the same manner.
As for differences - there may be some subtle diffferences, depending on which framework you're using. For example, Prism's DelegateCommand<T> also has a concept of IActiveAware, which is used for building composite commands.

Related

RelayCommand best practice

I'm working with RelayCommands (they are in a separate class) for about a month now and I got the feeling they're kind of clunky when declaring them. Below I have 3 ways I can think of how I can declare a RelayCommand.
In a first case I declare my ICommand and then when the ViewModel is loading I construct my RelayCommand that points to a method in my code.
public class MyViewModel
{
public ICommand MyCommand { get; private set; }
public MyViewModel()
{
MyCommand = new RelayCommand(MyMethod, CanMyMethod);
}
private void MyMethod()
{
// Do something here ...
}
private bool CanMyMethod()
{
return string.IsNullOrEmpty(MyString) ? false : true;
}
}
A second method is to do everything at once.
public ICommand MyCommand
{
get
{
return new RelayCommand(
() =>
{
// Do something here ...
},
() =>
string.IsNullOrEmpty(MyString) ? false : true);
}
}
Now, I'm planning to write an application with quite some Commands in a certain ViewModel. I also can't split the ViewModel in smaller ViewModels because all the controls have to work together.
So my questions are:
What is the best approach to declaring and constructing ICommands? Is it one of my approaches or is there an easier way?
How hard is it to maintain the overview with each approach considering there are over 50 ICommands in a single ViewModel.
I'm hoping to release my application on both Windows 7, 8 and 10 in the future. Are there any limitations to RelayCommands I have to take in account if I'm only using .NET4.5?
Besides RelayCommands I also found this project: Caliburn-Micro. It allows you to do something like the code below. Does anyone have an idea how good this works performance wise in comparison to RelayCommands? This is just an extra question and not required to be answered to have a post marked as an answer.
Xaml (View)
<Button x:Name="Login" Content="Log in" />
ViewModel
public bool CanLogin(string username, string password)
{
return !String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(password);
}
public string Login(string username, string password)
{
...
}
Refer below answers.
What is the best approach to declaring and constructing ICommands?
Is it one of my approaches or is there an easier way? Ans:You can
take combined approach. If your execute method is very minimal, you
can use RelayCommnd otherwise you can implement your own ICommand in
a separate class. This will improve readability of your viewmodel as
well as modularity of your code.
How hard is it to maintain the overview with each approach considering there are over 50 ICommands in a single ViewModel.
Ans: Cover in Ans 1
I'm hoping to release my application on both Windows 7, 8 and 10 in the future. Are there any limitations to RelayCommands I have to take in account if I'm only using .NET4.5?
Ans: I am not seeing any limitation in Windows 7 or 8 but not sure about Windows 10.
Besides RelayCommands I also found this project: Caliburn-Micro. It allows you to do something like the code below. Does anyone have an idea how good this works performance wise in comparison to RelayCommands? This is just an extra question and not required to be answered to have a post marked as an answer.
Ans: I am not sure about source code of RelayCommands in Caliburn-Micro. But if it using CommandManager to achieve the CanExecute functionality. Then command manager will get triggered for all the user inputs change and hence that will cause performance issues if have some heavy logic in the CanExecute method. Refer Are there any performance implications with CanExecuteCommand?
Here's the pattern that I prefer, which is basically a variation of method 1:
public class MyViewModel
{
private readonly RelayCommand myCommand;
public MyViewModel()
{
this.myCommand = new RelayCommand(this.DoStuff, () => ...);
}
public ICommand MyCommand
{
get { return this.myCommand; }
}
private void DoStuff() { ... }
}
This has the advantage of keeping extra methods on your RelayCommand implementation (like RaiseCanExecuteChanged) around for use from your view model, but exposes only an ICommand instance to consumers.
I agree with Krowi, the first approach is much easier to read. But I would put your relay command into its own class so you can re-use it:
public class RelayCommand : ICommand
{
#region Fields
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion // Fields
#region Constructors
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion
#region ICommand Members
[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
#endregion
}

Binding commands without PRISM

Since I started using MVVM, I've always used PRISM's DelegateCommand class for binding commands in my view models to button commands in my views. I believe Telerik also has an equivalent of DelegateCommand.
My question is, is there a built in alternative to using 3rd party frameworks such as prism and telerik. If I'm throwing together a quick throwaway application I may not want the hassle of installing packages from NuGet. Is there a way to achieve the same thing using a Func or an Action or a delegate?
No, you still need a Command class that implements ICommand. However, you can write your own DelegateCommand really easily (citation, I wrote this off the top of my head in less than a minute):
public class DelegateCommand : ICommand
{
private Action<object> execute;
public DelegateCommand(Action<object> executeMethod)
{
execute = executeMethod;
}
public bool CanExecute(object param)
{
return true;
}
public void Execute(object param)
{
if (execute != null)
execute(param);
}
}
Use and enjoy! You could take an additional Func<bool, object> parameter if you wanted custom CanExecute behavior instead of returning true.
Note, if you really don't like null as the function, and want it to throw if you try it, just use this constructor instead:
public DelegateCommand(Action<object> executeMethod)
{
if (executeMethod == null)
throw new ArgumentNullException("executeMethod");
execute = executeMethod;
}

What is my mistake in implementing an asynchronous RelayCommand?

I am learning WPF and MVVM at the moment and I faced a problem when i tried to write unit tests for a viewmodel, whose commands invoke async methods. That problem is well-described in this question. That question also has a solution: to write a new Command class with an additional awaitable method that can be awaited in unit tests. But since i use MvvmLight, i decided not to write a new class, but to inherit from the built-in RelayCommand class instead. However, i don't seem to understand how to do it properly. Below is a simplified example that illustrates my problem:
AsyncRelayCommand:
public class AsyncRelayCommand : RelayCommand
{
private readonly Func<Task> _asyncExecute;
public AsyncRelayCommand(Func<Task> asyncExecute)
: base(() => asyncExecute())
{
_asyncExecute = asyncExecute;
}
public AsyncRelayCommand(Func<Task> asyncExecute, Action execute)
: base(execute)
{
_asyncExecute = asyncExecute;
}
public Task ExecuteAsync()
{
return _asyncExecute();
}
//Overriding Execute like this fixes my problem, but the question remains unanswered.
//public override void Execute(object parameter)
//{
// _asyncExecute();
//}
}
My ViewModel (based on the default MvvmLight MainViewModel):
public class MainViewModel : ViewModelBase
{
private string _welcomeTitle = "Welcome!";
public string WelcomeTitle
{
get
{
return _welcomeTitle;
}
set
{
_welcomeTitle = value;
RaisePropertyChanged("WelcomeTitle");
}
}
public AsyncRelayCommand Command { get; private set; }
public MainViewModel(IDataService dataService)
{
Command = new AsyncRelayCommand(CommandExecute); //First variant
Command = new AsyncRelayCommand(CommandExecute, () => CommandExecute()); //Second variant
}
private async Task CommandExecute()
{
WelcomeTitle = "Command in progress";
await Task.Delay(1500);
WelcomeTitle = "Command completed";
}
}
As far as i understand it, both First and Second variants should invoke different constructors, but lead to the same result. However, only the second variant works the way i expect it to. The first one behaves strangely, for example, if i press the button, that is binded to Command once, it works ok, but if i try to press it a second time a few seconds later, it simply does nothing.
My understanding of async and await is far from complete. Please explain me why the two variants of instantiating the Command property behave so differently.
P.S.: this behavior is noticeable only when i inherit from RelayCommand. A newly created class that implements ICommand and has the same two constructors works as expected.
OK, I think I found the problem. RelayCommand uses a WeakAction to allow the owner (target) of the Action to be garbage collected. I'm not sure why they made this design decision.
So, in the working example where the () => CommandExecute() is in the view model constructor, the compiler is generating a private method on your constructor that looks like this:
[CompilerGenerated]
private void <.ctor>b__0()
{
this.CommandExecute();
}
Which works fine because the view model is not eligible for garbage collection.
However, in the odd-behavior example where the () => asyncExecute() is in the constructor, the lambda closes over the asyncExecute variable, causing a separate type to be created for that closure:
[CompilerGenerated]
private sealed class <>c__DisplayClass2
{
public Func<Task> asyncExecute;
public void <.ctor>b__0()
{
this.asyncExecute();
}
}
This time, the actual target of the Action is an instance of <>c__DisplayClass2, which is never saved anywhere. Since WeakAction only saves a weak reference, the instance of that type is eligible for garbage collection, and that's why it stops working.
If this analysis is correct, then you should always either pass a local method to RelayCommand (i.e., do not create lambda closures), or capture a (strong) reference to the resulting Action yourself:
private readonly Func<Task> _asyncExecute;
private readonly Action _execute;
public AsyncRelayCommand(Func<Task> asyncExecute)
: this(asyncExecute, () => asyncExecute())
{
}
private AsyncRelayCommand(Func<Task> asyncExecute, Action execute)
: base(execute)
{
_asyncExecute = asyncExecute;
_execute = execute;
}
Note that this actually has nothing to do with async; it's purely a question of lambda closures. I suspect it's the same underlying issue as this one regarding lambda closures with Messenger.

Avoid instantiate object when every time constructor being called

I have a PersonViewModel class and bind with Person User Control. Everytime when I select Person Use Control page, createPersonCommand, deletePersonCommand, viewPersonCommand will always reinstantiate. Are there any ideas to create those command once ? Execpt for InitializePersonCollection method because will retrieve latest date from DB.
My senior comments on my assignment, and he was mentioned on this.
private ICommand createPersonCommand;
public ICommand CreatePersonCommand
{
get { return createPersonCommand; }
}
private ICommand deletePersonCommand;
public ICommand DeletePersonCommand
{
get { return deletePersonCommand; }
}
private ICommand viewPersonCommand;
public ICommand ViewPersonCommand
{
get { return viewPersonCommand; }
}
public PersonViewModel()
{
createPersonCommand = new DelegateCommand<object>(ExecuteCreatePersonCommand);
deletePersonCommand = new DelegateCommand<object>(ExecuteDeletePersonCommand);
viewPersonCommand = new DelegateCommand<object>(ExecuteViewPersonCommand);
InitializePersonCollection();
}
As long as the command executing methods are not static, you will need to do this. If for whatever reason you can make the command methods static, you could make your DelegateCommands static as well and only assign the static instances to your members.
But as people already mentioned, that would not make sense. The code you have is a well established pattern. There is no reason to change it.
Maybe you should check if the commands are actually well placed. Does every person need it's own command, or do you need commands in your main viewmodel that get a single person as parameter? That might make more sense.
You can use "lazy-loading". Then commands will be instantiated on-demand and will not affect speed of loading data from database.
private ICommand createPersonCommand;
public ICommand CreatePersonCommand
{
get {
if (createPersonCommand == null) {
createPersonCommand = new DelegateCommand<object>(ExecuteCreatePersonCommand)
}
return createPersonCommand;
}
}

ICommand CanExecute not triggering after PropertyChanged?

I got a WPF application that shows a button bound to a command like that:
<Button Command="{Binding Path=TestrunStartCommand}" Content="GO!">
The command is defined like that:
public ICommand TestrunStartCommand
{
get { return new RelayCommand(TestrunStartExecute, () => !IsTestrunInProgress); }
}
public bool IsTestrunInProgress
{
get{
return _isTestrunInProgress;
}
set{
_isTestrunInProgress = value;
RaisePropertyChanged(IsTestrunInProgressPropertyName);
}
}
The problem is, the button won't be enabled immediately after I set IsTestrunInProgress to false, but only after I click inside the application window.
Could you help me understand this behaviour and show me how to fix this?
Further reading:
wpf command pattern - when does it query canexecute
The ICommand interface exposes an event ICommand.CanExecuteChanged which is used to inform the UI when to re-determine the IsEnabled state of command driven UI components.
Depending upon the implementation of the RelayCommand you are using, you may need to raise this event; Many implementations expose a method such as RelayCommand.RaiseCanExecuteChanged() which you can invoke to force the UI to refresh.
Some implementations of the RelayCommand make use of CommandManager.RequerySuggested, in which case you will need to call CommandManager.InvalidateRequerySuggested() to force the UI to refresh.
Long story short, you will need to call one of these methods from your property setter.
Update
As the state of the button is being determined when the active focus is changing, I believe the CommandManager is being used. So in the setter of your property, after assigning the backing field, invoke CommandManager.InvalidateRequerySuggested().
Update 2
The RelayCommand implementation is from the MVVM light toolkit. When consumed from WPF/.NET, the implementation wraps the methods and events exposed from the CommandManager. This will mean that these commands work automagically in the majority of situations (where the UI is altered, or the focused element is changed). But in a few cases, such as this one, you will need to manually force the command to re-query. The proper way to do this using this library would be to call the RaiseCanExecuteChanged() method on the RelayCommand.
This is so important and easy to miss, I am repeating what #Samir said in a comment. Mr Laurent Bugnion wrote in his blog:
In WPF 4 and WPF 4.5, however, there is a catch: The CommandManager will stop working after you upgrade MVVM Light to V5. What you will observe is that your UI elements (buttons, etc) will stop getting disabled/enabled when the RelayCommand’s CanExecute delegate returns false.
If you are in a hurry, here is the fix: In any class that uses the RelayCommand, replace the line saying:
using GalaSoft.MvvmLight.Command;
with:
using GalaSoft.MvvmLight.CommandWpf;
You can try with CommandManager.InvalidateRequerySuggested.
Anyway this did not help me sometimes in the past. For me the best solution turned out to be to bind the boolean property to the Button.IsEnabled dependency property.
In your case something like
IsEnabled={Binding IsTestrunInProgress}
The issue is, the ICommand Property TestrunStartCommand is always returning a new command object whenever it is accessed.
A simple fix is to create the ICommand object once and use it again and again.
private ICommand _testRunCommand = null;
public ICommand TestrunStartCommand
{
get
{
return _testRunCommand ?? (_testRunCommand = new RelayCommand(TestrunStartExecute, () => !IsTestrunInProgress));
}
}
This was quite a simple fix and it worked for me.
Addition to Riegardt Steyn's answer above: https://stackoverflow.com/a/33503341/1964969
If you don't want to change Command to CommandWpf usage (as that two RelayCommand versions are not compatible inbetween), another workaround could be to not instantiate a command at the declaration place. Use constructor code instead:
public class SomeVMClass
{
// CanExecute won't work:
// Declaration and instantiation same place
public RelayCommand MyCommand1 => new RelayCommand(MyBusinessLogic, MyCanExecuteValidator);
// CanExecute will work
// Declaration only
public RelayCommand MyCommand2 { get; private set; }
public SomeVMClass()
{
// Let's instantiate our declared command
MyCommand2 = new RelayCommand(MyBusinessLogic, MyCanExecuteValidator);
...
Blockquote
In your Command class change CanExcutedChanged to this
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
This is example of my command class
public class SaveConfigCommand : ICommand
{
public MyViewModel VM { get; set; }
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public SaveConfigCommand(MyViewModel vm)
{
VM = vm;
}
public bool CanExecute(object? parameter)
{
MyObjectModel model = parameter as MyObjectModel;
if (model == null)
return false;
// Validate others properties here
return true;
}
public void Execute(object? parameter)
{
VM.MyMethodInViewModel();
}
}

Categories

Resources