| Severity | Error |
| Reported by | PostSharp.Patterns.Xaml |
| Message | Error in the [<Aspect>] aspect applied to property <Property>: the <MethodKind> method "<Method>" must be static. |
Cause
An attached property, which the AttachedPropertyAttribute aspect implements, is declared by one class and set on objects of any other class, such as a control in a panel. The objects whose property changes are not instances of the declaring class, so the aspect has no instance to call an instance method on. PostSharp reports XAM059 when the change handler (On followed by the name of the property and Changed, or the method named by <xref:PostSharp.Patterns.Xaml.DependencyPropertyBaseAttribute.PropertyChangedMethod>) or the validation method (Validate followed by the name of the property, or the method named by <xref:PostSharp.Patterns.Xaml.DependencyPropertyBaseAttribute.ValidateValueMethod>) is not static.
<MethodKind> is change handler or validator. PostSharp reports the message on the method, not on the property. The current version of the message shows the method between the brackets, where the name of the aspect should be.
In the following example, the OnColumnChanged change handler of the Column attached property is an instance method.
using System;
using System.Windows;
using PostSharp.Patterns.Xaml;
namespace Errors.XAM059_Error;
// The [AttachedProperty] aspect initializes the property.
#pragma warning disable CS8618
public class GridLayout : DependencyObject
{
[AttachedProperty]
public static Attached<int> Column { get; private set; }
public static void SetColumn( DependencyObject host, int value ) => Column.SetValue( host, value );
// The change handler of an attached property must be static.
private void OnColumnChanged( DependencyProperty property ) => Console.WriteLine( $"{property.Name} changed." );
}
public static class Program
{
public static void Main()
{
var host = new DependencyObject();
GridLayout.SetColumn( host, 2 );
Console.WriteLine( $"Column: {GridLayout.Column.GetValue( host )}" );
}
}
error XAM059: XAM059_Error.cs(18,18): Error in the [AttachedProperty] aspect applied to property Errors.XAM059_Error.GridLayout.Column: the change handler method "Errors.XAM059_Error.GridLayout.OnColumnChanged(System.Windows.DependencyProperty)" must be static.
Resolution
Make the method static. To know which object the property changed on, add a parameter for the instance, of type DependencyObject or object, after the optional DependencyProperty parameter: the aspect passes the object on which the attached property is set.
In the following example, the static OnColumnChanged method receives the object and reads the new value of the property on it.
using System;
using System.Windows;
using PostSharp.Patterns.Xaml;
namespace Errors.XAM059_Fixed;
// The [AttachedProperty] aspect initializes the property.
#pragma warning disable CS8618
public class GridLayout : DependencyObject
{
[AttachedProperty]
public static Attached<int> Column { get; private set; }
public static void SetColumn( DependencyObject host, int value ) => Column.SetValue( host, value );
// The static change handler receives the object whose attached property changed.
private static void OnColumnChanged( DependencyProperty property, DependencyObject host )
=> Console.WriteLine( $"{property.Name} changed to {Column.GetValue( host )}." );
}
public static class Program
{
public static void Main()
{
var host = new DependencyObject();
GridLayout.SetColumn( host, 2 );
Console.WriteLine( $"Column: {GridLayout.Column.GetValue( host )}" );
}
}
Column changed to 2.
Column: 2
See Also
Reference
AttachedPropertyAttribute
Attached<T>
Other Resources