Open sandboxFocusImprove this doc

XAM081: Execute Method of a Command Not Found

Severity Error
Reported by PostSharp.Patterns.Xaml
Message Error in the [Command] aspect applied to property <CommandProperty>: type <DeclaringType> does not contain any method named <ExecuteMethodName> or <AlternateExecuteMethodName>.

Cause

The CommandAttribute aspect implements the Execute(object) method of the command by calling a method of the view model. When the ExecuteMethod property of the aspect is not set, the aspect follows a naming convention: for a command property named <Name>Command or <Name>, it looks for a method named Execute<Name>, <ExecuteMethodName> in the message, or <Name>, <AlternateExecuteMethodName>.

The aspect reports XAM081 when <DeclaringType>, the class of the command property, declares no method with either name. Methods inherited from a base class are not considered.

When ExecuteMethod is set to the name of a method that does not exist, PostSharp reports XAM071 instead.

In the following example, the method that executes the SubmitCommand command is named SubmitOrder, which does not follow the naming convention.

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

namespace Errors.XAM081_Error;

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

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

    // No method of the class is named ExecuteSubmit or Submit.
    [Command]
    public ICommand SubmitCommand { get; private set; }

    public void SubmitOrder()
    {
        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 XAM081: XAM081_Error.cs(16,21): Error in the [Command] aspect applied to property Errors.XAM081_Error.OrderViewModel.SubmitCommand: type Errors.XAM081_Error.OrderViewModel does not contain any method named ExecuteSubmit or Submit.

Resolution

  • Set the ExecuteMethod property of the aspect to the name of the method that executes the command.
  • Otherwise, rename the method to one of the names of the message, or add it to the class of the command property if it is missing or declared in a base class.

In the following example, the aspect names the SubmitOrder method.

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

namespace Errors.XAM081_Fixed;

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

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

    // The ExecuteMethod property names the method that executes the command.
    [Command( ExecuteMethod = nameof(SubmitOrder) )]
    public ICommand SubmitCommand { get; private set; }

    public void SubmitOrder()
    {
        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