Open sandboxFocusImprove this doc

XAM058: Property Change Handler Has an Invalid Signature

Severity Error
Reported by PostSharp.Patterns.Xaml
Message Error in the [<Aspect>] aspect applied to property <Property>: the change handling method <Method> does not have a correct signature. Correct signatures are: "void MyPropertyChangedHandler(DependencyProperty property, <DeclaringType> instance)" for static methods and "void MyPropertyChangedHandler(DependencyProperty property)" for instance methods. Parameters "property" and "instance" are optional and may be omitted in the method signature.

Cause

The DependencyPropertyAttribute and AttachedPropertyAttribute aspects call a change handler method after the value of the property changes. They find the method by its name: On followed by the name of the property and Changed, or the name that you set in the <xref:PostSharp.Patterns.Xaml.DependencyPropertyBaseAttribute.PropertyChangedMethod> property of the aspect. PostSharp reports XAM058 when the parameters of this method do not match one of the supported signatures:

The handler does not receive the old or the new value: it reads the property instead. <DeclaringType> is the short name of the class. The current version of the message shows the name of the property without its type, such as Quantity. PostSharp reports the message on the property.

In the following example, the OnQuantityChanged method takes the new value as an int parameter.

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

namespace Errors.XAM058_Error;

public class OrderLine : DependencyObject
{
    public const decimal UnitPrice = 12.5m;

    [DependencyProperty]
    public int Quantity { get; set; }

    // A change handler does not receive the new value of the property.
    private void OnQuantityChanged( int quantity ) => Console.WriteLine( $"Total: {quantity * UnitPrice}" );
}

public static class Program
{
    public static void Main()
    {
        var line = new OrderLine();
        line.Quantity = 2;
        line.Quantity = 3;
    }
}
error XAM058: XAM058_Error.cs(12,16): Error in the [DependencyProperty] aspect applied to property Quantity: the change handling method Errors.XAM058_Error.OrderLine.OnQuantityChanged(System.Int32) does not have a correct signature. Correct signatures are: "void MyPropertyChangedHandler(DependencyProperty property, OrderLine instance)" for static methods and "void MyPropertyChangedHandler(DependencyProperty property)" for instance methods. Parameters "property" and "instance" are optional and may be omitted in the method signature.

Resolution

Change the parameters of the change handler to one of the supported signatures, and read the new value from the property. When the method has the name of the convention but is not meant to handle the changes of the property, rename it, or set <xref:PostSharp.Patterns.Xaml.DependencyPropertyBaseAttribute.PropertyChangedMethod> to the name of another method.

In the following example, OnQuantityChanged takes no parameter and reads the Quantity property.

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

namespace Errors.XAM058_Fixed;

public class OrderLine : DependencyObject
{
    public const decimal UnitPrice = 12.5m;

    [DependencyProperty]
    public int Quantity { get; set; }

    // The change handler takes no parameter and reads the new value from the property.
    private void OnQuantityChanged() => Console.WriteLine( $"Total: {this.Quantity * UnitPrice}" );
}

public static class Program
{
    public static void Main()
    {
        var line = new OrderLine();
        line.Quantity = 2;
        line.Quantity = 3;
    }
}
Total: 25.0
Total: 37.5

See Also

Reference

DependencyPropertyAttribute
AttachedPropertyAttribute
<xref:PostSharp.Patterns.Xaml.DependencyPropertyBaseAttribute.PropertyChangedMethod>

Other Resources

Dependency Property
Attached Property