Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 8849041

Browse files
author
gumme
committed
Merge branch 'WpfDesignerParserFixes'
Conflicts: src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlProperty.cs
2 parents 9f719f9 + 91ef4ab commit 8849041

6 files changed

Lines changed: 159 additions & 14 deletions

File tree

src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/XamlDom/ExampleClassContainer.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ public class ExampleClassList : List<ExampleClass>
1111
{
1212
}
1313

14+
public class ExampleClassDictionary : Dictionary<string, ExampleClass>
15+
{
16+
}
17+
1418
[ContentProperty("List")]
1519
public class ExampleClassContainer : ExampleClass
1620
{
@@ -35,5 +39,18 @@ public ExampleClassList OtherList {
3539
otherList = value;
3640
}
3741
}
42+
43+
ExampleClassDictionary dictionary = new ExampleClassDictionary();
44+
45+
public ExampleClassDictionary Dictionary {
46+
get {
47+
TestHelperLog.Log("Dictionary.get " + Identity);
48+
return dictionary;
49+
}
50+
set {
51+
TestHelperLog.Log("Dictionary.set " + Identity);
52+
dictionary = value;
53+
}
54+
}
3855
}
3956
}

src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/XamlDom/ExampleService.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ public static void SetExample(DependencyObject element, string value)
2626
TestHelperLog.Log("ExampleService.SetExample");
2727
element.SetValue(ExampleProperty, value);
2828
}
29+
30+
public static readonly DependencyProperty ExampleCollectionProperty = DependencyProperty.RegisterAttached(
31+
"ExampleCollection", typeof(ExampleClassList), typeof(ExampleService)
32+
);
33+
34+
public static ExampleClassList GetExampleCollection(DependencyObject element)
35+
{
36+
TestHelperLog.Log("ExampleService.GetExampleCollection");
37+
return (ExampleClassList)element.GetValue(ExampleCollectionProperty);
38+
}
39+
40+
public static void SetExampleCollection(DependencyObject element, ExampleClassList value)
41+
{
42+
TestHelperLog.Log("ExampleService.SetExampleCollection");
43+
element.SetValue(ExampleCollectionProperty, value);
44+
}
2945
}
3046

3147
public class ExampleDependencyObject : DependencyObject

src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Tests/XamlDom/SimpleLoadTests.cs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,88 @@ public void ContainerExplicitList()
208208
");
209209
}
210210

211+
[Test]
212+
public void ContainerImplicitDictionary()
213+
{
214+
TestLoading(@"
215+
<ExampleClassContainer
216+
xmlns=""" + XamlTypeFinderTests.XamlDomTestsNamespace + @"""
217+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
218+
<ExampleClassContainer.Dictionary>
219+
<ExampleClass x:Key=""key1"" OtherProp=""a""> </ExampleClass>
220+
<ExampleClass x:Key=""key2"" OtherProp=""b"" />
221+
<ExampleClass x:Key=""key3"" OtherProp=""c"" />
222+
</ExampleClassContainer.Dictionary>
223+
</ExampleClassContainer>
224+
");
225+
}
226+
227+
[Test]
228+
public void ContainerExplicitDictionary()
229+
{
230+
TestLoading(@"
231+
<ExampleClassContainer
232+
xmlns=""" + XamlTypeFinderTests.XamlDomTestsNamespace + @"""
233+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
234+
<ExampleClassContainer.Dictionary>
235+
<ExampleClassDictionary>
236+
<ExampleClass x:Key=""key1"" OtherProp=""a""> </ExampleClass>
237+
<ExampleClass x:Key=""key2"" OtherProp=""b"" />
238+
<ExampleClass x:Key=""key3"" OtherProp=""c"" />
239+
</ExampleClassDictionary>
240+
</ExampleClassContainer.Dictionary>
241+
</ExampleClassContainer>
242+
");
243+
}
244+
245+
[Test]
246+
public void ResourceDictionaryImplicit()
247+
{
248+
TestLoading(@"
249+
<Window
250+
xmlns=""http://schemas.microsoft.com/netfx/2007/xaml/presentation""
251+
xmlns:t=""" + XamlTypeFinderTests.XamlDomTestsNamespace + @"""
252+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
253+
<Window.Resources>
254+
<t:ExampleClass x:Key=""key1"" OtherProp=""a""> </t:ExampleClass>
255+
<t:ExampleClass x:Key=""key2"" OtherProp=""b"" />
256+
</Window.Resources>
257+
</Window>
258+
");
259+
}
260+
261+
[Test]
262+
public void ResourceDictionaryExplicitWinfx2006()
263+
{
264+
ResourceDictionaryExplicitInternal("http://schemas.microsoft.com/winfx/2006/xaml/presentation");
265+
}
266+
267+
[Test]
268+
[Ignore("Own XamlParser should handle different namespaces pointing to same types, because builtin XamlReader does.")]
269+
public void ResourceDictionaryExplicitNetfx2007()
270+
{
271+
// The reason this test case fails is because own XamlParser cannot always handle the case where multiple xmlns points to the same type.
272+
// In this test case the default xmlns is set to netfx/20007 (compare with the test above that uses winfx/2006 and is successfully executed).
273+
ResourceDictionaryExplicitInternal("http://schemas.microsoft.com/netfx/2007/xaml/presentation");
274+
}
275+
276+
void ResourceDictionaryExplicitInternal(string defaultXmlns)
277+
{
278+
TestLoading(@"
279+
<Window
280+
xmlns=""" + defaultXmlns + @"""
281+
xmlns:t=""" + XamlTypeFinderTests.XamlDomTestsNamespace + @"""
282+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
283+
<Window.Resources>
284+
<ResourceDictionary>
285+
<t:ExampleClass x:Key=""key1"" OtherProp=""a""> </t:ExampleClass>
286+
<t:ExampleClass x:Key=""key2"" OtherProp=""b"" />
287+
</ResourceDictionary>
288+
</Window.Resources>
289+
</Window>
290+
");
291+
}
292+
211293
[Test]
212294
public void ExampleServiceTest()
213295
{
@@ -221,6 +303,25 @@ public void ExampleServiceTest()
221303
");
222304
}
223305

306+
[Test]
307+
public void ExampleServiceCollectionTest()
308+
{
309+
TestLoading(@"
310+
<t:ExampleDependencyObject
311+
xmlns=""http://schemas.microsoft.com/netfx/2007/xaml/presentation""
312+
xmlns:t=""" + XamlTypeFinderTests.XamlDomTestsNamespace + @"""
313+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
314+
<t:ExampleService.ExampleCollection>
315+
<t:ExampleClassList>
316+
<t:ExampleClass OtherProp=""a""> </t:ExampleClass>
317+
<t:ExampleClass OtherProp=""b"" />
318+
<t:ExampleClass OtherProp=""c"" />
319+
</t:ExampleClassList>
320+
</t:ExampleService.ExampleCollection>
321+
</t:ExampleDependencyObject>
322+
");
323+
}
324+
224325
[Test]
225326
public void ExampleClassObjectPropWithStringValue()
226327
{

src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/CollectionSupport.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static bool IsCollectionType(Type type)
2525
return typeof(IList).IsAssignableFrom(type)
2626
|| type.IsArray
2727
|| typeof(IAddChild).IsAssignableFrom(type)
28-
|| typeof(ResourceDictionary).IsAssignableFrom(type);
28+
|| typeof(IDictionary).IsAssignableFrom(type);
2929
}
3030

3131
/// <summary>
@@ -65,7 +65,7 @@ public static void AddToCollection(Type collectionType, object collectionInstanc
6565
} else {
6666
addChild.AddChild(newElement.GetValueFor(null));
6767
}
68-
} else if (collectionInstance is ResourceDictionary) {
68+
} else if (collectionInstance is IDictionary) {
6969
object val = newElement.GetValueFor(null);
7070
object key = newElement is XamlObject ? ((XamlObject)newElement).GetXamlAttribute("Key") : null;
7171
//if (key == null || key == "") {
@@ -74,7 +74,7 @@ public static void AddToCollection(Type collectionType, object collectionInstanc
7474
//}
7575
if (key == null || key == "")
7676
key = val;
77-
((ResourceDictionary)collectionInstance).Add(key, val);
77+
((IDictionary)collectionInstance).Add(key, val);
7878
} else {
7979
collectionType.InvokeMember(
8080
"Add", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance,

src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlProperty.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,21 @@ bool IsFirstChildResources(XamlObject obj)
276276
obj.Properties.Where((prop) => prop.IsResources).FirstOrDefault() != null;
277277
}
278278

279-
XmlElement CreatePropertyElement()
280-
{
281-
string ns = parentObject.OwnerDocument.GetNamespaceFor(parentObject.ElementType);
282-
return parentObject.OwnerDocument.XmlDocument.CreateElement(
283-
parentObject.OwnerDocument.GetPrefixForNamespace(ns),
284-
parentObject.ElementType.Name + "." + this.PropertyName,
285-
ns
286-
);
287-
}
279+
XmlElement CreatePropertyElement()
280+
{
281+
Type propertyElementType = GetPropertyElementType();
282+
string ns = parentObject.OwnerDocument.GetNamespaceFor(propertyElementType);
283+
return parentObject.OwnerDocument.XmlDocument.CreateElement(
284+
parentObject.OwnerDocument.GetPrefixForNamespace(ns),
285+
propertyElementType.Name + "." + this.PropertyName,
286+
ns
287+
);
288+
}
289+
290+
Type GetPropertyElementType()
291+
{
292+
return this.IsAttached ? this.PropertyTargetType : parentObject.ElementType;
293+
}
288294

289295
static XmlNode FindChildNode(XmlNode node, string localName, string namespaceURI)
290296
{
@@ -305,7 +311,8 @@ internal void AddChildNodeToProperty(XmlNode newChildNode)
305311
{
306312
if (this.IsCollection) {
307313
if (IsNodeCollectionForThisProperty(newChildNode)) {
308-
XmlNode parentNode = FindChildNode(parentObject.XmlElement, parentObject.ElementType.Name + "." + this.PropertyName, parentObject.OwnerDocument.GetNamespaceFor(parentObject.ElementType));
314+
Type propertyElementType = GetPropertyElementType();
315+
XmlNode parentNode = FindChildNode(parentObject.XmlElement, propertyElementType.Name + "." + this.PropertyName, parentObject.OwnerDocument.GetNamespaceFor(propertyElementType));
309316

310317
if (parentNode == null) {
311318
parentNode = CreatePropertyElement();

src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/XamlPropertyInfo.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ internal class XamlDependencyPropertyInfo : XamlPropertyInfo
3939
{
4040
readonly DependencyProperty property;
4141
readonly bool isAttached;
42+
readonly bool isCollection;
4243

4344
public override DependencyProperty DependencyProperty {
4445
get { return property; }
@@ -49,6 +50,7 @@ public XamlDependencyPropertyInfo(DependencyProperty property, bool isAttached)
4950
Debug.Assert(property != null);
5051
this.property = property;
5152
this.isAttached = isAttached;
53+
this.isCollection = CollectionSupport.IsCollectionType(property.PropertyType);
5254
}
5355

5456
public override TypeConverter TypeConverter {
@@ -84,7 +86,9 @@ public override bool IsAttached {
8486
}
8587

8688
public override bool IsCollection {
87-
get { return false; }
89+
get {
90+
return isCollection;
91+
}
8892
}
8993

9094
public override object GetValue(object instance)

0 commit comments

Comments
 (0)