[EditorBrowsable(EditorBrowsableState.Never)]
public abstract partial class Element : BindableObject, IElement, INameScope, IElementController
{
-
- // public static readonly BindableProperty MenuProperty = BindableProperty.CreateAttached(nameof(Menu), typeof(Menu), typeof(Element), null);
-
- // public static Menu GetMenu(BindableObject bindable)
- // {
- // return (Menu)bindable.GetValue(MenuProperty);
- // }
-
- // public static void SetMenu(BindableObject bindable, Menu menu)
- // {
- // bindable.SetValue(MenuProperty, menu);
- // }
-
internal static readonly ReadOnlyCollection<Element> EmptyChildren = new ReadOnlyCollection<Element>(new Element[0]);
/// <summary>
Dictionary<BindableProperty, string> _dynamicResources;
- IEffectControlProvider _effectControlProvider;
-
- TrackableCollection<Effect> _effects;
-
Guid? _id;
Element _parentOverride;
- IPlatform _platform;
-
string _styleId;
/// <summary>
set { SetValue(ClassIdProperty, value); }
}
- internal IList<Effect> Effects
- {
- get
- {
- if (_effects == null)
- {
- _effects = new TrackableCollection<Effect>();
- _effects.CollectionChanged += EffectsOnCollectionChanged;
- _effects.Clearing += EffectsOnClearing;
- }
- return _effects;
- }
- }
-
/// <summary>
/// Gets a value that can be used to uniquely identify an element through the run of an application.
/// </summary>
/// <summary>
/// For internal use.
/// </summary>
- internal IPlatform Platform
- {
- get
- {
- if (_platform == null && RealParent != null)
- return RealParent.Platform;
- return _platform;
- }
- set
- {
- if (_platform == value)
- return;
- _platform = value;
- PlatformSet?.Invoke(this, EventArgs.Empty);
- foreach (Element descendant in Descendants())
- {
- descendant._platform = _platform;
- descendant.PlatformSet?.Invoke(this, EventArgs.Empty);
- }
- }
- }
-
- /// <summary>
- /// For internal use.
- /// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public Element RealParent { get; private set; }
OnParentSet();
- if (RealParent != null)
- {
- IPlatform platform = RealParent.Platform;
- if (platform != null)
- Platform = platform;
- }
-
OnPropertyChanged();
}
}
_changeHandlers.Remove(onchanged);
}
- /// <summary>
- /// For internal use.
- /// </summary>
- internal IEffectControlProvider EffectControlProvider
- {
- get { return _effectControlProvider; }
- set
- {
- if (_effectControlProvider == value)
- return;
- if (_effectControlProvider != null && _effects != null)
- {
- foreach (Effect effect in _effects)
- effect?.SendDetached();
- }
- _effectControlProvider = value;
- if (_effectControlProvider != null && _effects != null)
- {
- foreach (Effect effect in _effects)
- {
- if (effect != null)
- AttachEffect(effect);
- }
- }
- }
- }
-
//void IElementController.SetValueFromRenderer(BindableProperty property, object value) => SetValueFromRenderer(property, value);
/// <summary>
SetValueCore(property, value);
}
- /// <summary>
- /// For internal use.
- /// </summary>
- /// <param name="name">The nameof the effect</param>
- /// <returns>true if attached</returns>
- [EditorBrowsable(EditorBrowsableState.Never)]
- public bool EffectIsAttached(string name)
- {
- foreach (var effect in Effects)
- {
- if (effect.ResolveId == name)
- return true;
- }
- return false;
- }
-
object INameScope.FindByName(string name)
{
INameScope namescope = GetNameScope();
protected virtual void OnChildAdded(Element child)
{
child.Parent = this;
- if (Platform != null)
- child.Platform = Platform;
child.ApplyBindings(skipBindingContext: false, fromBindingContextChanged:true);
protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
base.OnPropertyChanged(propertyName);
-
- if (_effects == null || _effects.Count == 0)
- return;
-
- var args = new PropertyChangedEventArgs(propertyName);
- foreach (Effect effect in _effects)
- {
- effect?.SendOnElementPropertyChanged(args);
- }
}
/// <summary>
{
}
- /// <summary>
- /// For internal use.
- /// </summary>
- [EditorBrowsable(EditorBrowsableState.Never)]
- public event EventHandler PlatformSet;
-
internal virtual void SetChildInheritedBindingContext(Element child, object context)
{
SetInheritedBindingContext(child, context);
}
}
- void AttachEffect(Effect effect)
- {
- if (_effectControlProvider == null)
- return;
- if (effect.IsAttached)
- throw new InvalidOperationException("Cannot attach Effect to multiple sources");
-
- Effect effectToRegister = effect;
- if (effect is RoutingEffect)
- effectToRegister = ((RoutingEffect)effect).Inner;
- _effectControlProvider.RegisterEffect(effectToRegister);
- effectToRegister.Element = this;
- effect.SendAttached();
- }
-
- void EffectsOnClearing(object sender, EventArgs eventArgs)
- {
- foreach (Effect effect in _effects)
- {
- effect?.ClearEffect();
- }
- }
-
- void EffectsOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
- {
- switch (e.Action)
- {
- case NotifyCollectionChangedAction.Add:
- foreach (Effect effect in e.NewItems)
- {
- AttachEffect(effect);
- }
- break;
- case NotifyCollectionChangedAction.Move:
- break;
- case NotifyCollectionChangedAction.Remove:
- foreach (Effect effect in e.OldItems)
- {
- effect.ClearEffect();
- }
- break;
- case NotifyCollectionChangedAction.Replace:
- foreach (Effect effect in e.NewItems)
- {
- AttachEffect(effect);
- }
- foreach (Effect effect in e.OldItems)
- {
- effect.ClearEffect();
- }
- break;
- case NotifyCollectionChangedAction.Reset:
- if (e.NewItems != null)
- {
- foreach (Effect effect in e.NewItems)
- {
- AttachEffect(effect);
- }
- }
- if (e.OldItems != null)
- {
- foreach (Effect effect in e.OldItems)
- {
- effect.ClearEffect();
- }
- }
- break;
- default:
- throw new ArgumentOutOfRangeException();
- }
- }
-
INameScope GetNameScope()
{
INameScope namescope = NameScope.GetNameScope(this);