-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDisposingBehavior.cs
39 lines (30 loc) · 1.06 KB
/
DisposingBehavior.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System.Reactive.Disposables;
using Avalonia.Xaml.Interactivity;
namespace Avalonia.Xaml.Interactions.Custom;
/// <summary>
/// A base class for behaviors with disposable resources.
/// </summary>
/// <typeparam name="T">The object type to attach to</typeparam>
public abstract class DisposingBehavior<T> : StyledElementBehavior<T> where T : AvaloniaObject
{
private CompositeDisposable? _disposables;
/// <inheritdoc />
protected override void OnAttached()
{
base.OnAttached();
_disposables?.Dispose();
_disposables = new CompositeDisposable();
OnAttached(_disposables);
}
/// <summary>
/// Called after the behavior is attached to the <see cref="IBehavior.AssociatedObject"/>.
/// </summary>
/// <param name="disposables">The group of disposable resources that are disposed together.</param>
protected abstract void OnAttached(CompositeDisposable disposables);
/// <inheritdoc />
protected override void OnDetaching()
{
base.OnDetaching();
_disposables?.Dispose();
}
}