/// <summary> /// Creates a new collection instance of the specified type. /// </summary> /// <param name="uv">The Ultraviolet context.</param> /// <param name="instance">The object instance which is being mutated.</param> /// <param name="context">The current instantiation context.</param> /// <param name="type">The type of collection to create.</param> /// <param name="collection">The collection which was created.</param> /// <returns><see langword="true"/> if the collection was able to be created; /// otherwise, <see langword="false"/>.</returns> protected Boolean CreateCollectionOfType(UltravioletContext uv, Object instance, UvmlInstantiationContext context, Type type, out Object collection) { var ctor = type.GetConstructor(Type.EmptyTypes); if (ctor == null) { collection = null; return(false); } collection = ctor.Invoke(null); return(true); }
/// <inheritdoc/> public override Object Instantiate(UltravioletContext uv, UvmlInstantiationContext context) { if (String.Equals(Literal, "{{null}}", StringComparison.Ordinal)) { return(Type.IsValueType ? Activator.CreateInstance(Type) : null); } if (typeof(IInputElement).IsAssignableFrom(Type)) { return(new UvmlElementReference(Literal)); } return(ObjectResolver.FromString(Literal, Type, Culture)); }
/// <summary> /// Creates a handler delegate for an event. /// </summary> /// <param name="name">The name of the method which will handle the event.</param> /// <param name="type">The type of delegate which handles the event.</param> /// <param name="context">The current instantiation context.</param> /// <returns>The delegate which was created to handle the event.</returns> protected static Delegate CreateEventHandlerDelegate(String name, Type type, UvmlInstantiationContext context) { var dataSource = context.DataSource; var dataSourceType = context.DataSourceType; var templatedParent = context.TemplatedParent as UIElement; if (templatedParent != null) { dataSource = templatedParent; PresentationFoundation.Instance.ComponentTemplates.Get(templatedParent, out dataSourceType); } return(BindingExpressions.CreateBoundEventDelegate(dataSource, dataSourceType, type, name)); }
/// <summary> /// Performs initialization required by instances of the <see cref="UIElement"/> class. /// </summary> private void InitializeElement(UltravioletContext uv, Object instance, UvmlInstantiationContext context) { var uiElement = instance as UIElement; if (uiElement == null) { return; } if (uiElement != null && classes != null) { foreach (var className in classes) { uiElement.Classes.Add(className); } } }
/// <inheritdoc/> public override Object Instantiate(UltravioletContext uv, UvmlInstantiationContext context) { var instance = instantiator(uv, name); if (instance == null) { throw new NullReferenceException(nameof(instance)); } InitializeFrameworkElement(uv, instance, context); InitializeElement(uv, instance, context); InitializeDependencyObject(uv, instance, context); var mutatorsWithValues = (from mutator in mutators select new { Mutator = mutator, Value = mutator.InstantiateValue(uv, instance, context) }).ToArray(); return(new UvmlTemplateInstance(instance, () => { foreach (var mutator in mutatorsWithValues) { mutator.Mutator.Mutate(uv, instance, mutator.Value, context); } InitializeContentPresenter(uv, instance, context); if (IsItemsPanelForTemplatedParent) { var itemsControl = context.TemplatedParent as ItemsControl; if (itemsControl != null) { itemsControl.ItemsPanelElement = instance as Panel; } } var fe = instance as FrameworkElement; if (fe != null) { fe.EndInit(); } })); }
/// <summary> /// Processes the specified value for use by a mutator. /// </summary> /// <typeparam name="T">The type of value which is expected.</typeparam> /// <param name="value">The value to process.</param> /// <param name="context">The current instantiation context.</param> /// <returns>The value after it has been converted to the expected type.</returns> protected static T ProcessPrecomputedValue <T>(Object value, UvmlInstantiationContext context) { var templateInstance = value as UvmlTemplateInstance; if (templateInstance != null) { return((T)templateInstance.FinalizeInstance()); } var elementReference = value as UvmlElementReference; if (elementReference != null) { return((T)elementReference.GetReferencedElement(context.Namescope)); } return((T)value); }
/// <summary> /// Applies the mutation to the specified object instance using the specified precomputed value. /// </summary> /// <param name="uv">The Ultraviolet context.</param> /// <param name="instance">The object instance to mutate.</param> /// <param name="value">The precomputed value to apply with the mutation.</param> /// <param name="context">The current instantiation context.</param> public abstract void Mutate(UltravioletContext uv, Object instance, Object value, UvmlInstantiationContext context);
/// <summary> /// Instantiates the value associated with this mutation. /// </summary> /// <param name="uv">The Ultraviolet context.</param> /// <param name="instance">The object instance to mutate.</param> /// <param name="context">The current instantiation context.</param> /// <returns>The value which was instantiated.</returns> public abstract Object InstantiateValue(UltravioletContext uv, Object instance, UvmlInstantiationContext context);
/// <inheritdoc/> public override void Mutate(UltravioletContext uv, Object instance, Object value, UvmlInstantiationContext context) { var uiElement = instance as UIElement; if (uiElement == null) { return; } var eventHandlerName = ProcessPrecomputedValue <String>(value, context); if (eventHandlerName == null) { throw new UvmlException(PresentationStrings.InvalidEventHandler.Format(revtID.Name, "(null)")); } var eventHandlerDelegate = CreateEventHandlerDelegate(eventHandlerName, revtID.DelegateType, context); uiElement.AddHandler(revtID, eventHandlerDelegate); }
/// <inheritdoc/> public override void Mutate(UltravioletContext uv, Object instance, Object value, UvmlInstantiationContext context) { var dobj = instance as DependencyObject; if (dobj == null) { return; } var processedValue = ProcessPrecomputedValue <Object>(value, context); dpropSetter.Invoke(instance, new Object[] { dpropID, processedValue }); }
/// <summary> /// Creates a new collection instance for the mutated property. /// </summary> /// <param name="uv">The Ultraviolet context.</param> /// <param name="instance">The object instance which is being mutated.</param> /// <param name="context">The current instantiation context.</param> /// <param name="collection">The collection which was created.</param> /// <returns><see langword="true"/> if the collection was able to be created; /// otherwise, <see langword="false"/>.</returns> protected abstract Boolean CreateCollection(UltravioletContext uv, Object instance, UvmlInstantiationContext context, out Object collection);
/// <inheritdoc/> public override void Mutate(UltravioletContext uv, Object instance, Object value, UvmlInstantiationContext context) { var processedValue = ProcessPrecomputedValue <Object>(value, context); propertyInfo.SetValue(instance, processedValue, null); }
/// <inheritdoc/> public override void Mutate(UltravioletContext uv, Object instance, UvmlInstantiationContext context) { var value = InstantiateValue(uv, instance, context); Mutate(uv, instance, value, context); }
/// <inheritdoc/> public override Object Instantiate(UltravioletContext uv, UvmlInstantiationContext context) { return(Template); }
/// <summary> /// Performs initialization required by instances of the <see cref="FrameworkElement"/> class. /// </summary> private void InitializeFrameworkElement(UltravioletContext uv, Object instance, UvmlInstantiationContext context) { var frameworkElement = instance as FrameworkElement; if (frameworkElement == null) { return; } if (!String.IsNullOrEmpty(frameworkElement.Name)) { context.Namescope.RegisterName(frameworkElement.Name, frameworkElement); } frameworkElement.BeginInit(); frameworkElement.TemplatedParent = context.TemplatedParent as DependencyObject; }
/// <inheritdoc/> public override void Mutate(UltravioletContext uv, Object instance, Object value, UvmlInstantiationContext context) { var items = ProcessPrecomputedValue <List <Object> >(value, context); if (items == null) { throw new ArgumentException(nameof(value)); } var propname = default(String); var collection = default(Object); if (!GetCollection(instance, out collection, out propname)) { throw new UvmlException(PresentationStrings.PropertyHasNoGetter.Format(propname)); } if (collection == null) { if (!CreateCollection(uv, instance, context, out collection)) { throw new UvmlException(PresentationStrings.CollectionCannotBeCreated.Format(propname)); } if (!SetCollection(instance, collection)) { throw new UvmlException(PresentationStrings.PropertyHasNoSetter.Format(propname)); } } var listType = collection.GetType(); var listItemType = GetCollectionItemType(listType); if (listItemType == null) { throw new UvmlException(PresentationStrings.CollectionTypeNotSupported.Format(listType)); } var listClearMethod = listType.GetMethod("Clear", BindingFlags.Public | BindingFlags.Instance, null, Type.EmptyTypes, null); if (listClearMethod == null) { throw new UvmlException(PresentationStrings.CollectionCannotBeCleared.Format(listType.Name)); } listClearMethod.Invoke(collection, null); var listAddRangeMethod = listType.GetMethod("AddRange", BindingFlags.Public | BindingFlags.Instance, null, new[] { typeof(IEnumerable <>).MakeGenericType(listItemType) }, null); if (listAddRangeMethod != null) { var itemInstances = Array.CreateInstance(listItemType, items.Count); for (int i = 0; i < items.Count; i++) { var item = ProcessPrecomputedValue <Object>(items[i], context); itemInstances.SetValue(item, i); } listAddRangeMethod.Invoke(collection, new[] { itemInstances }); } else { var listAddMethodParameters = new Object[] { null }; var listAddMethod = listType.GetMethod("Add", BindingFlags.Public | BindingFlags.Instance, null, new[] { listItemType }, null); if (listAddMethod != null) { foreach (var item in items) { listAddMethodParameters[0] = ProcessPrecomputedValue <Object>(item, context); listAddMethod.Invoke(collection, listAddMethodParameters); } } else { throw new UvmlException(PresentationStrings.CollectionHasNoAddMethod.Format(listType.Name)); } } }
/// <inheritdoc/> public override Object InstantiateValue(UltravioletContext uv, Object instance, UvmlInstantiationContext context) { return(items.Select(x => x.Instantiate(uv, context)).ToList()); }
/// <inheritdoc/> protected override Boolean CreateCollection(UltravioletContext uv, Object instance, UvmlInstantiationContext context, out Object collection) { collection = null; return(false); }
/// <inheritdoc/> public override void Mutate(UltravioletContext uv, Object instance, Object value, UvmlInstantiationContext context) { var dobj = instance as DependencyObject; if (dobj == null) { return; } var expression = ProcessPrecomputedValue <String>(value, context); if (expression == null) { throw new UvmlException(PresentationStrings.InvalidBindingExpression.Format("(null)")); } var compiled = context.GetCompiledBindingExpression(dpropID.PropertyType, expression); if (compiled == null) { throw new UvmlException(PresentationStrings.CompiledExpressionNotFound.Format(expression)); } var fmtString = BindingExpressions.GetBindingFormatStringPart(expression); if (fmtString != null) { dobj.BindValue(dpropID, context.DataSourceType, "{{" + compiled.Name + "}}[" + fmtString + "]"); } else { dobj.BindValue(dpropID, context.DataSourceType, "{{" + compiled.Name + "}}"); } }
/// <inheritdoc/> public override Object InstantiateValue(UltravioletContext uv, Object instance, UvmlInstantiationContext context) { return(propertyValue.Instantiate(uv, context)); }
/// <summary> /// Performs initialization required by instances of the <see cref="DependencyObject"/> class. /// </summary> private void InitializeDependencyObject(UltravioletContext uv, Object instance, UvmlInstantiationContext context) { var dobj = instance as DependencyObject; if (dobj == null) { return; } dobj.DeclarativeDataSource = context.DataSource; }
/// <inheritdoc/> protected override Boolean CreateCollection(UltravioletContext uv, Object instance, UvmlInstantiationContext context, out Object collection) { return(CreateCollectionOfType(uv, instance, context, propertyInfo.PropertyType, out collection)); }
/// <inheritdoc/> public override Object InstantiateValue(UltravioletContext uv, Object instance, UvmlInstantiationContext context) { return(eventHandler.Instantiate(uv, context)); }
/// <summary> /// Performs initialization required by instances of the <see cref="ContentPresenter"/> class. /// </summary> private void InitializeContentPresenter(UltravioletContext uv, Object instance, UvmlInstantiationContext context) { var contentPresenter = instance as ContentPresenter; if (contentPresenter == null) { return; } if (contentPresenter.HasDefinedValue(ContentPresenter.ContentProperty) || contentPresenter.TemplatedParent == null) { return; } var alias = contentPresenter.ContentSource ?? "Content"; if (alias == String.Empty) { return; } var templateType = contentPresenter.TemplatedParent.GetType(); var templateWrapperType = uv.GetUI().GetPresentationFoundation().GetDataSourceWrapperType(templateType); var dpAliasedContent = DependencyProperty.FindByName(alias, templateType); if (dpAliasedContent != null) { contentPresenter.BindValue(ContentPresenter.ContentProperty, templateWrapperType, "{{" + dpAliasedContent.Name + "}}"); } if (!contentPresenter.HasDefinedValue(ContentPresenter.ContentStringFormatProperty)) { var dpAliasedContentStringFormat = DependencyProperty.FindByName(alias + "StringFormat", templateType); if (dpAliasedContentStringFormat != null) { contentPresenter.BindValue(ContentPresenter.ContentStringFormatProperty, templateWrapperType, "{{" + dpAliasedContentStringFormat.Name + "}}"); } } }
/// <inheritdoc/> public override void Mutate(UltravioletContext uv, Object instance, Object value, UvmlInstantiationContext context) { var eventHandlerName = ProcessPrecomputedValue <String>(value, context); if (eventHandlerName == null) { throw new UvmlException(PresentationStrings.InvalidEventHandler.Format(eventInfo.Name, "(null)")); } var eventHandlerDelegate = CreateEventHandlerDelegate(eventHandlerName, eventInfo.EventHandlerType, context); eventInfo.AddEventHandler(instance, eventHandlerDelegate); }
/// <summary> /// Instantiates the value represented by the node. /// </summary> /// <param name="uv">The Ultraviolet context.</param> /// <param name="context">The instantiation context for this node.</param> /// <returns>The object which was instantiated by the node.</returns> public abstract Object Instantiate(UltravioletContext uv, UvmlInstantiationContext context);