| Severity | Error |
| Reported by | PostSharp.Patterns.Xaml |
| Message | Error in the [Command] aspect applied to property <Property>: type <Type> contains more than one method named "<Name>" or "<AlternateName>". Keep a single method with these names. |
Cause
The CommandAttribute aspect binds a command to exactly one Execute method and at most one CanExecute method of its class. For a SaveCommand property, it looks by default for a method named ExecuteSave or Save, and for a method named CanExecuteSave or CanSave. When you set the ExecuteMethod or CanExecuteMethod property, it looks for this name only.
PostSharp reports XAM075 on the command property when the class declares more than one method with these names, whatever their signatures: two methods that each follow one of the conventions, or overloads of the same method. When the name comes from an aspect property, <AlternateName> is empty.
In the following example, the SaveCommand property matches both the ExecuteSave method and the Save method, which ExecuteSave calls.
using System;
using System.Windows.Input;
using PostSharp.Patterns.Xaml;
namespace Errors.XAM075_Error;
// The [Command] aspect initializes the command properties.
#pragma warning disable CS8618
public class DocumentViewModel
{
// By convention, the aspect looks for a method named ExecuteSave or Save, and finds two.
[Command]
public ICommand SaveCommand { get; private set; }
public void ExecuteSave()
{
this.Save( "document.txt" );
}
public void Save( string fileName )
{
Console.WriteLine( $"The document is saved to {fileName}." );
}
}
public static class Program
{
public static void Main()
{
new DocumentViewModel().SaveCommand.Execute( null );
}
}
error XAM075: XAM075_Error.cs(14,21): Error in the [Command] aspect applied to property SaveCommand: type DocumentViewModel contains more than one method named "ExecuteSave" or "Save". Keep a single method with these names.
Resolution
- Rename the methods that do not implement the command, so that only one method of the class has one of the names that the message shows.
- When the command method has overloads, keep a single method with this name, with zero parameters or one parameter that receives the command parameter.
In the following example, the helper method is renamed SaveAs.
using System;
using System.Windows.Input;
using PostSharp.Patterns.Xaml;
namespace Errors.XAM075_Fixed;
// The [Command] aspect initializes the command properties.
#pragma warning disable CS8618
public class DocumentViewModel
{
// Only ExecuteSave matches the naming convention.
[Command]
public ICommand SaveCommand { get; private set; }
public void ExecuteSave()
{
this.SaveAs( "document.txt" );
}
public void SaveAs( string fileName )
{
Console.WriteLine( $"The document is saved to {fileName}." );
}
}
public static class Program
{
public static void Main()
{
new DocumentViewModel().SaveCommand.Execute( null );
}
}
The document is saved to document.txt.
See Also
Reference
Other Resources