using System.Reactive.Disposables;
using Avalonia.Xaml.Interactivity;
namespace Avalonia.Xaml.Interactions.Custom;
///
/// A base class for behaviors with disposable resources.
///
/// The object type to attach to
public abstract class DisposingBehavior : StyledElementBehavior where T : AvaloniaObject
{
private CompositeDisposable? _disposables;
///
protected override void OnAttached()
{
base.OnAttached();
_disposables?.Dispose();
_disposables = new CompositeDisposable();
OnAttached(_disposables);
}
///
/// Called after the behavior is attached to the .
///
/// The group of disposable resources that are disposed together.
protected abstract void OnAttached(CompositeDisposable disposables);
///
protected override void OnDetaching()
{
base.OnDetaching();
_disposables?.Dispose();
}
}