| Severity | Error |
| Reported by | PostSharp.Patterns.Xaml |
| Message | Error in the [Command] aspect applied to property <Property>: the CanExecuteMethod and CanExecuteProperty properties cannot both be set. |
Cause
The CommandAttribute aspect implements the CanExecute(object) method of the command with a single member of the class: either a method or a property. When you set both the CanExecuteMethod and the CanExecuteProperty properties of the aspect, it cannot tell which one to use, and reports XAM072 on the command property. The aspect checks this before it looks for the two members, so the message does not depend on whether they exist.
In the following example, the aspect names both a CanSubmitOrder method and a HasItems property.
using System;
using System.Windows.Input;
using PostSharp.Patterns.Xaml;
namespace Errors.XAM072_Error;
// The [Command] aspect initializes the command properties.
#pragma warning disable CS8618
public class OrderViewModel
{
public int ItemCount { get; set; }
// The aspect cannot decide between the method and the property.
[Command( ExecuteMethod = nameof(SubmitOrder), CanExecuteMethod = nameof(CanSubmitOrder), CanExecuteProperty = nameof(HasItems) )]
public ICommand SubmitCommand { get; private set; }
public bool HasItems => this.ItemCount > 0;
public bool CanSubmitOrder() => this.ItemCount > 0;
public void SubmitOrder()
{
Console.WriteLine( $"The order with {this.ItemCount} items is submitted." );
}
}
public static class Program
{
public static void Main()
{
var viewModel = new OrderViewModel();
Console.WriteLine( $"Can submit an empty order: {viewModel.SubmitCommand.CanExecute( null )}" );
viewModel.ItemCount = 2;
Console.WriteLine( $"Can submit an order with items: {viewModel.SubmitCommand.CanExecute( null )}" );
viewModel.SubmitCommand.Execute( null );
}
}
error XAM072: XAM072_Error.cs(16,21): Error in the [Command] aspect applied to property Errors.XAM072_Error.OrderViewModel.SubmitCommand: the CanExecuteMethod and CanExecuteProperty properties cannot both be set.
Resolution
Set only one of the two properties:
- Use CanExecuteMethod when the availability of the command depends on the command parameter: the method can have one parameter.
- Use CanExecuteProperty otherwise. In a class with the NotifyPropertyChangedAttribute aspect, the command then raises its CanExecuteChanged event when the property changes.
In the following example, the aspect only names the CanSubmitOrder method.
using System;
using System.Windows.Input;
using PostSharp.Patterns.Xaml;
namespace Errors.XAM072_Fixed;
// The [Command] aspect initializes the command properties.
#pragma warning disable CS8618
public class OrderViewModel
{
public int ItemCount { get; set; }
// Only the CanExecute method is set.
[Command( ExecuteMethod = nameof(SubmitOrder), CanExecuteMethod = nameof(CanSubmitOrder) )]
public ICommand SubmitCommand { get; private set; }
public bool HasItems => this.ItemCount > 0;
public bool CanSubmitOrder() => this.ItemCount > 0;
public void SubmitOrder()
{
Console.WriteLine( $"The order with {this.ItemCount} items is submitted." );
}
}
public static class Program
{
public static void Main()
{
var viewModel = new OrderViewModel();
Console.WriteLine( $"Can submit an empty order: {viewModel.SubmitCommand.CanExecute( null )}" );
viewModel.ItemCount = 2;
Console.WriteLine( $"Can submit an order with items: {viewModel.SubmitCommand.CanExecute( null )}" );
viewModel.SubmitCommand.Execute( null );
}
}
Can submit an empty order: False
Can submit an order with items: True
The order with 2 items is submitted.
See Also
Reference
Other Resources