/// <summary> /// Creates a new XamlObjectServiceProvider instance. /// </summary> public XamlObjectServiceProvider(XamlObject obj) { if (obj == null) throw new ArgumentNullException("obj"); XamlObject = obj; Resolver = new XamlTypeResolverProvider(obj); }
/// <summary> /// registers components from an existing XAML tree /// </summary> internal XamlDesignItem RegisterXamlComponentRecursive(XamlObject obj) { if (obj == null) { return(null); } foreach (XamlProperty prop in obj.Properties) { RegisterXamlComponentRecursive(prop.PropertyValue as XamlObject); foreach (XamlPropertyValue val in prop.CollectionElements) { RegisterXamlComponentRecursive(val as XamlObject); } } XamlDesignItem site = new XamlDesignItem(obj, _context); _sites.Add(site.Component, site); if (ComponentRegistered != null) { ComponentRegistered(this, new DesignItemEventArgs(site)); } if (_context.RootItem != null && !string.IsNullOrEmpty(site.Name)) { var nameScope = _context.RootItem.Component as INameScope; nameScope = NameScope.GetNameScope((DependencyObject)_context.RootItem.Component); var fnd = nameScope.FindName(site.Name); if (fnd != null) { string newNm = site.Name + "_Copy"; fnd = nameScope.FindName(newNm); if (fnd == null) { site.Name = newNm; } else { int i = 1; while (fnd != null) { newNm = site.Name + "_Copy" + i; fnd = nameScope.FindName(newNm); i++; } site.Name = newNm; } } } return(site); }
private ObjectInto ProcessObject(XamlObject obj) { object instance = Activator.CreateInstance(obj.ObjType); SetProperties(instance, obj.Properties); var childObjects = obj.Children.Select(c => ProcessObject(c)).ToArray(); AssignContent(instance, childObjects); return(new ObjectInto(instance, obj)); }
// for use by parser only internal XamlProperty(XamlObject parentObject, XamlPropertyInfo propertyInfo, XamlPropertyValue propertyValue) : this(parentObject, propertyInfo) { PossiblyNameChanged(null, propertyValue); this.propertyValue = propertyValue; if (propertyValue != null) { propertyValue.ParentProperty = this; } UpdateValueOnInstance(); }
/// <summary> /// Find's the Root XamlObject (real Root, or Root Object in Namescope) /// </summary> /// <param name="item"></param> /// <param name="onlyFromSameNamescope"></param> /// <returns></returns> internal static XamlObject GetRootXamlObject(XamlObject item, bool onlyFromSameNamescope = false) { var root = item; while (root.ParentObject != null) { if (onlyFromSameNamescope && NameScopeHelper.GetNameScopeFromObject(root) != NameScopeHelper.GetNameScopeFromObject(root.ParentObject)) { break; } root = root.ParentObject; } return(root); }
/// <summary> /// Get's all Child XamlObject Instances /// </summary> /// <param name="item"></param> /// <param name="onlyFromSameNamescope"></param> /// <returns></returns> private IEnumerable <XamlObject> GetAllChildXamlObjects(XamlObject item, bool onlyFromSameNamescope = false) { foreach (var prop in item.Properties) { if (prop.PropertyValue as XamlObject != null) { if (!onlyFromSameNamescope || NameScopeHelper.GetNameScopeFromObject(item) == NameScopeHelper.GetNameScopeFromObject(prop.PropertyValue as XamlObject)) { yield return(prop.PropertyValue as XamlObject); } foreach (var i in GetAllChildXamlObjects(prop.PropertyValue as XamlObject)) { if (!onlyFromSameNamescope || NameScopeHelper.GetNameScopeFromObject(item) == NameScopeHelper.GetNameScopeFromObject(i)) { yield return(i); } } } if (prop.IsCollection) { foreach (var collectionElement in prop.CollectionElements) { if (collectionElement as XamlObject != null) { if (!onlyFromSameNamescope || NameScopeHelper.GetNameScopeFromObject(item) == NameScopeHelper.GetNameScopeFromObject(collectionElement as XamlObject)) { yield return(collectionElement as XamlObject); } foreach (var i in GetAllChildXamlObjects(collectionElement as XamlObject)) { if (!onlyFromSameNamescope || NameScopeHelper.GetNameScopeFromObject(item) == NameScopeHelper.GetNameScopeFromObject(i)) { yield return(i); } } } } } } }
internal XamlProperty(XamlObject parentObject, XamlPropertyInfo propertyInfo) { this.parentObject = parentObject; this.propertyInfo = propertyInfo; if (propertyInfo.IsCollection) { isCollection = true; collectionElements = new CollectionElementsCollection(this); collectionElements.CollectionChanged += OnCollectionChanged; if (propertyInfo.Name.Equals(XamlConstants.ResourcesPropertyName, StringComparison.Ordinal) && propertyInfo.ReturnType == typeof(ResourceDictionary)) { isResources = true; } } }
public static void Compose(object objToCompose) { Type objType = objToCompose.GetType(); string fullTypeName = objType.FullName; using Stream resource = objType.Assembly.GetManifestResourceStream(string.Format("{0}.xaml", fullTypeName)); using var resourceReader = new System.IO.StreamReader(resource); using var xamlReader = new DotX.Xaml.XamlReader(resourceReader); XamlObject thisObj = xamlReader.Parse(); var composer = new ObjectComposer(objToCompose, thisObj); composer.Compose(); }
internal override void OnParentPropertyChanged() { parentObject = (ParentProperty != null) ? ParentProperty.ParentObject : null; base.OnParentPropertyChanged(); }
private record ObjectInto(object Target, XamlObject Info) { }
bool IsFirstChildResources(XamlObject obj) { return obj.XmlElement.FirstChild != null && obj.XmlElement.FirstChild.Name.EndsWith("." + XamlConstants.ResourcesPropertyName) && obj.Properties.Where((prop) => prop.IsResources).FirstOrDefault() != null; }
/// <summary> /// registers components from an existing XAML tree /// </summary> internal XamlDesignItem RegisterXamlComponentRecursive(XamlObject obj) { if (obj == null) { return(null); } foreach (XamlProperty prop in obj.Properties) { RegisterXamlComponentRecursive(prop.PropertyValue as XamlObject); foreach (XamlPropertyValue val in prop.CollectionElements) { RegisterXamlComponentRecursive(val as XamlObject); } } XamlDesignItem site = new XamlDesignItem(obj, _context); _context.Services.ExtensionManager.ApplyDesignItemInitializers(site); _sites.Add(site.Component, site); if (ComponentRegistered != null) { ComponentRegistered(this, new DesignItemEventArgs(site)); } if (_context.RootItem != null && !string.IsNullOrEmpty(site.Name)) { var nameScope = NameScopeHelper.GetNameScopeFromObject(((XamlDesignItem)_context.RootItem).XamlObject); if (nameScope != null) { // The object will be a part of the RootItem namescope, remove local namescope if set NameScopeHelper.ClearNameScopeProperty(obj.Instance); string newName = site.Name; if (nameScope.FindName(newName) != null) { int copyIndex = newName.LastIndexOf("_Copy", StringComparison.Ordinal); if (copyIndex < 0) { newName += "_Copy"; } else if (!newName.EndsWith("_Copy", StringComparison.Ordinal)) { string copyEnd = newName.Substring(copyIndex + "_Copy".Length); int copyEndValue; if (Int32.TryParse(copyEnd, out copyEndValue)) { newName = newName.Remove(copyIndex + "_Copy".Length); } else { newName += "_Copy"; } } int i = 1; string newNameTemplate = newName; while (nameScope.FindName(newName) != null) { newName = newNameTemplate + i++; } site.Name = newName; } nameScope.RegisterName(newName, obj.Instance); } } return(site); }
public StyledPropertyValue(XamlObject obj) : this(ValueType.SingleObject) { _object = obj; }
public StaticResourceWrapper(XamlObject xamlObject) : base(xamlObject) { }
public BindingWrapper(XamlObject xamlObject) : base(xamlObject) { }
bool UpdateXmlAttribute(bool force, out XamlObject holder) { holder = FindXmlAttributeHolder(); if (holder == null && force && IsMarkupExtension) { holder = this; } if (holder != null && MarkupExtensionPrinter.CanPrint(holder)) { var s = MarkupExtensionPrinter.Print(holder); holder.XmlAttribute = holder.ParentProperty.SetAttribute(s); return true; } return false; }
void UpdateChildMarkupExtensions(XamlObject obj) { foreach (XamlObject propXamlObject in obj.Properties.Where((prop) => prop.IsSet).Select((prop) => prop.PropertyValue).OfType<XamlObject>()) { UpdateChildMarkupExtensions(propXamlObject); } if (obj.IsMarkupExtension && obj.ParentProperty != null) { obj.ParentProperty.UpdateValueOnInstance(); } }
/// <summary> /// Gets a <see cref="FrameworkTemplate"/> based on the specified parameters. /// </summary> /// <param name="xmlElement">The xml element to get template xaml from.</param> /// <param name="parentObject">The <see cref="XamlObject"/> to use as source for resources and contextual information.</param> /// <returns>A <see cref="FrameworkTemplate"/> based on the specified parameters.</returns> public static FrameworkTemplate GetFrameworkTemplate(XmlElement xmlElement, XamlObject parentObject) { var nav = xmlElement.CreateNavigator(); var ns = new Dictionary<string, string>(); while (true) { var nsInScope = nav.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml); foreach (var ak in nsInScope) { if (!ns.ContainsKey(ak.Key) && ak.Key != "") ns.Add(ak.Key, ak.Value); } if (!nav.MoveToParent()) break; } xmlElement = (XmlElement)xmlElement.CloneNode(true); foreach (var dictentry in ns.ToList()) { var value = dictentry.Value; if (value.StartsWith("clr-namespace") && !value.Contains(";assembly=")) { if (!string.IsNullOrEmpty(parentObject.OwnerDocument.CurrentProjectAssemblyName)) { value += ";assembly=" + parentObject.OwnerDocument.CurrentProjectAssemblyName; } } xmlElement.SetAttribute("xmlns:" + dictentry.Key, value); } var keyAttrib = xmlElement.GetAttribute("Key", XamlConstants.XamlNamespace); if (string.IsNullOrEmpty(keyAttrib)) { xmlElement.SetAttribute("Key", XamlConstants.XamlNamespace, "$$temp&&§§%%__"); } var xaml = xmlElement.OuterXml; xaml = "<ResourceDictionary xmlns=\"http://schemas.microsoft.com/netfx/2007/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">" + xaml + "</ResourceDictionary>"; StringReader stringReader = new StringReader(xaml); XmlReader xmlReader = XmlReader.Create(stringReader); var xamlReader = new XamlXmlReader(xmlReader, parentObject.ServiceProvider.SchemaContext); var seti = new XamlObjectWriterSettings(); var resourceDictionary = new ResourceDictionary(); var obj = parentObject; while (obj != null) { if (obj.Instance is ResourceDictionary) { var r = obj.Instance as ResourceDictionary; foreach (var k in r.Keys) { if (!resourceDictionary.Contains(k)) resourceDictionary.Add(k, r[k]); } } else if (obj.Instance is FrameworkElement) { var r = ((FrameworkElement)obj.Instance).Resources; foreach (var k in r.Keys) { if (!resourceDictionary.Contains(k)) resourceDictionary.Add(k, r[k]); } } obj = obj.ParentObject; } seti.BeforePropertiesHandler = (s, e) => { if (seti.BeforePropertiesHandler != null) { var rr = e.Instance as ResourceDictionary; rr.MergedDictionaries.Add(resourceDictionary); seti.BeforePropertiesHandler = null; } }; var writer = new XamlObjectWriter(parentObject.ServiceProvider.SchemaContext, seti); XamlServices.Transform(xamlReader, writer); var result = (ResourceDictionary)writer.Result; var enr = result.Keys.GetEnumerator(); enr.MoveNext(); var rdKey = enr.Current; var template = result[rdKey] as FrameworkTemplate; result.Remove(rdKey); return template; }
protected bool Equals(VectorXaml other) { return(string.Equals(PathGeometryXaml, other.PathGeometryXaml) && string.Equals(StreamGeometryXaml, other.StreamGeometryXaml) && string.Equals(PathData, other.PathData) && string.Equals(XamlObject.ToString(), other.XamlObject.ToString())); }
public XamlDesignItem(XamlObject xamlObject, XamlDesignContext designContext) { this._xamlObject = xamlObject; this._designContext = designContext; this._properties = new XamlModelPropertyCollection(this); }
void UpdateChildMarkupExtensions(XamlObject obj) { foreach (var prop in obj.Properties) { if (prop.IsSet) { var propXamlObject = prop.PropertyValue as XamlObject; if (propXamlObject != null) { UpdateChildMarkupExtensions(propXamlObject); } } else if (prop.IsCollection) { foreach (var propXamlObject in prop.CollectionElements.OfType<XamlObject>()) { UpdateChildMarkupExtensions(propXamlObject); } } } if (obj.IsMarkupExtension && obj.ParentProperty != null) { obj.ParentProperty.UpdateValueOnInstance(); } }