Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PropertyItem"/> class.
        /// </summary>
        public PropertyItem(PropertyDescriptor property, object instance)
        {
            Property = property;
            Instance = instance;

            ResetCommand        = new Command <object>(ResetValue, CanResetValue);
            EditBindingCommand  = new Command <object>(ShowBindingEditor, o => !property.IsReadOnly);
            EditResourceCommand = new Command <object>(ShowResourceEditor, o => !property.IsReadOnly);

            _propertySortInfo = PropertySorter.GetSortInfo(property);
            _markupObject     = MarkupWriter.GetMarkupObjectFor(Instance);

            property.AddValueChanged(instance, OnValueChanged);

            _dpd = DependencyPropertyDescriptor.FromProperty(property);
        }
Example #2
0
 //https://stackoverflow.com/questions/4794071/how-to-enumerate-all-dependency-properties-of-control
 public static IEnumerable <DependencyProperty> EnumerateAttachedProperties(object element)
 {
     if (element != null)
     {
         MarkupObject markupObject = MarkupWriter.GetMarkupObjectFor(element);
         if (markupObject != null)
         {
             foreach (MarkupProperty mp in markupObject.Properties)
             {
                 if (mp.IsAttached)
                 {
                     yield return(mp.DependencyProperty);
                 }
             }
         }
     }
 }
Example #3
0
        private void ResolveXmlNamespaces(object obj)
        {
            MarkupObject markupObj = MarkupWriter.GetMarkupObjectFor(obj);

            string ns     = _namespaceCache.GetXmlNamespace(markupObj.ObjectType);
            string prefix = _namespaceCache.GetPrefixForNamespace(ns);

            _namespaceMaps[ns] = new NamespaceMap(prefix, ns);

            foreach (MarkupProperty markupProperty in markupObj.Properties)
            {
                if (IsContentProperty(markupObj, markupProperty))
                {
                    if (!(markupProperty.Value is String))
                    {
                        ResolveChildXmlNamespaces(markupProperty);
                    }
                    continue;
                }

                if (markupProperty.Value.GetType() == typeof(NullExtension) || markupProperty.IsValueAsString)
                {
                    continue;
                }

                if (!markupProperty.IsComposite)
                {
                    if (markupProperty.DependencyProperty != null)
                    {
                        string ns1     = _namespaceCache.GetXmlNamespace(markupProperty.DependencyProperty.OwnerType);
                        string prefix1 = _namespaceCache.GetPrefixForNamespace(ns1);
                        if (!string.IsNullOrEmpty(prefix1))
                        {
                            _namespaceMaps[ns1] = new NamespaceMap(prefix1, ns1);
                        }
                    }
                }
                else
                {
                    string ns2     = _namespaceCache.GetXmlNamespace(markupObj.ObjectType);
                    string prefix2 = _namespaceCache.GetPrefixForNamespace(ns2);
                    _namespaceMaps[ns2] = new NamespaceMap(prefix2, ns2);
                    ResolveChildXmlNamespaces(markupProperty);
                }
            }
        }
Example #4
0
        /// <summary>
        ///     Save writes the xml respresentation
        ///     for the given object instance to the given stream
        /// </summary>
        /// <param name="obj">
        ///     Object instance
        /// </param>
        /// <param name="stream">
        ///     Stream
        /// </param>
        /// <remarks>
        ///     This API requires unmanaged code permission
        /// </remarks>
        public static void Save(object obj, Stream stream)
        {
            // Validate input arguments
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            // Create XmlTextWriter
            XmlTextWriter xmlWriter = new XmlTextWriter(stream, null);

            MarkupWriter.SaveAsXml(xmlWriter, obj);
        }
Example #5
0
        /// <summary>
        ///     Save writes the xml respresentation
        ///     for the given object instance using the given writer
        /// </summary>
        /// <param name="obj">
        ///     Object instance
        /// </param>
        /// <param name="writer">
        ///     Text Writer
        /// </param>
        /// <remarks>
        ///     This API requires unmanaged code permission
        /// </remarks>
        public static void Save(object obj, TextWriter writer)
        {
            // Validate input arguments
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            // Create XmlTextWriter
            XmlTextWriter xmlWriter = new XmlTextWriter(writer);

            MarkupWriter.SaveAsXml(xmlWriter, obj);
        }
Example #6
0
        public void Dump()
        {
            values.Clear();

            var markupObject = MarkupWriter.GetMarkupObjectFor(this.Object);

            if (markupObject != null)
            {
                foreach (MarkupProperty mp in markupObject.Properties)
                {
                    if (mp.DependencyProperty != null)
                    {
                        values[mp.DependencyProperty] = this.Object.GetValue(mp.DependencyProperty);
                    }
                }
            }
        }
Example #7
0
        /// <summary>
        ///     Save writes the xml respresentation
        ///     for the given object instance using the
        ///     given XmlTextWriter embedded in the manager.
        /// </summary>
        /// <param name="obj">
        ///     Object instance
        /// </param>
        /// <param name="manager">
        ///     Serialization Manager
        /// </param>
        /// <remarks>
        ///     This API requires unmanaged code permission
        /// </remarks>
        public static void Save(object obj, XamlDesignerSerializationManager manager)
        {
            // Must be in full trust
            SecurityHelper.DemandUnmanagedCode();

            // Validate input arguments
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }
            if (manager == null)
            {
                throw new ArgumentNullException("manager");
            }

            MarkupWriter.SaveAsXml(manager.XmlWriter, obj, manager);
        }
Example #8
0
        /// <summary>
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public static List <DependencyProperty> GetDependencyProperties(Object element)
        {
            List <DependencyProperty> properties = new List <DependencyProperty>();
            MarkupObject markupObject            = MarkupWriter.GetMarkupObjectFor(element);

            if (markupObject is not null)
            {
                foreach (MarkupProperty mp in markupObject.Properties)
                {
                    if (mp.DependencyProperty is not null)
                    {
                        properties.Add(mp.DependencyProperty);
                    }
                }
            }
            return(properties);
        }
Example #9
0
        public static List <DependencyProperty> GetAttachedProperties(Object element)
        {
            List <DependencyProperty> attachedProperties = new List <DependencyProperty>();
            MarkupObject markupObject = MarkupWriter.GetMarkupObjectFor(element);

            if (markupObject != null)
            {
                foreach (MarkupProperty mp in markupObject.Properties)
                {
                    if (mp.IsAttached)
                    {
                        attachedProperties.Add(mp.DependencyProperty);
                    }
                }
            }
            return(attachedProperties);
        }
        private void Init(PropertyDescriptor propertyDescriptor, object selectedObject)
        {
            if (propertyDescriptor == null)
            {
                throw new ArgumentNullException("propertyDescriptor");
            }

            if (selectedObject == null)
            {
                throw new ArgumentNullException("selectedObject");
            }

            _propertyDescriptor = propertyDescriptor;
            _selectedObject     = selectedObject;
            _dpDescriptor       = DependencyPropertyDescriptor.FromProperty(propertyDescriptor);
            _markupObject       = MarkupWriter.GetMarkupObjectFor(SelectedObject);
        }
Example #11
0
        public static List <DependencyProperty> GetDependencyProperties(Object element)
        {
            List <DependencyProperty> properties = new List <DependencyProperty>();
            MarkupObject markupObject            = MarkupWriter.GetMarkupObjectFor(element);

            if (markupObject != null)
            {
                foreach (MarkupProperty mp in markupObject.Properties)
                {
                    Console.WriteLine("{1} : MarkupProperty = {0}", mp.Name, element);
                    if (mp.DependencyProperty != null)
                    {
                        properties.Add(mp.DependencyProperty);
                    }
                }
            }
            return(properties);
        }
        internal static IReadOnlyList <DependencyProperty> GetAttachedProperties(object element)
        {
            var attachedProperties = new List <DependencyProperty>();
            var markupObject       = MarkupWriter.GetMarkupObjectFor(element);

            if (markupObject != null)
            {
                foreach (MarkupProperty mp in markupObject.Properties)
                {
                    if (mp.IsAttached)
                    {
                        attachedProperties.Add(mp.DependencyProperty);
                    }
                }
            }

            return(attachedProperties);
        }
        internal DescriptorPropertyDefinition(PropertyDescriptor propertyDescriptor, object selectedObject, bool isPropertyGridCategorized)
            : base(isPropertyGridCategorized)
        {
            if (propertyDescriptor == null)
            {
                throw new ArgumentNullException("propertyDescriptor");
            }

            if (selectedObject == null)
            {
                throw new ArgumentNullException("selectedObject");
            }

            _propertyDescriptor = propertyDescriptor;
            _selectedObject     = selectedObject;
            _dpDescriptor       = DependencyPropertyDescriptor.FromProperty(propertyDescriptor);
            _markupObject       = MarkupWriter.GetMarkupObjectFor(SelectedObject);
        }
Example #14
0
        /// <summary>
        ///     Save writes the xml respresentation
        ///     for the given object instance using the given writer
        /// </summary>
        /// <param name="obj">
        ///     Object instance
        /// </param>
        /// <param name="writer">
        ///     Text Writer
        /// </param>
        /// <remarks>
        ///     This API requires unmanaged code permission
        /// </remarks>
        public static void Save(object obj, TextWriter writer)
        {
            // Must be in full trust
            SecurityHelper.DemandUnmanagedCode();

            // Validate input arguments
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            // Create XmlTextWriter
            XmlTextWriter xmlWriter = new XmlTextWriter(writer);

            MarkupWriter.SaveAsXml(xmlWriter, obj);
        }
Example #15
0
 public static void Save(object obj, XmlWriter xmlWriter)
 {
     SecurityHelper.DemandUnmanagedCode();
     if (obj == null)
     {
         throw new ArgumentNullException("obj");
     }
     if (xmlWriter == null)
     {
         throw new ArgumentNullException("xmlWriter");
     }
     try
     {
         MarkupWriter.SaveAsXml(xmlWriter, obj);
     }
     finally
     {
         xmlWriter.Flush();
     }
 }
Example #16
0
        /// <summary>
        ///     Save writes the xml respresentation
        ///     for the given object instance using the given
        ///     writer. In addition it also allows the designer
        ///     to participate in this conversion.
        /// </summary>
        /// <param name="obj">
        ///     Object instance
        /// </param>
        /// <param name="xmlWriter">
        ///     XmlWriter
        /// </param>
        /// <remarks>
        ///     This API requires unmanaged code permission
        /// </remarks>
        public static void Save(object obj, XmlWriter xmlWriter)
        {
            // Validate input arguments
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }
            if (xmlWriter == null)
            {
                throw new ArgumentNullException("xmlWriter");
            }

            try
            {
                MarkupWriter.SaveAsXml(xmlWriter, obj);
            }
            finally
            {
                xmlWriter.Flush();
            }
        }
        public static List <IPropertyGridItem> GetPropertyItems(object instance)
        {
            var propertyItems = new List <IPropertyGridItem>();

            if (instance == null)
            {
                return(propertyItems);
            }

            var properties = TypeDescriptor.GetProperties(instance.GetType(),
                                                          new Attribute[] { new PropertyFilterAttribute(PropertyFilterOptions.All) });

            // Get all properties of the type
            propertyItems.AddRange(
                properties.Cast <PropertyDescriptor>().Where(p => p.IsBrowsable && p.Name != "GenericParameterAttributes").Select(property => CreatePropertyItem(property, instance)));

            if (instance is FrameworkElement)
            {
                propertyItems.Add(CreatePropertyItem(DependencyPropertyDescriptor.FromProperty(AutomationProperties.NameProperty, instance.GetType()), instance));
                propertyItems.Add(CreatePropertyItem(DependencyPropertyDescriptor.FromProperty(AutomationProperties.HelpTextProperty, instance.GetType()), instance));
            }

            // Get all set attached dependency properties
            var markupObject = MarkupWriter.GetMarkupObjectFor(instance);

            foreach (MarkupProperty mp in markupObject.Properties)
            {
                if (mp.IsAttached && mp.DependencyProperty != null)
                {
                    var dpd = DependencyPropertyDescriptor.FromProperty(mp.DependencyProperty, instance.GetType());
                    if (dpd != null)
                    {
                        propertyItems.Add(CreatePropertyItem(dpd, instance));
                    }
                }
            }

            return(propertyItems);
        }
Example #18
0
        private void WriteObject(object key, object obj, XmlTextWriter writer, bool isRoot)
        {
            MarkupObject markupObj  = MarkupWriter.GetMarkupObjectFor(obj);
            Type         objectType = markupObj.ObjectType;

            _namespaceCache.GetXmlNamespace(objectType);
            string ns     = _namespaceCache.GetXmlNamespace(objectType);
            string prefix = _namespaceCache.GetPrefixForNamespace(ns);

            WriteStartElement(writer, prefix, markupObj.ObjectType.Name);

            if (isRoot)
            {
                foreach (NamespaceMap map in _namespaceMaps.Values)
                {
                    if (string.IsNullOrEmpty(map.Prefix))
                    {
                        writer.WriteAttributeString("xmlns", map.XmlNamespace);
                    }
                    else
                    {
                        writer.WriteAttributeString("xmlns:" + map.Prefix, map.XmlNamespace);
                    }
                }

                if (!_namespaceMaps.ContainsKey(NamespaceCache.XamlNamespace))
                {
                    writer.WriteAttributeString("xmlns:x", NamespaceCache.XamlNamespace);
                }
            }

            if (key != null)
            {
                string keyString = key.ToString();
                if (keyString.Length > 0)
                {
                    writer.WriteAttributeString("x:Key", keyString);
                }
                else
                {
                    //TODO: key may not be a string, what about x:Type...
                    throw new NotImplementedException();
                }
            }

            List <MarkupProperty> propertyElements = new List <MarkupProperty>();
            MarkupProperty        contentProperty  = null;
            string contentString = string.Empty;

            foreach (MarkupProperty markupProperty in markupObj.Properties)
            {
                if (IsContentProperty(markupObj, markupProperty))
                {
                    contentProperty = markupProperty;
                    continue;
                }

                if (markupProperty.IsValueAsString)
                {
                    contentString = markupProperty.Value as string;
                }
                else if (!markupProperty.IsComposite)
                {
                    string temp = markupProperty.Value == null ? string.Empty : _formatterConverter.ToString(markupProperty.Value);

                    if (markupProperty.IsAttached)
                    {
                        string ns1     = _namespaceCache.GetXmlNamespace(markupProperty.DependencyProperty.OwnerType);
                        string prefix1 = _namespaceCache.GetPrefixForNamespace(ns1);
                        if (string.IsNullOrEmpty(prefix1))
                        {
                            writer.WriteAttributeString(markupProperty.Name, temp);
                        }
                        else
                        {
                            writer.WriteAttributeString(prefix1 + ":" + markupProperty.Name, temp);
                        }
                    }
                    else
                    {
                        if (markupProperty.Name == "Name" && NamespaceCache.GetAssemblyNameFromType(markupProperty.DependencyProperty.OwnerType).Equals("PresentationFramework"))
                        {
                            writer.WriteAttributeString("x:" + markupProperty.Name, temp);
                        }
                        else
                        {
                            writer.WriteAttributeString(markupProperty.Name, temp);
                        }
                    }
                }
                else if (markupProperty.Value.GetType() == typeof(NullExtension))
                {
                    writer.WriteAttributeString(markupProperty.Name, "{x:Null}");
                }
                else
                {
                    propertyElements.Add(markupProperty);
                }
            }

            if (contentProperty != null || propertyElements.Count > 0 || !string.IsNullOrEmpty(contentString))
            {
                foreach (MarkupProperty markupProp in propertyElements)
                {
                    string ns2             = _namespaceCache.GetXmlNamespace(markupObj.ObjectType);
                    string prefix2         = _namespaceCache.GetPrefixForNamespace(ns2);
                    string propElementName = markupObj.ObjectType.Name + "." + markupProp.Name;

                    WriteStartElement(writer, prefix2, propElementName);
                    WriteChildren(writer, markupProp);
                    writer.WriteEndElement();
                }

                if (!string.IsNullOrEmpty(contentString))
                {
                    writer.WriteValue(contentString);
                }
                else if (contentProperty != null)
                {
                    if (contentProperty.Value is string)
                    {
                        writer.WriteValue(contentProperty.StringValue);
                    }
                    else
                    {
                        WriteChildren(writer, contentProperty);
                    }
                }
            }

            writer.WriteEndElement();
        }
Example #19
0
 public RequestConsumer()
 {
     _markupWriter = new MarkupWriter();
     Receive<Request>(r => HandleRequest(r));
 }
Example #20
0
        public void WriteObject(object key, object obj, XmlWriter writer, bool isRoot)
        {
            var doc = xmldoc.DocumentElement;
            List <MarkupProperty> propertyElements = new List <MarkupProperty>();
            MarkupProperty        contentProperty  = null;
            string       contentPropertyName       = null;
            MarkupObject markupObj  = MarkupWriter.GetMarkupObjectFor(obj);
            Type         objectType = markupObj.ObjectType;

            string ns     = _namespaceCache.GetNamespaceUriFor(objectType);
            string prefix = _namespaceCache.GetDefaultPrefixFor(ns);

            if (isRoot)
            {
                if (String.IsNullOrEmpty(prefix))
                {
                    if (String.IsNullOrEmpty(ns))
                    {
                        writer.WriteStartElement(markupObj.ObjectType.Name, NamespaceCache.DefaultNamespace);
                        writer.WriteAttributeString("xmlns",
                                                    NamespaceCache.XmlnsNamespace, NamespaceCache.DefaultNamespace);
                    }
                    else
                    {
                        writer.WriteStartElement(markupObj.ObjectType.Name, ns);
                        writer.WriteAttributeString("xmlns", NamespaceCache.XmlnsNamespace, ns);
                    }
                }
                else
                {
                    if (doc.LocalName == markupObj.ObjectType.Name)
                    {
                        if (doc.NamespaceURI == ns)
                        {
                            if (doc.Prefix == prefix)
                            {
                            }
                        }
                    }
                }
                //writer.WriteAttributeString("xmlns", "x",
                //    NamespaceCache.XmlnsNamespace, NamespaceCache.XamlNamespace);

                //foreach (NamespaceMap map in _dicNamespaceMap.Values) {
                //    if (!String.IsNullOrEmpty(map.Prefix) && !String.Equals(map.Prefix, "x"))
                //        writer.WriteAttributeString("xmlns", map.Prefix, NamespaceCache.XmlnsNamespace, map.XmlNamespace);
                //}
            }
            else
            {
                //TODO: Fix - the best way to handle this case...
                if (markupObj.ObjectType.Name == "PathFigureCollection" && markupObj.Instance != null)
                {
                    WriteState writeState = writer.WriteState;

                    if (writeState == WriteState.Element)
                    {
                        writer.WriteAttributeString("Figures", markupObj.Instance.ToString());
                    }
                    else
                    {
                        if (String.IsNullOrEmpty(prefix))
                        {
                            writer.WriteStartElement("PathGeometry.Figures");
                        }
                        else
                        {
                            writer.WriteStartElement("PathGeometry.Figures", ns);
                        }
                        writer.WriteString(markupObj.Instance.ToString());
                        writer.WriteEndElement();
                    }
                    return;
                }
                else
                {
                    if (String.IsNullOrEmpty(prefix))
                    {
                        writer.WriteStartElement(markupObj.ObjectType.Name);
                    }
                    else
                    {
                        writer.WriteStartElement(markupObj.ObjectType.Name, ns);
                    }
                }
            }

            // Add the x:Name for object like Geometry/Drawing not derived from FrameworkElement...
            DependencyObject dep = obj as DependencyObject;

            if (dep != null)
            {
                string nameValue = dep.GetValue(FrameworkElement.NameProperty) as string;
                if (!String.IsNullOrEmpty(nameValue) && !(dep is FrameworkElement))
                {
                    writer.WriteAttributeString("x", "Name", NamespaceCache.XamlNamespace, nameValue);
                }
            }

            if (key != null)
            {
                string keyString = key.ToString();
                if (keyString.Length > 0)
                {
                    writer.WriteAttributeString("x", "Key", NamespaceCache.XamlNamespace, keyString);
                }
                else
                {
                    //TODO: key may not be a string, what about x:Type...
                    throw new NotImplementedException(
                              "Sample XamlWriter cannot yet handle keys that aren't strings");
                }
            }

            //Look for CPA info in our cache that keeps contentProperty names per Type
            //If it doesn't have an entry, go get the info and store it.
            if (!_contentProperties.ContainsKey(objectType))
            {
                string lookedUpContentProperty = String.Empty;
                foreach (Attribute attr in markupObj.Attributes)
                {
                    ContentPropertyAttribute cpa = attr as ContentPropertyAttribute;
                    if (cpa != null)
                    {
                        lookedUpContentProperty = cpa.Name;
                        //Once content property is found, come out of the loop.
                        break;
                    }
                }

                _contentProperties.Add(objectType, lookedUpContentProperty);
            }

            contentPropertyName = _contentProperties[objectType];
            string contentString = String.Empty;

            foreach (MarkupProperty markupProperty in markupObj.Properties)
            {
                if (markupProperty.Name != contentPropertyName)
                {
                    if (markupProperty.IsValueAsString)
                    {
                        contentString = markupProperty.Value as string;
                    }
                    else if (!markupProperty.IsComposite)
                    {
                        string temp = markupProperty.StringValue;

                        if (markupProperty.IsAttached)
                        {
                            string ns1     = _namespaceCache.GetNamespaceUriFor(markupProperty.DependencyProperty.OwnerType);
                            string prefix1 = _namespaceCache.GetDefaultPrefixFor(ns1);

                            if (String.IsNullOrEmpty(prefix1))
                            {
                                writer.WriteAttributeString(markupProperty.Name, temp);
                            }
                            else
                            {
                                writer.WriteAttributeString(markupProperty.Name, ns1, temp);
                            }
                        }
                        else
                        {
                            if (markupProperty.Name == "FontUri" &&
                                (_wpfSettings != null))
                            {
                                string fontUri = temp.ToLower();
                                fontUri = fontUri.Replace(_windowsDir, _windowsPath);

                                StringBuilder builder = new StringBuilder();
                                builder.Append("{");
                                builder.Append("svg");
                                builder.Append(":");
                                builder.Append("SvgFontUri ");
                                builder.Append(fontUri);
                                builder.Append("}");

                                writer.WriteAttributeString(markupProperty.Name, builder.ToString());
                            }
                            else
                            {
                                if (doc.HasAttribute(markupProperty.Name))
                                {
                                    PropertyInfo pI = obj.GetType().GetProperty(markupProperty.Name);
                                    SetValue(pI, doc, obj);
                                }
                            }
                        }
                    }
                    else if (markupProperty.Value.GetType() == _nullType)
                    {
                        if (_nullExtension)
                        {
                            //writer.WriteAttributeString(markupProperty.Name, "{x:Null}");
                        }
                    }
                    else
                    {
                        propertyElements.Add(markupProperty);
                    }
                }
                else
                {
                    contentProperty = markupProperty;
                }
            }

            if (contentProperty != null || propertyElements.Count > 0 || contentString != String.Empty)
            {
                foreach (MarkupProperty markupProp in propertyElements)
                {
                    string propElementName = markupObj.ObjectType.Name + "." + markupProp.Name;

                    if (doc.HasChildNodes)
                    {
                        foreach (XmlElement child in doc.ChildNodes)
                        {
                            if (child.LocalName == propElementName)
                            {
                                if (child.HasChildNodes)
                                {
                                    foreach (XmlElement _child in child.ChildNodes)
                                    {
                                        string _namespace = _child.NamespaceURI;
                                        string _prefix    = _child.Prefix;
                                        string _nameType  = _namespace + "." + _child.LocalName;
                                        if (IsKnowType(_nameType))
                                        {
                                            Type   _type = Type.GetType(_nameType);
                                            object _obj  = _type.InvokeMember(_type.FullName, BindingFlags.CreateInstance, null, null, null);
                                            Create(_type, _child, _obj);
                                            IList       collection = markupProp.Value as IList;
                                            IDictionary dictionary = markupProp.Value as IDictionary;
                                            if (collection != null)
                                            {
                                                collection.Add(_obj);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                if (contentString != String.Empty)
                {
                    //writer.WriteValue(contentString);
                }
                else if (contentProperty != null)
                {
                    if (contentProperty.Value is string)
                    {
                        //writer.WriteValue(contentProperty.StringValue);
                    }
                    else
                    {
                        //WriteChildren(writer, contentProperty);
                    }
                }
            }
            //writer.WriteEndElement();
        }
Example #21
0
        private void XmlWrite(XmlWriter xmlw, object obj, bool isRoot)
        {
            MarkupObject markupObj  = MarkupWriter.GetMarkupObjectFor(obj);
            Type         objectType = markupObj.ObjectType;

            if (isRoot)
            {
                string nspace = objectType.Namespace;
                xmlw.WriteStartElement(objectType.Name, nspace);
            }

            List <MarkupProperty> propertyElements = new List <MarkupProperty>();

            foreach (System.Windows.Markup.Primitives.MarkupProperty markupProp in markupObj.Properties)
            {
                try {
                    if (!markupProp.IsComposite)
                    {
                        string temp = markupProp.StringValue;
                        if (!string.IsNullOrEmpty(temp))
                        {
                            xmlw.WriteAttributeString(markupProp.Name, temp);
                        }
                    }
                    else if (markupProp.Value.GetType() == typeof(System.Windows.Markup.NullExtension))
                    {
                        if (_nullExtension)
                        {
                            xmlw.WriteAttributeString(markupProp.Name, "{x:Null}");
                        }
                    }
                    else
                    {
                        propertyElements.Add(markupProp);
                        //XmlWrite(xmlw, markupProp.Value, false);
                    }
                } catch { }
            }

            if (propertyElements.Count > 0)
            {
                foreach (MarkupProperty markupProp in propertyElements)
                {
                    string propElementName = markupObj.ObjectType.Name + "." + markupProp.Name;
                    if (isRoot)
                    {
                        xmlw.WriteStartElement(propElementName);
                    }

                    System.Collections.IList       collection = markupProp.Value as System.Collections.IList;
                    System.Collections.IDictionary dictionary = markupProp.Value as System.Collections.IDictionary;
                    if (collection != null && collection.Count > 0)
                    {
                        foreach (object iobj in collection)
                        {
                            XmlWrite(xmlw, iobj, true);
                            xmlw.WriteEndElement();
                        }
                    }
                    else if (dictionary != null && dictionary.Count > 0)
                    {
                        foreach (object iobj in dictionary)
                        {
                            XmlWrite(xmlw, iobj, true);
                            xmlw.WriteEndElement();
                        }
                    }
                    else
                    {
                        if (markupProp.Value.GetType() == typeof(Equip) || markupProp.Value.GetType() == typeof(Solution))
                        {
                            XmlWrite(xmlw, markupProp.Value, true);
                        }
                        xmlw.WriteEndElement();
                    }
                }
            }
        }
Example #22
0
        // ==========================================================================

        /// <summary>
        /// Initializes a new instance of the <see cref="XamlSerializer"/> class.
        /// </summary>
        /// <param name="instance">The instance.</param>
        public XamlSerializer(object instance)
        {
            MarkupObject = MarkupWriter.GetMarkupObjectFor(instance);
        }
Example #23
0
 /// <summary>
 /// List all attached properties of a given dependency object
 /// </summary>
 public static IEnumerable <DependencyProperty> GetAttachedProperties(this DependencyObject element)
 {
     return(MarkupWriter.GetMarkupObjectFor(element)
            .Properties.Where(p => p.IsAttached)
            .Select(p => p.DependencyProperty));
 }
        private void ResolveXmlNamespaces(object obj)
        {
            List <MarkupProperty> propertyElements = new List <MarkupProperty>();
            MarkupProperty        contentProperty  = null;
            string       contentPropertyName       = null;
            MarkupObject markupObj  = MarkupWriter.GetMarkupObjectFor(obj);
            Type         objectType = markupObj.ObjectType;

            string ns = _namespaceCache.GetNamespaceUriFor(objectType);

            if (!string.IsNullOrWhiteSpace(ns))
            {
                string prefix = _namespaceCache.GetDefaultPrefixFor(ns);
                _dicNamespaceMap[ns] = new NamespaceMap(prefix, ns);
            }

            //Look for CPA info in our cache that keeps contentProperty names per Type
            //If it doesn't have an entry, go get the info and store it.
            if (!_contentProperties.ContainsKey(objectType))
            {
                string lookedUpContentProperty = string.Empty;

                foreach (Attribute attr in markupObj.Attributes)
                {
                    ContentPropertyAttribute cpa = attr as ContentPropertyAttribute;
                    if (cpa != null)
                    {
                        lookedUpContentProperty = cpa.Name;
                        //Once content property is found, come out of the loop.
                        break;
                    }
                }

                _contentProperties.Add(objectType, lookedUpContentProperty);
            }

            contentPropertyName = _contentProperties[objectType];

            string contentString = string.Empty;

            foreach (MarkupProperty markupProperty in markupObj.Properties)
            {
                if (markupProperty.Name != contentPropertyName)
                {
                    if (markupProperty.IsValueAsString)
                    {
                        contentString = markupProperty.Value as string;
                    }
                    else if (!markupProperty.IsComposite)
                    {
                        //Bug Fix DX-0120123
                        if (markupProperty.DependencyProperty != null)
                        {
                            string ns1 = _namespaceCache.GetNamespaceUriFor(
                                markupProperty.DependencyProperty.OwnerType);
                            string prefix1 = _namespaceCache.GetDefaultPrefixFor(ns1);

                            if (!string.IsNullOrWhiteSpace(prefix1))
                            {
                                _dicNamespaceMap[ns1] = new NamespaceMap(prefix1, ns1);
                            }
                        }
                    }
                    else if (markupProperty.Value.GetType() == _nullType)
                    {
                    }
                    else
                    {
                        propertyElements.Add(markupProperty);
                    }
                }
                else
                {
                    contentProperty = markupProperty;
                }
            }

            if (contentProperty != null || propertyElements.Count > 0 || contentString != string.Empty)
            {
                foreach (MarkupProperty markupProp in propertyElements)
                {
                    string ns2 = _namespaceCache.GetNamespaceUriFor(markupObj.ObjectType);
                    if (!string.IsNullOrWhiteSpace(ns2))
                    {
                        string prefix2 = _namespaceCache.GetDefaultPrefixFor(ns2);
                        _dicNamespaceMap[ns2] = new NamespaceMap(prefix2, ns2);
                    }
                    ResolveChildXmlNamespaces(markupProp);
                }

                if (contentProperty != null)
                {
                    if (!(contentProperty.Value is String))
                    {
                        ResolveChildXmlNamespaces(contentProperty);
                    }
                }
            }
        }
        private void WriteObject(object key, object obj, XmlWriter writer, bool isRoot)
        {
            List <MarkupProperty> propertyElements = new List <MarkupProperty>();
            MarkupProperty        contentProperty  = null;
            string       contentPropertyName       = null;
            MarkupObject markupObj  = MarkupWriter.GetMarkupObjectFor(obj);
            Type         objectType = markupObj.ObjectType;

            string ns     = _namespaceCache.GetNamespaceUriFor(objectType);
            string prefix = _namespaceCache.GetDefaultPrefixFor(ns);

            if (isRoot)
            {
                if (string.IsNullOrWhiteSpace(prefix))
                {
                    if (string.IsNullOrWhiteSpace(ns))
                    {
                        writer.WriteStartElement(markupObj.ObjectType.Name, NamespaceCache.DefaultNamespace);
                        writer.WriteAttributeString("xmlns",
                                                    NamespaceCache.XmlnsNamespace, NamespaceCache.DefaultNamespace);
                    }
                    else
                    {
                        writer.WriteStartElement(markupObj.ObjectType.Name, ns);
                        writer.WriteAttributeString("xmlns", NamespaceCache.XmlnsNamespace, ns);
                    }
                }
                else
                {
                    writer.WriteStartElement(prefix, markupObj.ObjectType.Name, ns);
                }
                writer.WriteAttributeString("xmlns", "x",
                                            NamespaceCache.XmlnsNamespace, NamespaceCache.XamlNamespace);

                foreach (NamespaceMap map in _dicNamespaceMap.Values)
                {
                    if (!string.IsNullOrWhiteSpace(map.Prefix) && !string.Equals(map.Prefix, "x"))
                    {
                        writer.WriteAttributeString("xmlns", map.Prefix, NamespaceCache.XmlnsNamespace, map.XmlNamespace);
                    }
                }
            }
            else
            {
                //TODO: Fix - the best way to handle this case...
                if (markupObj.ObjectType.Name == "PathFigureCollection" && markupObj.Instance != null)
                {
                    WriteState writeState = writer.WriteState;

                    if (writeState == WriteState.Element)
                    {
                        //writer.WriteAttributeString("Figures",
                        //    markupObj.Instance.ToString());
                        writer.WriteAttributeString("Figures",
                                                    System.Convert.ToString(markupObj.Instance, _culture));
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(prefix))
                        {
                            writer.WriteStartElement("PathGeometry.Figures");
                        }
                        else
                        {
                            writer.WriteStartElement("PathGeometry.Figures", ns);
                        }
                        //writer.WriteString(markupObj.Instance.ToString());
                        writer.WriteString(System.Convert.ToString(
                                               markupObj.Instance, _culture));
                        writer.WriteEndElement();
                    }
                    return;
                }
                else
                {
                    if (string.IsNullOrWhiteSpace(prefix))
                    {
                        writer.WriteStartElement(markupObj.ObjectType.Name);
                    }
                    else
                    {
                        writer.WriteStartElement(markupObj.ObjectType.Name, ns);
                    }
                }
            }

            // Add the x:Name for object like Geometry/Drawing not derived from FrameworkElement...
            DependencyObject dep = obj as DependencyObject;

            if (dep != null)
            {
                string nameValue = dep.GetValue(FrameworkElement.NameProperty) as string;
                if (!string.IsNullOrWhiteSpace(nameValue) && !(dep is FrameworkElement))
                {
                    writer.WriteAttributeString("x", "Name", NamespaceCache.XamlNamespace, nameValue);
                }
            }

            if (key != null)
            {
                string keyString = key.ToString();
                if (keyString.Length > 0)
                {
                    writer.WriteAttributeString("x", "Key", NamespaceCache.XamlNamespace, keyString);
                }
                else
                {
                    //TODO: key may not be a string, what about x:Type...
                    throw new NotImplementedException(
                              "Sample XamlWriter cannot yet handle keys that aren't strings");
                }
            }

            //Look for CPA info in our cache that keeps contentProperty names per Type
            //If it doesn't have an entry, go get the info and store it.
            if (!_contentProperties.ContainsKey(objectType))
            {
                string lookedUpContentProperty = string.Empty;
                foreach (Attribute attr in markupObj.Attributes)
                {
                    ContentPropertyAttribute cpa = attr as ContentPropertyAttribute;
                    if (cpa != null)
                    {
                        lookedUpContentProperty = cpa.Name;
                        //Once content property is found, come out of the loop.
                        break;
                    }
                }

                _contentProperties.Add(objectType, lookedUpContentProperty);
            }

            contentPropertyName = _contentProperties[objectType];
            string contentString = string.Empty;

            foreach (MarkupProperty markupProperty in markupObj.Properties)
            {
                if (markupProperty.Name != contentPropertyName)
                {
                    if (markupProperty.IsValueAsString)
                    {
                        contentString = markupProperty.Value as string;
                    }
                    else if (!markupProperty.IsComposite)
                    {
                        string temp = markupProperty.StringValue;

                        if (markupProperty.IsAttached)
                        {
                            string ns1     = _namespaceCache.GetNamespaceUriFor(markupProperty.DependencyProperty.OwnerType);
                            string prefix1 = _namespaceCache.GetDefaultPrefixFor(ns1);

                            if (temp.IndexOfAny("{}".ToCharArray()) >= 0)
                            {
                                temp = "{}" + temp;
                            }
                            if (string.IsNullOrWhiteSpace(prefix1))
                            {
                                writer.WriteAttributeString(markupProperty.Name, temp);
                            }
                            else
                            {
                                writer.WriteAttributeString(markupProperty.Name, ns1, temp);
                            }
                        }
                        else
                        {
                            if (markupProperty.Name == "FontUri" &&
                                (_wpfSettings != null && _wpfSettings.IncludeRuntime))
                            {
                                string fontUri = temp.ToLower();
                                fontUri = fontUri.Replace(_windowsDir, _windowsPath);

                                StringBuilder builder = new StringBuilder();
                                builder.Append("{");
                                builder.Append("svg");
                                builder.Append(":");
                                builder.Append("SvgFontUri ");
                                builder.Append(fontUri.Replace('\\', '/'));
                                builder.Append("}");

                                writer.WriteAttributeString(markupProperty.Name, builder.ToString());
                            }
                            else
                            {
                                if (temp.IndexOfAny("{}".ToCharArray()) >= 0)
                                {
                                    temp = "{}" + temp;
                                }
                                writer.WriteAttributeString(markupProperty.Name, temp);
                            }
                        }
                    }
                    else if (markupProperty.Value.GetType() == _nullType)
                    {
                        if (_nullExtension)
                        {
                            writer.WriteAttributeString(markupProperty.Name, "{x:Null}");
                        }
                    }
                    else
                    {
                        propertyElements.Add(markupProperty);
                    }
                }
                else
                {
                    contentProperty = markupProperty;
                }
            }

            if (contentProperty != null || propertyElements.Count > 0 || contentString != string.Empty)
            {
                foreach (MarkupProperty markupProp in propertyElements)
                {
                    string ns2     = _namespaceCache.GetNamespaceUriFor(markupObj.ObjectType);
                    string prefix2 = null;
                    if (!string.IsNullOrWhiteSpace(ns2))
                    {
                        prefix2 = _namespaceCache.GetDefaultPrefixFor(ns2);
                    }

                    string propElementName = markupObj.ObjectType.Name + "." + markupProp.Name;
                    if (string.IsNullOrWhiteSpace(prefix2))
                    {
                        writer.WriteStartElement(propElementName);
                    }
                    else
                    {
                        writer.WriteStartElement(prefix2, propElementName, ns2);
                    }

                    WriteChildren(writer, markupProp);
                    writer.WriteEndElement();
                }

                if (contentString != string.Empty)
                {
                    writer.WriteValue(contentString);
                }
                else if (contentProperty != null)
                {
                    if (contentProperty.Value is string)
                    {
                        writer.WriteValue(contentProperty.StringValue);
                    }
                    else
                    {
                        WriteChildren(writer, contentProperty);
                    }
                }
            }
            writer.WriteEndElement();
        }