1
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
3
<title>NameScope test</title>
8
<script type="text/javascript">
13
function Log (color, text)
15
var run = content.createFromXaml ("<Run Foreground='" + color + "'/>");
16
var eoln = content.createFromXaml ("<LineBreak/>");
19
logger.inlines.add (run);
20
logger.inlines.add (eoln);
25
// see what happens when a named brush is set as a property
26
// on multiple elements in the same NameScope.
28
var tree0 = content.createFromXaml ("<Canvas xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Canvas.Top='5' Canvas.Left='5' x:Name='Tree0'><Rectangle x:Name='Rect0' Canvas.Left='0' Width='10' Height='10'><Rectangle.Stroke><SolidColorBrush x:Name='MyBrush' Color='Green'/></Rectangle.Stroke></Rectangle><Rectangle x:Name='Rect1' Canvas.Left='15' Width='10' Height='10'/></Canvas>", true);
30
var rect = tree0.findName ("Rect0");
31
var brush = rect.stroke;
33
rect = tree0.findName ("Rect1");
36
var res = tree0.findName ("MyBrush");
38
Log ("green", "FindName() correctly did not find the re-used brush in the same namescope.");
40
Log ("red", "FindName() incorrectly found the re-used brush in the same namescope.");
45
// see if we can re-use the same brush in a second namespace
46
// and whether or not FindName() will find it in both places.
47
var tree0 = content.createFromXaml ("<Canvas xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Canvas.Top='5' Canvas.Left='5' x:Name='Tree0'><Rectangle x:Name='Rect0' Canvas.Left='0' Width='10' Height='10'><Rectangle.Stroke><SolidColorBrush x:Name='MyBrush' Color='Green'/></Rectangle.Stroke></Rectangle><Rectangle x:Name='Rect1' Canvas.Left='15' Width='10' Height='10'/></Canvas>", true);
49
var tree1 = content.createFromXaml ("<Canvas xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Canvas.Top='5' Canvas.Left='35' x:Name='Tree1'><Rectangle x:Name='Rect2' Canvas.Left='0' Width='10' Height='10'/><Rectangle x:Name='Rect3' Canvas.Left='15' Width='10' Height='10'/></Canvas>", true);
51
var rect = tree0.findName ("Rect0");
52
var brush = rect.stroke;
54
rect = tree1.findName ("Rect2");
57
rect = tree1.findName ("Rect3");
60
res = tree1.findName ("MyBrush");
62
Log ("green", "FindName() correctly did not find the re-used brush in the alternate namescope.");
64
Log ("red", "FindName() incorrectly found the re-used brush in the alternate namescope.");
69
// see if names conflict if added from a separate subtree (which has a temp scope)
71
var tree2 = content.createFromXaml ("<Canvas xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Canvas.Top='5' Canvas.Left='35' x:Name='Tree1'><Rectangle x:Name='Rect2' Canvas.Left='0' Width='10' Height='10'/><Rectangle x:Name='Rect3' Canvas.Left='15' Width='10' Height='10'/></Canvas>", true);
72
var tree3 = content.createFromXaml ("<Canvas xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Canvas.Top='5' Canvas.Left='35' x:Name='Tree2'><Rectangle x:Name='Rect2'/></Canvas>");
76
tree2.children.add (tree3);
82
if (exception != null)
83
Log ("green", "merging namescopes with conflicting names did generate error");
85
Log ("red", "merging namescopes with conflicting names did not generate error");
87
var conflicting = tree3.findName ("Rect2");
89
Log ("red", "after merge failed due to exception, Rect2 was still findName()able in child tree");
91
Log ("green", "after merge failed due to exception, Rect2 was not findName()able in child tree");
93
var original = tree2.findName ("Rect2");
95
Log ("green", "after merge failed due to exception, Rect2 was still findName()able in original tree");
97
Log ("red", "after merge failed due to exception, Rect2 was not findName()able in original tree");
103
// see if names in conflict in the same xaml block generate an error (a child element)
107
var tree4 = content.createFromXaml ("<Canvas xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Canvas.Top='5' Canvas.Left='35' x:Name='Tree1'><Rectangle x:Name='Tree1'/></Canvas>");
113
if (exception != null)
114
Log ("green", "two elements with the same name in the same xaml block did generate an error");
116
Log ("red", "two elements with the same name in the same xaml block did not generate an error");
121
// see if names in conflict in the same xaml block generate an error (a property element)
125
var tree4 = content.createFromXaml ("<Canvas xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Canvas.Top='5' Canvas.Left='35' x:Name='Tree1'><Canvas.Clip><RectangleGeometry x:Name='Tree1'/></Canvas.Clip></Canvas>");
131
if (exception != null)
132
Log ("green", "two elements with the same name (property element) in the same xaml block did generate an error");
134
Log ("red", "two elements with the same name (property element) in the same xaml block did not generate an error");
139
// see if we can remove a child then re-add it and have the name be available
141
var tree5 = content.createFromXaml ("<Canvas xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='Tree2'><Canvas x:Name='Subtree'><Rectangle x:Name='Rect2'/></Canvas></Canvas>");
143
var child = tree5.children.getItem(0);
144
tree5.children.clear ();
146
tree5.printf ("before children.add");
147
tree5.children.add (child);
148
tree5.printf ("after children.add");
150
// if (tree5.findName ("Subtree") != null)
151
// Log ("green", "child is once again findName()able after being removed and re-added");
153
// Log ("red", "child is not findName()able after being removed and re-added");
155
if (tree5.findName ("Rect2") != null)
156
Log ("green", "descendent is once again findName()able after being removed and re-added");
158
Log ("red", "descendent is not findName()able after being removed and re-added");
163
// see if we can remove a child then re-add it and have the name be available
165
var tree5 = content.createFromXaml ("<Canvas xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='Tree2'><Canvas x:Name='Subtree'><Rectangle x:Name='Rect2'/></Canvas></Canvas>");
167
var child = tree5.children.getItem(0);
168
tree5.children.clear ();
170
if (child.findName ("Subtree") == null)
171
Log ("green", "Subtree cannot be looked up in disconnected subtree");
173
Log ("red", "Subtree can be looked up in disconnected subtree");
175
if (child.findName ("Rect2") == null)
176
Log ("green", "Rect2 cannot be looked up in disconnected subtree");
178
Log ("red", "Rect2 can be looked up in disconnected subtree");
180
tree5.children.add (child);
182
if (tree5.findName ("Subtree") != null)
183
Log ("green", "child is once again findName()able after being removed and re-added");
185
Log ("red", "child is not findName()able after being removed and re-added");
187
if (tree5.findName ("Rect2") != null)
188
Log ("green", "descendent is once again findName()able after being removed and re-added");
190
Log ("red", "descendent is not findName()able after being removed and re-added");
195
// see if a temp namescope causes children to no longer be
196
// findName()able if we remove the subtree from the hierarchy and
199
var tree = content.createFromXaml ("<Canvas xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='Tree'/>");
200
var subtree = content.createFromXaml ("<Canvas xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='Subtree'><Rectangle x:Name='Rect2'/></Canvas>");
202
tree.children.add (subtree);
204
if (tree.findName ("Subtree") != null)
205
Log ("green", "child is locatable after adding to hierarchy");
207
Log ("red", "child is not locatable after adding to hierarchy");
209
tree.children.clear ();
211
if (subtree.findName ("Subtree") == null)
212
Log ("green", "Subtree cannot be looked up in disconnected subtree");
214
Log ("red", "Subtree can be looked up in disconnected subtree");
216
if (subtree.findName ("Rect2") == null)
217
Log ("green", "Rect2 cannot be looked up in disconnected subtree");
219
Log ("red", "Rect2 can be looked up in disconnected subtree");
221
tree.children.add (subtree);
223
if (tree.findName ("Rect2") != null)
224
Log ("green", "descendent is once again findName()able after being removed and re-added");
226
Log ("red", "descendent is not findName()able after being removed and re-added");
232
// we create a temp namescope on a subtree, add the subtree to
233
// the main tree, then remove it. then we add a new name to it
234
// and see if we can look up the new name on the subtree. then
235
// we add it back to the main tree and look up the name there.
237
var tree = content.createFromXaml ("<Canvas xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='Tree'/>");
238
var subtree = content.createFromXaml ("<Canvas xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='Subtree'><Rectangle x:Name='Rect2'/></Canvas>");
240
tree.children.add (subtree);
242
tree.children.clear ();
244
var new_child = content.createFromXaml ("<Canvas xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='Subtree2' />");
246
subtree.children.add (new_child);
248
if (subtree.findName ("Subtree2") == null)
249
Log ("green", "Subtree2 cannot be looked up in disconnected subtree");
251
Log ("red", "Subtree2 can be looked up in disconnected subtree");
253
tree.children.add (subtree);
255
if (tree.findName ("Subtree2") != null)
256
Log ("green", "Subtree2 can be looked up once the subtree is reconnected");
258
Log ("red", "Subtree2 cannot be looked up once the subtree is reconnected");
263
// we create a temp namescope on a subtree, add the subtree to the
264
// main tree, then remove it (to kill its namescope). then we add a
265
// conflicting named child to it and see if that flags an error.
267
var tree = content.createFromXaml ("<Canvas xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='Tree'/>");
268
var subtree = content.createFromXaml ("<Canvas xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='Subtree'><Rectangle x:Name='Rect2'/></Canvas>");
270
tree.children.add (subtree);
272
tree.children.clear ();
274
var new_child = content.createFromXaml ("<Canvas xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='Subtree' />");
276
var exception = null;
278
subtree.children.add (new_child);
284
if (exception != null)
285
Log ("red", "adding conflicting child to subtree without namescope raised exception");
287
Log ("green", "adding conflicting child to subtree without namescope didn't raise exception");
291
tree.children.add (subtree);
297
if (exception != null)
298
Log ("green", "adding subtree with conflicting child name back to root tree raised exception");
300
Log ("red", "adding subtree with conflicting child name back to root tree didn't raise exception");
303
function OnLoaded (sender, eventArgs)
305
control = document.getElementById ("agControl");
306
content = control.content;
308
logger = sender.findName ("Log");
310
Log ("black", "test1");
312
Log ("black", "test2");
314
Log ("black", "test3");
316
Log ("black", "test4");
318
Log ("black", "test5");
320
Log ("black", "test6");
322
Log ("black", "test7");
324
Log ("black", "test8");
326
Log ("black", "test9");
328
Log ("black", "test10");
333
<object type="application/x-silverlight" data="data:," id="agControl" width="100%" height="100%">
334
<param name="background" value="#FFFFFF"/>
335
<param name="source" value="#xamlContent"/>
338
<script type="text/xaml" id="xamlContent">
339
<?xml version="1.0"?>
340
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
341
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
343
<TextBlock FontSize="10" Canvas.Left="5" Canvas.Top="25" x:Name="Log"/>