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 8428997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:12:43+00:00 2026-06-10T05:12:43+00:00

I’m using the current version of MvvmLight available on Nuget (4.1.23.0) and calling RaiseCanExecuteChanged

  • 0

I’m using the current version of MvvmLight available on Nuget (4.1.23.0) and calling RaiseCanExecuteChanged does not appear to be doing anything in a unit test. The scenario is very simple, I have a command:

public RelayCommand FooCommand { get; private set; }

I new it up in the view model constructor and point it to some private methods:

FooCommand = new RelayCommand(Foo, CanFoo);

private void Foo()
{
    // do some fooing.
}

private bool CanFoo()
{
    return SomeRequiredProperty != null;
}

Then in the setter for SomeRequiredProperty I call RaiseCanExecuteChanged:

public object SomeRequiredProperty
{
    get
    {
        return someRequiredProperty;
    }

    set
    {
        someRequiredProperty = value;
        FooCommand.RaiseCanExecuteChanged();
    }
}

Now in a unit test I do the following:

// Arrange
var canExecuteChanged = false;
viewModel.FooCommand.CanExecuteChanged += (sender, args) => canExecuteChanged = true;

// Act
viewModel.SomeRequiredProperty = new object();

// Assert
Assert.That(canExecuteChanged, Is.True);

The test fails because my event handler is not firing. Why is that?

Update: The behaviour does indeed work at run time.

  • 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-10T05:12:45+00:00Added an answer on June 10, 2026 at 5:12 am

    Fixed!

    nemesv was correct in that FooCommand.RaiseCanExecuteChanged() simply calls CommandManager.InvalidateRequerySuggested().

    In addition to that, FooCommand.CanExecuteChanged simply forwards the handler on to the CommandManager.RequerySuggested event:

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

    The cause of the problem was the following line of code in the CommandManager class:

    private void RaiseRequerySuggested()
    {
        ...
        _requerySuggestedOperation = dispatcher.
            BeginInvoke(
                DispatcherPriority.Background,
                new DispatcherOperationCallback(RaiseRequerySuggested),
                null); // dispatcher is the Dispatcher for the current thread.
    
        ...
    }
    

    This line places a work item with DispatcherPriority Background on the Dispatcher work item queue. The work item is supposed to notify all handlers of the CommandManager.RequerySuggested event.

    The problem is that this work item is never run.

    The solution is to force the dispatcher to run the work item.

    I found the solution in this discussion on the MVVM Foundation CodePlex page. I managed to simplify the code somewhat into the following helper class.

    public static class DispatcherTestHelper
    {
        private static DispatcherOperationCallback exitFrameCallback = ExitFrame;
    
        /// <summary>
        /// Synchronously processes all work items in the current dispatcher queue.
        /// </summary>
        /// <param name="minimumPriority">
        /// The minimum priority. 
        /// All work items of equal or higher priority will be processed.
        /// </param>
        public static void ProcessWorkItems(DispatcherPriority minimumPriority)
        {
            var frame = new DispatcherFrame();
    
            // Queue a work item.
            Dispatcher.CurrentDispatcher.BeginInvoke(
                minimumPriority, exitFrameCallback, frame);
    
            // Force the work item to run.
            // All queued work items of equal or higher priority will be run first. 
            Dispatcher.PushFrame(frame);
        }
    
        private static object ExitFrame(object state)
        {
            var frame = (DispatcherFrame)state;
    
            // Stops processing of work items, causing PushFrame to return.
            frame.Continue = false;
            return null;
        }
    }
    

    My test now looks like this:

    // Arrange
    var canExecuteChanged = false;
    viewModel.FooCommand.CanExecuteChanged += 
        (sender, args) => canExecuteChanged = true;
    
    // Act
    viewModel.SomeRequiredProperty = new object();
    DispatcherTestHelper.ProcessWorkItems(DispatcherPriority.Background);
    
    // Assert
    Assert.That(canExecuteChanged, Is.True);
    

    And, most importantly, it passes 🙂

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.