From 19486b6cf2cc7fbaaea36d44fb589169d6089d87 Mon Sep 17 00:00:00 2001 From: jogibear9988 Date: Mon, 24 Nov 2014 20:49:48 +0100 Subject: [PATCH] Fix multiple XML-Namspaces for the same CLR-Namespace Now all UnitTests work --- .../Tests/XamlDom/SimpleLoadTests.cs | 1 - .../WpfDesign.XamlDom/Project/XamlDocument.cs | 10 +++++++ .../WpfDesign.XamlDom/Project/XamlProperty.cs | 8 +++--- .../Project/XamlTypeFinder.cs | 27 +++++++++++++++++++ 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/XamlDom/SimpleLoadTests.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/XamlDom/SimpleLoadTests.cs index 1854242157..b60120ff35 100644 --- a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/XamlDom/SimpleLoadTests.cs +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/XamlDom/SimpleLoadTests.cs @@ -293,7 +293,6 @@ namespace ICSharpCode.WpfDesign.Tests.XamlDom } [Test] - [Ignore("Own XamlParser should handle different namespaces pointing to same types, because builtin XamlReader does.")] public void ResourceDictionaryExplicitNetfx2007() { // The reason this test case fails is because own XamlParser cannot always handle the case where multiple xmlns points to the same type. diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlDocument.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlDocument.cs index b09468d7c8..49abdacf13 100644 --- a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlDocument.cs +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlDocument.cs @@ -228,6 +228,16 @@ namespace ICSharpCode.WpfDesign.XamlDom return _typeFinder.GetXmlNamespaceFor(type.Assembly, type.Namespace, getClrNamespace); } + + internal List GetNamespacesFor(Type type, bool getClrNamespace = false) + { + if (type == typeof (DesignTimeProperties)) + return new List(){XamlConstants.DesignTimeNamespace}; + if (type == typeof (MarkupCompatibilityProperties)) + return new List(){XamlConstants.MarkupCompatibilityNamespace}; + + return _typeFinder.GetXmlNamespacesFor(type.Assembly, type.Namespace, getClrNamespace); + } internal string GetPrefixForNamespace(string @namespace) { diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlProperty.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlProperty.cs index 6e2e3fb176..d408c95746 100644 --- a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlProperty.cs +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlProperty.cs @@ -356,19 +356,19 @@ namespace ICSharpCode.WpfDesign.XamlDom static XmlNode FindChildNode(XmlNode node, Type elementType, string propertyName, XamlDocument xamlDocument) { var localName = elementType.Name + "." + propertyName; - var namespaceURI = xamlDocument.GetNamespaceFor(elementType); + var namespacesURI = xamlDocument.GetNamespacesFor(elementType); var clrNamespaceURI = xamlDocument.GetNamespaceFor(elementType, true); foreach (XmlNode childNode in node.ChildNodes) { - if (childNode.LocalName == localName && (childNode.NamespaceURI == namespaceURI || childNode.NamespaceURI == clrNamespaceURI)) + if (childNode.LocalName == localName && (namespacesURI.Contains(childNode.NamespaceURI) || childNode.NamespaceURI == clrNamespaceURI)) { return childNode; } } var type = elementType.BaseType; - namespaceURI = xamlDocument.GetNamespaceFor(type); + namespacesURI = xamlDocument.GetNamespacesFor(type); while (type != typeof(object)) { @@ -379,7 +379,7 @@ namespace ICSharpCode.WpfDesign.XamlDom foreach (XmlNode childNode in node.ChildNodes) { - if (childNode.LocalName == localName && childNode.NamespaceURI == namespaceURI) + if (childNode.LocalName == localName && namespacesURI.Contains(childNode.NamespaceURI)) { return childNode; } diff --git a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlTypeFinder.cs b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlTypeFinder.cs index ac8f9ee102..3373b4772d 100644 --- a/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlTypeFinder.cs +++ b/src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlTypeFinder.cs @@ -19,6 +19,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Linq; using System.Reflection; using System.Windows.Markup; using System.Xaml; @@ -82,6 +83,7 @@ namespace ICSharpCode.WpfDesign.XamlDom Dictionary namespaces = new Dictionary(); Dictionary reverseDict = new Dictionary(); + Dictionary> reverseDictList = new Dictionary>(); /// /// Gets a type referenced in XAML. @@ -128,6 +130,20 @@ namespace ICSharpCode.WpfDesign.XamlDom } } + /// + /// Gets the XML namespaces that can be used for the specified assembly/namespace combination. + /// + public List GetXmlNamespacesFor(Assembly assembly, string @namespace, bool getClrNamespace = false) + { + AssemblyNamespaceMapping mapping = new AssemblyNamespaceMapping(assembly, @namespace); + List xmlNamespaces; + if (!getClrNamespace && reverseDictList.TryGetValue(mapping, out xmlNamespaces)) { + return xmlNamespaces; + } else { + return new List() { "clr-namespace:" + mapping.Namespace + ";assembly=" + mapping.Assembly.GetName().Name }; + } + } + /// /// Gets the prefix to use for the specified XML namespace, /// or null if no suitable prefix could be found. @@ -178,6 +194,14 @@ namespace ICSharpCode.WpfDesign.XamlDom { ns.ClrNamespaces.Add(mapping); + List xmlNamespaceList; + if (reverseDictList.TryGetValue(mapping, out xmlNamespaceList)) { + if (!xmlNamespaceList.Contains(ns.XmlNamespace)) + xmlNamespaceList.Add(ns.XmlNamespace); + } + else + reverseDictList.Add(mapping, new List(){ ns.XmlNamespace }); + string xmlNamespace; if (reverseDict.TryGetValue(mapping, out xmlNamespace)) { if (xmlNamespace == XamlConstants.PresentationNamespace) { @@ -262,6 +286,9 @@ namespace ICSharpCode.WpfDesign.XamlDom foreach (KeyValuePair pair in source.reverseDict) { this.reverseDict.Add(pair.Key, pair.Value); } + foreach (KeyValuePair> pair in source.reverseDictList) { + this.reverseDictList.Add(pair.Key, pair.Value.ToList()); + } } object ICloneable.Clone()