Open sandboxFocusImprove this doc

XAM078: Invalid Signature of the CanExecute Method of a Command

Severity Error
Reported by PostSharp.Patterns.Xaml
Message Error in the [Command] aspect applied to property <CommandProperty>: the signature of method <CanExecuteMethod> is incorrect. The CanExecute method must be a non-static method that returns bool and has zero or one parameter, for example "bool MyCanExecute()" or "bool MyCanExecute(ParamType)".

Cause

The CommandAttribute aspect implements the CanExecute(object) method of the command by calling a method of the view model. It looks for a method named CanExecute<Name> or Can<Name>, where <Name> is the name of the command property without the Command suffix, or for the method named by the CanExecuteMethod property of the aspect. When the class declares such a method, a method takes precedence over a CanExecute property of the same name.

The aspect reports XAM078 when this method cannot implement CanExecute, because it:

  • does not return bool;
  • is static;
  • has more than one parameter. The only parameter allowed receives the command parameter.

PostSharp reports the message on the command property, not on the method. In the current version, <CanExecuteMethod> shows the name of an internal type of the aspect instead of the name of the method.

In the following example, the CanExecuteSubmit method of the SubmitCommand property returns void.

using System;
using System.Windows.Input;
using PostSharp.Patterns.Xaml;

namespace Errors.XAM078_Error;

// The [Command] aspect initializes the command property.
#pragma warning disable CS8618

public class OrderViewModel
{
    public int ItemCount { get; set; }

    // The CanExecuteSubmit method of this command returns void instead of bool.
    [Command]
    public ICommand SubmitCommand { get; private set; }

    public void CanExecuteSubmit()
    {
    }

    public void ExecuteSubmit()
    {
        Console.WriteLine( $"Submitting {this.ItemCount} items." );
    }
}

public static class Program
{
    public static void Main()
    {
        var viewModel = new OrderViewModel { ItemCount = 3 };

        Console.WriteLine( $"Can submit: {viewModel.SubmitCommand.CanExecute( null )}" );
        viewModel.SubmitCommand.Execute( null );
    }
}
error XAM078: XAM078_Error.cs(16,21): Error in the [Command] aspect applied to property Errors.XAM078_Error.OrderViewModel.SubmitCommand: the signature of method Errors.XAM078_Error.OrderViewModel.CanExecuteSubmit is incorrect. The CanExecute method must be a non-static method that returns bool and has zero or one parameter, for example "bool MyCanExecute()" or "bool MyCanExecute(ParamType)".

Resolution

Change the signature of the CanExecute method: make it an instance method that returns bool and has no parameter, or one parameter that receives the command parameter. If the method is not meant to implement CanExecute, rename it, or set CanExecuteMethod to the name of another method.

In the following example, CanExecuteSubmit returns bool.

using System;
using System.Windows.Input;
using PostSharp.Patterns.Xaml;

namespace Errors.XAM078_Fixed;

// The [Command] aspect initializes the command property.
#pragma warning disable CS8618

public class OrderViewModel
{
    public int ItemCount { get; set; }

    // The CanExecuteSubmit method of this command returns bool.
    [Command]
    public ICommand SubmitCommand { get; private set; }

    public bool CanExecuteSubmit()
    {
        return this.ItemCount > 0;
    }

    public void ExecuteSubmit()
    {
        Console.WriteLine( $"Submitting {this.ItemCount} items." );
    }
}

public static class Program
{
    public static void Main()
    {
        var viewModel = new OrderViewModel { ItemCount = 3 };

        Console.WriteLine( $"Can submit: {viewModel.SubmitCommand.CanExecute( null )}" );
        viewModel.SubmitCommand.Execute( null );
    }
}
Can submit: True
Submitting 3 items.

See Also

Reference

CommandAttribute
ICommand

Other Resources

Command