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

Commit 91ef4ab

Browse files
author
gumme
committed
IDictionary (and not just ResourceDictionary) is considered a collection and supported by own XamlParser.
1 parent 14dd5a9 commit 91ef4ab

3 files changed

Lines changed: 102 additions & 3 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/SimpleLoadTests.cs

Lines changed: 82 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
{

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,

0 commit comments

Comments
 (0)