summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/NavigationMenu.cs
blob: 5be803ceac7c628c91495d27baa3c213b3bb11b1 (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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Xamarin.Forms.Platform;

namespace Xamarin.Forms.Internals
{
	// Mark as internal until renderers are ready for release after 1.0
	[RenderWith(typeof(_NavigationMenuRenderer))]
	[EditorBrowsable(EditorBrowsableState.Never)]
	public class NavigationMenu : View, INavigationMenuController, IElementConfiguration<NavigationMenu>
	{
		readonly List<Page> _targets = new List<Page>();

		readonly Lazy<PlatformConfigurationRegistry<NavigationMenu>> _platformConfigurationRegistry;

		public NavigationMenu()
		{
			_platformConfigurationRegistry = new Lazy<PlatformConfigurationRegistry<NavigationMenu>>(() => new PlatformConfigurationRegistry<NavigationMenu>(this));
		}

		public IEnumerable<Page> Targets
		{
			get { return _targets; }
			set
			{
				if (_targets.AsEnumerable().SequenceEqual(value))
					return;

				foreach (Page page in value)
				{
					VerifyTarget(page);
				}

				OnPropertyChanging();
				_targets.Clear();
				_targets.AddRange(value);
				OnPropertyChanged();
			}
		}

		public void Add(Page target)
		{
			if (_targets.Contains(target))
				return;
			VerifyTarget(target);

			OnPropertyChanging("Targets");
			_targets.Add(target);
			OnPropertyChanged("Targets");
		}

		public void Remove(Page target)
		{
			if (_targets.Contains(target))
			{
				OnPropertyChanging("Targets");
				if (_targets.Remove(target))
					OnPropertyChanged("Targets");
			}
		}

		public IPlatformElementConfiguration<T, NavigationMenu> On<T>() where T : IConfigPlatform
		{
			return _platformConfigurationRegistry.Value.On<T>();
		}

		[EditorBrowsable(EditorBrowsableState.Never)]
		public void SendTargetSelected(Page target)
		{
			Navigation.PushAsync(target);
		}

		void VerifyTarget(Page target)
		{
			if (target.Icon == null || string.IsNullOrWhiteSpace(target.Icon.File))
				throw new Exception("Icon must be set for each page before adding them to a Navigation Menu");
		}
	}
}