| Severity | Error |
| Reported by | PostSharp.Patterns.Xaml |
| Message | Error in the [Command] aspect applied to property <Property>: the signature of method <Method> is incorrect. The Execute method must be an instance method with the signature "void MyExecute()" or "void MyExecute(ParamType)". |
Cause
The CommandAttribute aspect implements the Execute(object) method of the command by calling the Execute method of the class: the method that follows the naming conventions, such as ExecuteSave for a SaveCommand property, or the method named by the ExecuteMethod property. The aspect reports XAM077 when this method:
- returns a value instead of
void; - is
static; - has more than one parameter.
The visibility of the method does not matter, and its single parameter, when it has one, can be of any type: it receives the command parameter.
PostSharp reports the message on the command property, not on the method. In the current version, <Method> shows the name of an internal type of the aspect, PostSharp.Patterns.Xaml.CommandAttribute+ExecuteMethodWrapper, instead of the name of the method.
In the following example, the ExecuteSave method returns a bool.
using System;
using System.Windows.Input;
using PostSharp.Patterns.Xaml;
namespace Errors.XAM077_Error;
// The [Command] aspect initializes the command properties.
#pragma warning disable CS8618
public class DocumentViewModel
{
public bool IsSaved { get; private set; }
[Command]
public ICommand SaveCommand { get; private set; }
// An Execute method must return void.
public bool ExecuteSave()
{
this.IsSaved = true;
return this.IsSaved;
}
}
public static class Program
{
public static void Main()
{
var viewModel = new DocumentViewModel();
viewModel.SaveCommand.Execute( null );
Console.WriteLine( $"Saved: {viewModel.IsSaved}" );
}
}
error XAM077: XAM077_Error.cs(15,21): Error in the [Command] aspect applied to property Errors.XAM077_Error.DocumentViewModel.SaveCommand: the signature of method Errors.XAM077_Error.DocumentViewModel.ExecuteSave is incorrect. The Execute method must be an instance method with the signature "void MyExecute()" or "void MyExecute(ParamType)".
Resolution
Change the method to a void instance method with zero or one parameter. When the caller needs a result, expose it in a property of the view model, which the view can bind to. When the method needs several values, pass them in one object as the command parameter.
In the following example, ExecuteSave returns void and the result is in the IsSaved property.
using System;
using System.Windows.Input;
using PostSharp.Patterns.Xaml;
namespace Errors.XAM077_Fixed;
// The [Command] aspect initializes the command properties.
#pragma warning disable CS8618
public class DocumentViewModel
{
public bool IsSaved { get; private set; }
[Command]
public ICommand SaveCommand { get; private set; }
// The Execute method returns void; the view model exposes the result in a property.
public void ExecuteSave()
{
this.IsSaved = true;
}
}
public static class Program
{
public static void Main()
{
var viewModel = new DocumentViewModel();
viewModel.SaveCommand.Execute( null );
Console.WriteLine( $"Saved: {viewModel.IsSaved}" );
}
}
Saved: True
See Also
Reference
Other Resources