summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WP8/TabbedPageRenderer.cs
blob: 9192fc6f3d43d078ac0f5b1586bdd93ed86532b5 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
´╗┐using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Microsoft.Phone.Controls;

namespace Xamarin.Forms.Platform.WinPhone
{
	public class TabbedPagePresenter : System.Windows.Controls.ContentPresenter
	{
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();

			DependencyObject parent = VisualTreeHelper.GetParent(this);
			while (parent != null && !(parent is PivotItem))
				parent = VisualTreeHelper.GetParent(parent);

			var pivotItem = parent as PivotItem;
			if (pivotItem == null)
				throw new Exception("No parent PivotItem found for tab");

			pivotItem.SizeChanged += (s, e) =>
			{
				if (pivotItem.ActualWidth > 0 && pivotItem.ActualHeight > 0)
				{
					var tab = (Page)DataContext;
					((TabbedPage)tab.RealParent).ContainerArea = new Rectangle(0, 0, pivotItem.ActualWidth, pivotItem.ActualHeight);
				}
			};
		}
	}

	public class TabbedPageRenderer : Pivot, IVisualElementRenderer
	{
		TabbedPage _page;
		BackgroundTracker<Control> _tracker;

		public TabbedPageRenderer()
		{
			SetBinding(TitleProperty, new System.Windows.Data.Binding("Title"));
			SetBinding(ItemsSourceProperty, new System.Windows.Data.Binding("Children"));
			HeaderTemplate = (System.Windows.DataTemplate)System.Windows.Application.Current.Resources["TabbedPageHeader"];
			ItemTemplate = (System.Windows.DataTemplate)System.Windows.Application.Current.Resources["TabbedPage"];

			SelectionChanged += OnSelectionChanged;
		}

		public UIElement ContainerElement
		{
			get { return this; }
		}

		public VisualElement Element
		{
			get { return _page; }
		}

		public event EventHandler<VisualElementChangedEventArgs> ElementChanged;

		public SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint)
		{
			return new SizeRequest(new Size(widthConstraint, heightConstraint));
		}

		public void SetElement(VisualElement element)
		{
			TabbedPage oldElement = _page;
			_page = (TabbedPage)element;
			_tracker = new BackgroundTracker<Control>(BackgroundProperty) { Model = _page, Element = this };

			DataContext = element;

			_page.PropertyChanged += OnPropertyChanged;

			Loaded += (sender, args) => _page.SendAppearing();
			Unloaded += (sender, args) => _page.SendDisappearing();

			OnElementChanged(new VisualElementChangedEventArgs(_page, element));
		}

		protected virtual void OnElementChanged(VisualElementChangedEventArgs e)
		{
			EventHandler<VisualElementChangedEventArgs> changed = ElementChanged;
			if (changed != null)
				changed(this, e);
		}

		void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName == "CurrentPage")
			{
				Page current = _page.CurrentPage;
				if (current != null)
					SelectedItem = current;
			}
		}

		void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			_page.CurrentPage = (Page)SelectedItem;
		}
	}
}