summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core/Internals/NameScope.cs
blob: b29534b67fe68a656ac4a99b73b342582d81a7bf (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
using System;
using System.Collections.Generic;
using System.Xml;
using Xamarin.Forms.Xaml;

namespace Xamarin.Forms.Internals
{
	public class NameScope : INameScope
	{
		public static readonly BindableProperty NameScopeProperty = BindableProperty.CreateAttached("NameScope", typeof(INameScope), typeof(NameScope), default(INameScope));

		readonly Dictionary<string, object> _names = new Dictionary<string, object>();

		object INameScope.FindByName(string name)
		{
			if (_names.ContainsKey(name))
				return _names[name];
			return null;
		}

		void INameScope.RegisterName(string name, object scopedElement)
		{
			if (_names.ContainsKey(name))
				throw new ArgumentException("An element with the same key already exists in NameScope", "name");

			_names[name] = scopedElement;
		}

		void INameScope.RegisterName(string name, object scopedElement, IXmlLineInfo xmlLineInfo)
		{
			try
			{
				((INameScope)this).RegisterName(name, scopedElement);
			}
			catch (ArgumentException)
			{
				throw new XamlParseException(string.Format("An element with the name \"{0}\" already exists in this NameScope", name), xmlLineInfo);
			}
		}

		void INameScope.UnregisterName(string name)
		{
			_names.Remove(name);
		}

		public static INameScope GetNameScope(BindableObject bindable)
		{
			return (INameScope)bindable.GetValue(NameScopeProperty);
		}

		public static void SetNameScope(BindableObject bindable, INameScope value)
		{
			bindable.SetValue(NameScopeProperty, value);
		}
	}
}