This repository has been archived by the owner on Nov 21, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
SafeBehavior.cs
76 lines (65 loc) · 2.42 KB
/
SafeBehavior.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// ****************************************************************************
// <copyright file="SafeBehavior.cs" company="Pedro Lamas">
// Copyright © Pedro Lamas 2011
// </copyright>
// ****************************************************************************
// <author>Pedro Lamas</author>
// <email>pedrolamas@gmail.com</email>
// <date>29-11-2011</date>
// <project>Cimbalino.Phone.Toolkit</project>
// <web>http://www.pedrolamas.com</web>
// <license>
// See license.txt in this solution or http://www.pedrolamas.com/license_MIT.txt
// </license>
// ****************************************************************************
using System.Windows;
using System.Windows.Interactivity;
namespace Cimbalino.Phone.Toolkit.Behaviors
{
/// <summary>
/// A BaseBehavior that ensures all resources are released when the <see cref="Behavior.AssociatedObject"/> is unloaded or detached.
/// </summary>
/// <typeparam name="T">The <see cref="Behavior.AssociatedObject"/> type.</typeparam>
public abstract class SafeBehavior<T> : Behavior<T>
where T : FrameworkElement
{
private bool _isCleanedUp;
/// <summary>
/// Called after the behavior is attached to an AssociatedObject.
/// </summary>
/// <remarks>Override this to hook up functionality to the AssociatedObject.</remarks>
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Unloaded += AssociatedObjectUnloaded;
}
/// <summary>
/// Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred.
/// </summary>
/// <remarks>Override this to unhook functionality from the AssociatedObject.</remarks>
protected override void OnDetaching()
{
InternalCleanUp();
base.OnDetaching();
}
/// <summary>
/// Releases all resources used by this instance.
/// </summary>
protected virtual void CleanUp()
{
}
private void InternalCleanUp()
{
if (!_isCleanedUp)
{
_isCleanedUp = true;
AssociatedObject.Unloaded -= AssociatedObjectUnloaded;
CleanUp();
}
}
private void AssociatedObjectUnloaded(object sender, RoutedEventArgs e)
{
InternalCleanUp();
}
}
}