/// <summary> /// /// </summary> /// <param name="lookupTypes"></param> /// <returns></returns> static string ChooseResourceLookupType(ResourceLookupTypes lookupTypes) { if (ResourceLookupTypes.None == lookupTypes) { return(String.Empty); } if (((lookupTypes & ResourceLookupTypes.Dynamic) == ResourceLookupTypes.Dynamic) && ((lookupTypes & ResourceLookupTypes.Static) == ResourceLookupTypes.Static)) { if (WPFXamlGenerator.ShouldThrottle(1, 2)) { return("StaticResource"); } else { return("DynamicResource"); } } if ((lookupTypes & ResourceLookupTypes.Dynamic) == ResourceLookupTypes.Dynamic) { return("DynamicResource"); } if ((lookupTypes & ResourceLookupTypes.Static) == ResourceLookupTypes.Static) { return("StaticResource"); } throw new Exception("TestException"); }
/// <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); } } }
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> } } } }
/// <summary> /// /// </summary> /// <param name="xmlDoc"></param> /// <param name="listAll"></param> static void ScanForResources(XmlDocument xmlDoc, List <ResourceSourceNode> listAll) { XmlElement xmlRoot = xmlDoc.DocumentElement; NameTable table = new NameTable(); XmlNamespaceManager mngr = new XmlNamespaceManager(table); mngr.AddNamespace("x", XamlURI); XmlNodeList listResourceNodex = xmlDoc.SelectNodes("//*[@x:Key]", mngr); foreach (XmlNode resourceNode in listResourceNodex) { if (WPFXamlGenerator.ShouldThrottle(1, 2)) { continue; } XmlElement resourceElement = (XmlElement)resourceNode; string keyName = resourceElement.GetAttribute(KeyName, XamlURI); ResourceSourceNode bag = new ResourceSourceNode(resourceElement); bag.Key = keyName; SetShareableState(bag, resourceElement); // Validates that the parent on the resource XML Node // is Foo.Resources collection. if (resourceElement.ParentNode.Name.IndexOf(".Resource") != -1) { // Getting the grandparent for the assumption that of we are under Foo.Resources // and properties can be applied to the GrandParent node. // <TextBox> <=== GrandParent // <TextBox.Resources> // <SolidColorBrush Color="Red" /> <=== ResourceElement // </TextBox.Resources> // <Button /> <=== TargetNode // </TextBox> // XmlElement gradparentNode = resourceElement.ParentNode.ParentNode as XmlElement; XmlNodeList descendatNodesList = gradparentNode.SelectNodes("descendant::*"); // First scan the grandparent node to find properties. ScanSingleInstance(bag, gradparentNode, resourceElement); // Now we scan all the descendents of the grandparent node. foreach (XmlNode node in descendatNodesList) { XmlElement targetNode = node as XmlElement; ScanSingleInstance(bag, targetNode, resourceElement); } } if (bag.Properties.Count > 0) { listAll.Add(bag); } } }