| Severity | Error |
| Reported by | PostSharp.Patterns.Xaml |
| Message | Error in the [Command] aspect applied to property <Property>: only instance properties of type System.Windows.Input.ICommand are supported. |
Cause
The CommandAttribute aspect initializes its property with a command object that calls the Execute and CanExecute members of the instance of the class. PostSharp reports XAM070 when the property cannot hold this object:
- The property is static, so there is no instance whose members the command could call.
- The type of the property is not exactly ICommand. A class or another interface that implements ICommand is not supported either.
The aspect does not check the visibility of the property, although the current version of the message says that the property must be public. PostSharp reports the message on the property.
In the following example, the SaveCommand property is static.
using System;
using System.Windows.Input;
using PostSharp.Patterns.Xaml;
namespace Errors.XAM070_Error;
// The [Command] aspect initializes the property.
#pragma warning disable CS8618
public class DocumentViewModel
{
// A command property must be an instance property.
[Command]
public static ICommand SaveCommand { get; private set; }
private void ExecuteSave() => Console.WriteLine( "Document saved." );
}
public static class Program
{
public static void Main()
{
DocumentViewModel.SaveCommand.Execute( null );
}
}
error XAM070: XAM070_Error.cs(14,28): Error in the [Command] aspect applied to property Errors.XAM070_Error.DocumentViewModel.SaveCommand: only instance properties of type System.Windows.Input.ICommand are supported.
Resolution
Declare the command as an instance property of type ICommand, usually with a public getter and a private setter, which the aspect sets. When several objects must share a command, keep the command property in each instance and move the shared logic into the method that the command executes.
In the following example, SaveCommand is an instance property, and its command calls the ExecuteSave method.
using System;
using System.Windows.Input;
using PostSharp.Patterns.Xaml;
namespace Errors.XAM070_Fixed;
// The [Command] aspect initializes the property.
#pragma warning disable CS8618
public class DocumentViewModel
{
// The command is an instance property of type ICommand.
[Command]
public ICommand SaveCommand { get; private set; }
private void ExecuteSave() => Console.WriteLine( "Document saved." );
}
public static class Program
{
public static void Main()
{
new DocumentViewModel().SaveCommand.Execute( null );
}
}
Document saved.
See Also
Reference
Other Resources