2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using Tizen.NUI.Binding.Internals;
6 namespace Tizen.NUI.Binding
9 /// Base class for DataTemplate and ControlTemplate classes.
11 [EditorBrowsable(EditorBrowsableState.Never)]
12 internal class ElementTemplate : IElement, IDataTemplate
14 List<Action<object, ResourcesChangedEventArgs>> _changeHandlers;
16 bool _canRecycle; // aka IsDeclarative
18 internal ElementTemplate()
22 internal ElementTemplate(Type type) : this()
25 throw new ArgumentNullException("type");
29 LoadTemplate = () => Activator.CreateInstance(type);
32 internal ElementTemplate(Func<object> loadTemplate) : this()
34 if (loadTemplate == null)
35 throw new ArgumentNullException("loadTemplate");
37 LoadTemplate = loadTemplate;
40 Func<object> LoadTemplate { get; set; }
42 #pragma warning disable 0612
43 Func<object> IDataTemplate.LoadTemplate
45 get { return LoadTemplate; }
46 set { LoadTemplate = value; }
48 #pragma warning restore 0612
50 void IElement.AddResourcesChangedListener(Action<object, ResourcesChangedEventArgs> onchanged)
52 _changeHandlers = _changeHandlers ?? new List<Action<object, ResourcesChangedEventArgs>>(1);
53 _changeHandlers.Add(onchanged);
56 internal bool CanRecycle => _canRecycle;
57 Element IElement.Parent
59 get { return _parent; }
65 ((IElement)_parent).RemoveResourcesChangedListener(OnResourcesChanged);
68 ((IElement)_parent).AddResourcesChangedListener(OnResourcesChanged);
72 void IElement.RemoveResourcesChangedListener(Action<object, ResourcesChangedEventArgs> onchanged)
74 if (_changeHandlers == null)
76 _changeHandlers.Remove(onchanged);
80 /// Used by the XAML infrastructure to load data templates and set up the content of the resulting UI.
82 /// <returns></returns>
83 public object CreateContent()
85 if (LoadTemplate == null)
86 throw new InvalidOperationException("LoadTemplate should not be null");
87 if (this is DataTemplateSelector)
88 throw new InvalidOperationException("Cannot call CreateContent directly on a DataTemplateSelector");
90 object item = LoadTemplate();
96 internal virtual void SetupContent(object item)
100 void OnResourcesChanged(object sender, ResourcesChangedEventArgs e)
102 if (_changeHandlers == null)
104 foreach (Action<object, ResourcesChangedEventArgs> handler in _changeHandlers)