Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8164263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:16:45+00:00 2026-06-06T19:16:45+00:00

I am learning MVVM for C# Silverlight development from http://msdn.microsoft.com/en-us/magazine/dd419663.aspx and I am confused

  • 0

I am learning MVVM for C# Silverlight development from

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

and I am confused about the RelayCommand class mentioned in the context. The code is:

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 // Constructors

#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 // ICommand Members
}

For which I dont really understand how _execute and _canExecute works in this case. (I am new to C# and even not sure what’s Action and Predicate. I know they are delegates but whats the difference between them and how they works?)

And also, in the program, I didnt get the line

    add { CommandManager.RequerySuggested += value; }
    remove { CommandManager.RequerySuggested -= value; }

Can someone give me a explaination about this, thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:16:48+00:00Added an answer on June 6, 2026 at 7:16 pm

    A RelayCommand requires two pieces of information:

    1. What code should run when the command is executed (the _execute action)
    2. What code should run to determine if this command can be executed (the _canExecute predicate)

    An Action is a delegate that represents a method that returns void. In this case the _execute action takes one parameter (an object) and returns void.

    A Predicate is a delegate that takes a value and returns a boolean result. In this case the _canExecute predicate takes an object value and returns a bool.

    Both the _execute and _canExecute values are supplied to the RelayCommand when it is constructed because these are the parts of the command that are unique to each individual command.

    Regarding the CanExecuteChanged event:

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }
    

    When a subscriber subscribes to the event, add is called and when they unsubscribe, remove is called. The above CanExecuteChanged event is just a pass-through event (i.e. if a subscriber subscribes to the CanExecuteChanged event, they automatically subscribe to the CommandManager.RequerySuggested event). According to MSDN, the CommandManager.RequerySuggested event…

    occurs when the CommandManager detects conditions that might change the ability of a command to execute.

    I believe that a subscriber would most likely call the CanExecute method on the RelayCommand when this event is fired to determine if the command can still be executed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning MVVM using sample created by Josh Smith at http://msdn.microsoft.com/en-us/magazine/dd419663.aspx I wanted
I'm learning MVVM. I have my View filling two comboboxes from ObservableCollection properties in
I'm learning MVVM in a course from Brian Lagunas in PluralSight. At the beginning,
I've just started learning WPF MVVM using Prism and Unity. Decoupling the view from
I'm learning about using Caliburn.Micro as a MVVM framework for a WPF application. In
Possible Duplicate: MVVM: Tutorial from start to finish? i just started learning WPF. i
I have just started learning MVVM. I've made the application from scratch by following
I'm learning about MVVM and one of the things I don't get is how
I am learning about MVVM so I can support an application that my predecessor
I'm learning Silverlight for WP7 and stumbled upon the MVVM Light toolkit. I thought

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.