using System; using System.Collections.Generic; using Xamarin.Forms.Internals; namespace Xamarin.Forms { #pragma warning disable 612 public class ElementTemplate : IElement, IDataTemplate #pragma warning restore 612 { List> _changeHandlers; Element _parent; internal ElementTemplate() { } internal ElementTemplate(Type type) : this() { if (type == null) throw new ArgumentNullException("type"); LoadTemplate = () => Activator.CreateInstance(type); } internal ElementTemplate(Func loadTemplate) : this() { if (loadTemplate == null) throw new ArgumentNullException("loadTemplate"); LoadTemplate = loadTemplate; } Func LoadTemplate { get; set; } #pragma warning disable 0612 Func IDataTemplate.LoadTemplate { #pragma warning restore 0612 get { return LoadTemplate; } set { LoadTemplate = value; } } void IElement.AddResourcesChangedListener(Action onchanged) { _changeHandlers = _changeHandlers ?? new List>(1); _changeHandlers.Add(onchanged); } Element IElement.Parent { get { return _parent; } set { if (_parent == value) return; if (_parent != null) ((IElement)_parent).RemoveResourcesChangedListener(OnResourcesChanged); _parent = value; if (_parent != null) ((IElement)_parent).AddResourcesChangedListener(OnResourcesChanged); } } void IElement.RemoveResourcesChangedListener(Action onchanged) { if (_changeHandlers == null) return; _changeHandlers.Remove(onchanged); } public object CreateContent() { if (LoadTemplate == null) throw new InvalidOperationException("LoadTemplate should not be null"); if (this is DataTemplateSelector) throw new InvalidOperationException("Cannot call CreateContent directly on a DataTemplateSelector"); object item = LoadTemplate(); SetupContent(item); return item; } internal virtual void SetupContent(object item) { } void OnResourcesChanged(object sender, ResourcesChangedEventArgs e) { if (_changeHandlers == null) return; foreach (Action handler in _changeHandlers) handler(this, e); } } }