Exemplo n.º 1
0
 internal XName FindTargetType(XName key)
 {
     foreach (var targetType in targetTypes.Keys)
     {
         var map = targetTypes[targetType];
         if (map.ContainsKey(key))
         {
             return(targetType);
         }
     }
     if (Parent != null)
     {
         return(Parent.FindTargetType(key));
     }
     return(null);
 }
Exemplo n.º 2
0
        private void WalkResources(string fileName, XElement root, NamespaceScope scope, Styles styles)
        {
            var local = new NamespaceScope(scope);

            AddNamespaces(root, local);

            string codeRef = null;

            foreach (var a in root.Attributes())
            {
                if (a.Name.LocalName == "TargetType")
                {
                    // should have already been found in FindStyles.
                }
                else if (a.Name.LocalName == "Key" && a.Name.Namespace == XamlNsUri)
                {
                    // should have already been found in FindStyles.
                }
                else if (a.Name.LocalName == "DataType")
                {
                    // todo: check type references
                }
                else if (a.Name.LocalName.Contains("CodeRef"))
                {
                    codeRef = a.Value;
                }
                else if (a.Name.LocalName != "xmlns" && a.Name.Namespace != XmlNsUri)
                {
                    CheckReferences(GetTargetTypeName(root, a), a.Value, local, styles);
                }
            }

            Styles localStyles = null;

            // see if this element contains local styles
            foreach (var e in root.Elements())
            {
                // find any local styles.
                if (e.Name.LocalName.EndsWith(".Resources"))
                {
                    if (localStyles == null)
                    {
                        localStyles = new Styles(fileName + "+" + e.Name, styles);
                        styles      = localStyles;
                    }
                    FindStyles(fileName, e, local, localStyles);
                }
            }

            if (!string.IsNullOrEmpty(codeRef))
            {
                // ah then the named styles are referenced from code, so record that fact
                foreach (var part in codeRef.Split(','))
                {
                    if (string.IsNullOrWhiteSpace(part))
                    {
                        continue;
                    }
                    XName reference = QualifyName(part.Trim(), local);
                    var   style     = styles.FindStyle(null, reference);
                    if (style == null)
                    {
                        // might have been a TargetTyped resource
                        XName targetType = styles.FindTargetType(reference);
                        if (targetType != null)
                        {
                            style = styles.FindStyle(targetType, reference);
                            if (style == null)
                            {
                                // might be a unnamed key TargetType only match
                                style = styles.FindStyle(targetType, emptyName);
                            }
                        }
                        if (style == null)
                        {
                            Program.WriteError("CodeRef {0} not found", reference.ToString());
                        }
                    }
                }
            }

            // record possible reference to a TargetType of the matching element name.
            styles.FindStyle(GetTargetTypeName(root, null), Program.emptyName);

            // Now we have the "usage" of styles, either something in a UserControl, or a ControlTemplate in a ResourceDictionary.
            foreach (var e in root.Elements())
            {
                WalkResources(fileName, e, local, styles);
            }

            if (localStyles != null)
            {
                localStyles.ReportUnreferenced();
            }
        }