summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WP8/NavigationMenuRenderer.cs
blob: 83af320b7eb7724ce4ea1e2d0131e8be555ca3b5 (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
´╗┐using System;
using System.ComponentModel;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Controls;

namespace Xamarin.Forms.Platform.WinPhone
{
	internal class NavigationMenuRenderer : ViewRenderer<NavigationMenu, System.Windows.Controls.Grid>
	{
		const int Spacing = 12;

		protected override void OnElementChanged(ElementChangedEventArgs<NavigationMenu> e)
		{
			base.OnElementChanged(e);

			var grid = new System.Windows.Controls.Grid();
			grid.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition { Width = System.Windows.GridLength.Auto });
			grid.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition { Width = System.Windows.GridLength.Auto });

			UpdateItems(grid);
			SetNativeControl(grid);
		}

		protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			base.OnElementPropertyChanged(sender, e);

			switch (e.PropertyName)
			{
				case "Targets":
					UpdateItems(Control);
					break;
			}
		}

		TileSize GetSize()
		{
			return RenderSize.Width >= 210 * 2 + Spacing ? TileSize.Medium : TileSize.Default;
		}

		void UpdateItems(System.Windows.Controls.Grid grid)
		{
			grid.Children.Clear();

			grid.RowDefinitions.Clear();

			var x = 0;
			var y = 0;
			foreach (Page target in Element.Targets)
			{
				if (x > 1)
				{
					x = 0;
					y++;
				}

				if (x == 0)
					grid.RowDefinitions.Add(new System.Windows.Controls.RowDefinition());

				var hubTile = new HubTile { Title = target.Title, Source = new BitmapImage(new Uri(target.Icon, UriKind.Relative)), Margin = new System.Windows.Thickness(0, 0, Spacing, Spacing) };

				if (target.BackgroundColor != Color.Default)
					hubTile.Background = target.BackgroundColor.ToBrush();

				Page tmp = target;
				hubTile.Tap += (sender, args) => Element.SendTargetSelected(tmp);

				hubTile.SetValue(System.Windows.Controls.Grid.RowProperty, y);
				hubTile.SetValue(System.Windows.Controls.Grid.ColumnProperty, x);
				hubTile.Size = GetSize();

				var weakRef = new WeakReference(hubTile);
				SizeChanged += (sender, args) =>
				{
					var hTile = (HubTile)weakRef.Target;
					if (hTile != null)
						hTile.Size = GetSize();
					((IVisualElementController)Element).NativeSizeChanged();
				};

				x++;
				grid.Children.Add(hubTile);
			}
		}
	}
}