Beispiel #1
0
        /// <summary>
        /// </summary>
        public static ResourceDictionary GenerateResourceDictionary()
        {
            Initialize();

            WPFXamlGenerator.ValidateDefaultFiles();
            return((ResourceDictionary)Parse(WPFXamlGenerator.GenerateResourceDictionary()));
        }
Beispiel #2
0
        /// <summary>
        /// </summary>
        public static object GenerateFreezable()
        {
            Initialize();
            WPFXamlGenerator.ValidateDefaultFiles();

            object obj = null;

            while (obj == null)
            {
                obj = Parse(WPFXamlGenerator.GenerateFreezable());

                if (obj is BitmapEffect && !WPFXamlGenerator.ShouldTestBitmapEffects)
                {
                    obj = null;
                    continue;
                }

                if (obj is Transform && !WPFXamlGenerator.ShouldTestTransforms)
                {
                    obj = null;
                    continue;
                }
            }

            return(obj);
        }
Beispiel #3
0
        /// <summary>
        /// </summary>
        public static object GenerateWindow()
        {
            Initialize();

            WPFXamlGenerator.ValidateDefaultFiles();
            return(Parse(WPFXamlGenerator.GenerateWindow()));
        }
Beispiel #4
0
        /// <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");
        }
Beispiel #5
0
        /// <summary>
        ///
        /// </summary>
        public static WPFLibrary Generate(WPFLibraryParams wpfLibraryParams)
        {
            WPFXamlGenerator.ValidateDefaultFiles();

            List <string> list = new List <string>();

            if (wpfLibraryParams.ResourceFiles != null)
            {
                list.AddRange(wpfLibraryParams.ResourceFiles);
            }
            if (wpfLibraryParams.XamlPages != null)
            {
                list.AddRange(wpfLibraryParams.XamlPages);
            }
            if (wpfLibraryParams.References != null)
            {
                list.AddRange(wpfLibraryParams.References);
            }

            ThrowIfNoAbsolutePath(list);

            WPFLibraryGenerator g = new WPFLibraryGenerator(wpfLibraryParams);

            return(new WPFLibrary(g));
        }
Beispiel #6
0
        private static void _EnsureInitialized()
        {
            if (_initialized)
            {
                return;
            }

            lock (_lockObj)
            {
                if (_initialized)
                {
                    return;
                }

                Initialize(new WPFXamlDefinition());
                WPFXamlGenerator.ValidateDefaultFiles();
            }
        }
Beispiel #7
0
        private static void CleaningResources(XmlNode node, SetupXamlInformation ppParams)
        {
            if (WPFXamlGenerator.IsResourceDictionaryNode(node))
            {
                foreach (XmlNode childResourceNode in node.ChildNodes)
                {
                    XmlElement childResource = childResourceNode as XmlElement;

                    if (childResource == null)
                    {
                        continue;
                    }

                    if (!GetXSharedXmlNodeValue(childResource))
                    {
                        // x:Shared is value is already False;

                        continue;
                    }

                    Type resourceType = FindType(childResource);


                    if (typeof(Visual).IsAssignableFrom(resourceType) || typeof(ContentElement).IsAssignableFrom(resourceType))
                    {
                        if (!ppParams.EnableXamlCompilationFeatures)
                        {
                            node.RemoveChild(childResourceNode);
                        }
                        else
                        {
                            WPFXamlGenerator.SetXSharedValue(childResource, "false");
                        }
                    }
                }
            }
            else
            {
                foreach (XmlNode n in node.ChildNodes)
                {
                    CleaningResources(n, ppParams);
                }
            }
        }
Beispiel #8
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>
                    }
                }
            }
        }
Beispiel #9
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);
                }
            }
        }
Beispiel #10
0
        /// <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);
                }
            }
        }