Open sandboxFocusImprove this doc

XAM080: Manually Implemented Command Property Without Getter or Setter

Severity Error
Reported by PostSharp.Patterns.Xaml
Message Error in the [Command] aspect applied to property <CommandProperty>: a manually implemented command property must have both a getter and a setter. Add a setter, which can be private, or make the property automatic.

Cause

The CommandAttribute aspect assigns the command object that it creates to the command property through the setter of the property. The view reads it through the getter.

The aspect reports XAM080 when the command property is implemented manually, with accessor bodies, and has no getter or no setter. The accessors can have any visibility. Automatic properties are not subject to this check.

In the following example, the SubmitCommand property has a getter that returns a field, but no setter.

using System;
using System.Windows.Input;
using PostSharp.Patterns.Xaml;

namespace Errors.XAM080_Error;

public class OrderViewModel
{
    private ICommand? submitCommand = null;

    public int ItemCount { get; set; }

    // This manually implemented command property has no setter.
    [Command]
    public ICommand SubmitCommand
    {
        get { return this.submitCommand!; }
    }

    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 XAM080: XAM080_Error.cs(15,21): Error in the [Command] aspect applied to property Errors.XAM080_Error.OrderViewModel.SubmitCommand: a manually implemented command property must have both a getter and a setter. Add a setter, which can be private, or make the property automatic.

Resolution

Add a setter to the command property, which can be private, or make it an automatic property such as { get; private set; }.

In the following example, the private setter stores the command in the field that the getter returns.

using System;
using System.Windows.Input;
using PostSharp.Patterns.Xaml;

namespace Errors.XAM080_Fixed;

public class OrderViewModel
{
    private ICommand? submitCommand = null;

    public int ItemCount { get; set; }

    // The aspect assigns the command through the setter.
    [Command]
    public ICommand SubmitCommand
    {
        get { return this.submitCommand!; }
        private set { this.submitCommand = value; }
    }

    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

CommandAttribute
ICommand

Other Resources

Command