summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WP8/VisualElementPackager.cs
blob: 65f589fc9257769877119ba98b08c9470bb147cc (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
´╗┐using System;
using System.Windows;
using System.Windows.Controls;

namespace Xamarin.Forms.Platform.WinPhone
{
	public class VisualElementPackager
	{
		readonly Panel _panel;
		readonly IVisualElementRenderer _renderer;
		bool _loaded;

		public VisualElementPackager(IVisualElementRenderer renderer)
		{
			if (renderer == null)
				throw new ArgumentNullException("renderer");

			_panel = renderer.ContainerElement as Panel;
			if (_panel == null)
				throw new ArgumentException("Renderer's container element must be a Panel or Panel subclass");

			_renderer = renderer;
		}

		public void Load()
		{
			if (_loaded)
				return;

			_loaded = true;
			_renderer.Element.ChildAdded += HandleChildAdded;
			_renderer.Element.ChildRemoved += HandleChildRemoved;
			_renderer.Element.ChildrenReordered += HandleChildrenReordered;

			foreach (Element child in _renderer.Element.LogicalChildren)
				HandleChildAdded(_renderer.Element, new ElementEventArgs(child));
		}

		void EnsureZIndex()
		{
			for (var index = 0; index < _renderer.Element.LogicalChildren.Count; index++)
			{
				var child = (VisualElement)_renderer.Element.LogicalChildren[index];
				IVisualElementRenderer r = Platform.GetRenderer(child);
				if (r == null)
					continue;
				// Even though this attached property is defined on Canvas, it actually works on all Panels
				// Why? Microsoft.
				Canvas.SetZIndex(r.ContainerElement, index + 1);
			}
		}

		void HandleChildAdded(object sender, ElementEventArgs e)
		{
			var view = e.Element as VisualElement;

			if (view == null)
				return;

			IVisualElementRenderer renderer;
			Platform.SetRenderer(view, renderer = Platform.CreateRenderer(view));

			_panel.Children.Add(renderer.ContainerElement);

			EnsureZIndex();
		}

		void HandleChildRemoved(object sender, ElementEventArgs e)
		{
			var view = e.Element as VisualElement;

			if (view == null)
				return;

			var renderer = Platform.GetRenderer(view) as UIElement;

			if (renderer != null)
				_panel.Children.Remove(renderer);

			EnsureZIndex();
		}

		void HandleChildrenReordered(object sender, EventArgs e)
		{
			EnsureZIndex();
		}
	}
}