| Severity | Error |
| Reported by | PostSharp.Patterns.Xaml |
| Message | Error in the [Command] aspect applied to property <Property>: type <Type> does not contain any <MemberKind> named <Name>. |
Cause
By default, the CommandAttribute aspect finds the members of a command by naming conventions: for a SubmitCommand property, an ExecuteSubmit or Submit method, and an optional CanExecuteSubmit or CanSubmit method or property. When you set the ExecuteMethod, CanExecuteMethod or CanExecuteProperty property of the aspect, the aspect looks for a member with exactly this name instead, and reports XAM071 when the class does not have one.
<MemberKind> is method or property, depending on the aspect property that you set. The aspect only looks at the members declared in the class of the command property itself: a member that a base class declares is not found either. PostSharp reports the message on the command property.
In the following example, the ExecuteMethod property names a Submit method, but the method is named SubmitOrder.
using System;
using System.Windows.Input;
using PostSharp.Patterns.Xaml;
namespace Errors.XAM071_Error;
// The [Command] aspect initializes the command properties.
#pragma warning disable CS8618
public class OrderViewModel
{
// The class has no method named Submit: the method is SubmitOrder.
[Command( ExecuteMethod = "Submit" )]
public ICommand SubmitCommand { get; private set; }
public void SubmitOrder()
{
Console.WriteLine( "The order is submitted." );
}
}
public static class Program
{
public static void Main()
{
new OrderViewModel().SubmitCommand.Execute( null );
}
}
error XAM071: XAM071_Error.cs(14,21): Error in the [Command] aspect applied to property Errors.XAM071_Error.OrderViewModel.SubmitCommand: type Errors.XAM071_Error.OrderViewModel does not contain any method named Submit.
Resolution
- Correct the name in the aspect property. Use the
nameofoperator rather than a string literal, so that the C# compiler checks the name and a rename updates it. - When the member is declared in a base class, declare it in the class of the command property, for example as a method that calls the member of the base class.
- Remove the aspect property and name the members by the conventions of the aspect.
In the following example, the aspect names the method with nameof.
using System;
using System.Windows.Input;
using PostSharp.Patterns.Xaml;
namespace Errors.XAM071_Fixed;
// The [Command] aspect initializes the command properties.
#pragma warning disable CS8618
public class OrderViewModel
{
// nameof makes the C# compiler check the name of the method.
[Command( ExecuteMethod = nameof(SubmitOrder) )]
public ICommand SubmitCommand { get; private set; }
public void SubmitOrder()
{
Console.WriteLine( "The order is submitted." );
}
}
public static class Program
{
public static void Main()
{
new OrderViewModel().SubmitCommand.Execute( null );
}
}
The order is submitted.
See Also
Reference
Other Resources