| Severity | Error |
| Reported by | PostSharp.Patterns.Xaml |
| Message | Error in the [Command] aspect applied to property <CommandProperty>: the signature of property <CanExecuteProperty> is incorrect. The CanExecute property must be a non-static and non-private property of type bool with a getter, for example "bool MyCanExecute { get; }" or "bool MyCanExecute { get; set; }". |
Cause
The CommandAttribute aspect can implement the CanExecute(object) method of the command by reading a property of the view model. It looks for a property named CanExecute<Name> or Can<Name>, where <Name> is the name of the command property without the Command suffix, or for the property named by the CanExecuteProperty property of the aspect. A CanExecute method with one of these names takes precedence over the property, unless CanExecuteProperty is set.
The aspect reports XAM079 when this property cannot implement CanExecute, because:
- its type is not
bool; - it has no getter;
- its getter is static;
- its getter is private. Public, protected and internal getters are accepted.
PostSharp reports the message on the command property, not on the CanExecute property. In the current version, <CanExecuteProperty> shows the name of an internal type of the aspect instead of the name of the property.
In the following example, the CanExecuteSubmit property of the SubmitCommand property is private.
using System;
using System.Windows.Input;
using PostSharp.Patterns.Xaml;
namespace Errors.XAM079_Error;
// The [Command] aspect initializes the command property.
#pragma warning disable CS8618
public class OrderViewModel
{
public int ItemCount { get; set; }
// The CanExecuteSubmit property of this command is private.
[Command]
public ICommand SubmitCommand { get; private set; }
private bool CanExecuteSubmit => this.ItemCount > 0;
public void ExecuteSubmit()
{
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 XAM079: XAM079_Error.cs(16,21): Error in the [Command] aspect applied to property Errors.XAM079_Error.OrderViewModel.SubmitCommand: the signature of property Errors.XAM079_Error.OrderViewModel.CanExecuteSubmit is incorrect. The CanExecute property must be a non-static and non-private property of type bool with a getter, for example "bool MyCanExecute { get; }" or "bool MyCanExecute { get; set; }".
Resolution
Make the CanExecute property an instance property of type bool with a getter that is not private. If the property is not meant to implement CanExecute, rename it, or set CanExecuteProperty to the name of another property.
In the following example, CanExecuteSubmit is public.
using System;
using System.Windows.Input;
using PostSharp.Patterns.Xaml;
namespace Errors.XAM079_Fixed;
// The [Command] aspect initializes the command property.
#pragma warning disable CS8618
public class OrderViewModel
{
public int ItemCount { get; set; }
// The CanExecuteSubmit property of this command is public.
[Command]
public ICommand SubmitCommand { get; private set; }
public bool CanExecuteSubmit => this.ItemCount > 0;
public void ExecuteSubmit()
{
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
Other Resources