-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathFrameworkElementState.cs
56 lines (49 loc) · 1.66 KB
/
FrameworkElementState.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
//
// Copyright Fela Ameghino 2015-2025
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
using System.Diagnostics;
using Windows.UI.Xaml;
namespace Telegram.Common
{
public partial class FrameworkElementState
{
public FrameworkElementState(FrameworkElement element)
{
element.Loaded += OnChanged;
element.Unloaded += OnChanged;
}
~FrameworkElementState()
{
Debug.WriteLine("~LifecycleManager");
}
private bool _loaded;
private bool _unloaded;
public bool IsLoaded => _loaded;
public bool IsUnloaded => _unloaded;
public event RoutedEventHandler Loaded;
public event RoutedEventHandler Unloaded;
private void OnChanged(object sender, RoutedEventArgs e)
{
// TODO: unfortunately FrameworkElement.Parent returns null
// whenever the control is a DataTemplate root or similar,
// hence we're forced to use VisualTreeHelper here, but I'm quite sure it's slower.
var element = sender as FrameworkElement;
var parent = element.Parent ?? Windows.UI.Xaml.Media.VisualTreeHelper.GetParent(element);
if (parent != null && !_loaded)
{
_loaded = true;
_unloaded = false;
Loaded?.Invoke(this, e);
}
else if (parent == null && _loaded)
{
_loaded = false;
_unloaded = true;
Unloaded?.Invoke(sender, e);
}
}
}
}