Exemplo n.º 1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="property"></param>
 public void AddProperty(TargetNodeToApply property)
 {
     for (int i = 0; i < Properties.Count; i++)
     {
         if (Properties[i].NodeToApply == property.NodeToApply)
         {
             return;
         }
     }
     Properties.Add(property);
 }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        static void ScanSingleInstance(ResourceSourceNode resourceSourceNode, XmlElement targetNode, XmlElement resourceElement)
        {
            XmlElement gradparentNode = resourceElement.ParentNode.ParentNode as XmlElement;

            if (IsInvalid(gradparentNode, resourceElement, targetNode))
            {
                return;
            }

            PropertyInfo[] properties = null;

            properties = GetPropertiesWherePossibleToApply(resourceElement, targetNode);

            if (properties != null && properties.Length > 0)
            {
                TargetNodeToApply toApply = new TargetNodeToApply(targetNode, properties);

                resourceSourceNode.Properties.Add(toApply);
            }
        }
Exemplo n.º 3
0
        static void ApplyResourceToXaml(
            XmlDocument xmlDoc,
            XmlElement sourceNode,
            string keyValue,
            TargetNodeToApply pApply,
            XmlElement targetNode,
            PropertyInfo targetProperty)
        {
            object xmlTarget     = null;
            string cpa           = null;
            bool   isPropertySet = IsPropertySet(targetNode, targetProperty, out xmlTarget, out cpa);

            //
            // Set a resource reference on a property using attribute or
            // property element syntax.
            //

            if (IsIList(targetProperty.PropertyType))
            {
                //
                return;
            }
            else
            {
                if (isPropertySet)
                {
                    if (xmlTarget is XmlAttribute)
                    {
                        XmlAttribute attribute = (XmlAttribute)xmlTarget;

                        SetXmlAttributeValue(attribute, keyValue, targetNode, sourceNode, targetProperty);
                    }
                    else
                    {
                        //
                        return;
                    }
                }
                else
                {
                    if (WPFXamlGenerator.ShouldThrottle(1, 2))
                    {
                        XmlAttribute attribute = xmlDoc.CreateAttribute(targetProperty.Name);

                        bool value = SetXmlAttributeValue(attribute, keyValue, targetNode, sourceNode, targetProperty);

                        if (!value)
                        {
                            return;
                        }

                        targetNode.SetAttributeNode(attribute);
                    }
                    else
                    {
                        // <ParentName.PropertyName>

                        ResourceLookupTypes lookupType = FindResourceLookupType(targetNode, sourceNode, targetProperty);

                        string lookupString = ChooseResourceLookupType(lookupType);

                        if (lookupString == String.Empty)
                        {
                            return;
                        }

                        string     tagName = targetNode.Name + "." + targetProperty.Name;
                        XmlElement e       = (XmlElement)xmlDoc.CreateElement(tagName, targetNode.NamespaceURI);
                        targetNode.PrependChild(e);


                        string typeDSResource = lookupString;

                        XmlElement e2 = (XmlElement)xmlDoc.CreateElement(typeDSResource, AvalonURI);
                        e2.SetAttribute("ResourceKey", keyValue);
                        e.AppendChild(e2);

                        // </ParentName.PropertyName>
                    }
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="xmlDoc"></param>
        /// <param name="listAll"></param>
        /// <returns></returns>
        static void ApplyResources(XmlDocument xmlDoc, List <ResourceSourceNode> listAll)
        {
            List <XmlElement>        unshareableResources = new List <XmlElement>();
            List <TargetNodeToApply> targetNodeList       = new List <TargetNodeToApply>();

            foreach (ResourceSourceNode infoBag in listAll)
            {
                XmlElement sourceNode = infoBag.SourceNode;
                string     keyValue   = sourceNode.GetAttribute(KeyName, XamlURI);

                // Check that we don't apply an unshareable resource twice.
                if (unshareableResources.Contains(sourceNode))
                {
                    throw new ApplicationException("ResourceSourceNode duplicate found in list.");
                }

                // Skip this resource if it can't be set on any property or we want to throttle
                // the number of references.
                if (infoBag.Properties.Count == 0 || WPFXamlGenerator.ShouldThrottle(1, 4))
                {
                    continue;
                }

                int amountApply = 1;

                if (!infoBag.XShared && infoBag.Properties.Count > 1)
                {
                    amountApply = GetRandomNumber(infoBag.Properties.Count);
                    targetNodeList.AddRange(infoBag.Properties.ToArray());
                }
                else
                {
                    targetNodeList.Add(infoBag.Properties[GetRandomNumber(infoBag.Properties.Count)]);
                }

                for (int i = 0; i < amountApply; i++)
                {
                    int index = GetRandomNumber(targetNodeList.Count);

                    TargetNodeToApply pApply = targetNodeList[index];

                    targetNodeList.RemoveAt(index);

                    XmlElement   targetNode     = pApply.NodeToApply;
                    PropertyInfo targetProperty = pApply.Properties[GetRandomNumber(pApply.Properties.Length)];

                    ApplyResourceToXaml(xmlDoc,
                                        sourceNode,
                                        keyValue,
                                        pApply,
                                        targetNode,
                                        targetProperty);
                }

                targetNodeList.Clear();

                //
                // If a reference to an unshareable resource was set on a property,
                // keep track of it so we don't set it again.
                //
                if (DoesMatch(infoBag.ResourceType, _unshareableTypes))
                {
                    unshareableResources.Add(sourceNode);
                }
            }
        }