Bonjour,
J'essai de parcourir un arbre XAML en profondeur afin d'associer à chaque élément trouvé une action spécifique: si le noeud est un bouton, associer actionBouton, si le noeud est un textField, associer actionTextfield, etc...
Le problème est que mon parcours ne fonctionne pas du tout...
Ma Méthode qui me permet de charger le fichier XAML et de créer l'arbre associé. Je pars du noeud racine et parcours recursivement chaque enfant. Pour le moment je n'affiche que le type du noeud en cours:
Et voici ma méthode de parcours qui affiche le type du noeud en cours:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 public void LoadXAMLMethod() { try { StreamReader mysr = new StreamReader("Page1.xaml"); DependencyObject rootObject = XamlReader.Load(mysr.BaseStream) as DependencyObject; System.Collections.IEnumerable RootChildren = null; RootChildren = LogicalTreeHelper.GetChildren(rootObject); foreach (DependencyObject obj in RootChildren) { System.Diagnostics.Debug.WriteLine(obj.DependencyObjectType.Name.ToString()); BrowseTree(obj, RootChildren, 0); } this.Content = rootObject; } catch (FileNotFoundException ex) { MessageBox.Show(ex.Message.ToString()); } }
Le fichier XAML de test:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 public void BrowseTree(DependencyObject RootObject, System.Collections.IEnumerable RootChildren, int niveau) { RootChildren = LogicalTreeHelper.GetChildren(RootObject); try { System.Diagnostics.Debug.WriteLine("niveau: " + niveau); foreach (DependencyObject obj in RootChildren) { System.Diagnostics.Debug.WriteLine(obj.DependencyObjectType.Name.ToString()); niveau = niveau + 1; BrowseTree(obj, RootChildren, niveau); return; } return; } catch (InvalidCastException e) { System.Diagnostics.Debug.WriteLine(e.Message); return; } }
En sortie j'obtiens:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration" > <Grid> <Grid.Background> <LinearGradientBrush StartPoint="0,0" EndPoint="0.2,1"> <GradientStop Color="#FF0000" Offset="0" /> <GradientStop Color="#FFFFFF" Offset="1.0" /> </LinearGradientBrush> </Grid.Background> <TabControl TabStripPlacement="Top" Margin="15, 15, 15, 15"> <TabItem Name="Tab1" Header="Page 1"> <TabItem.Content> <Canvas> <Border Canvas.Top="5" Canvas.Left="5" BorderBrush="Black" BorderThickness="2"> <WrapPanel Name="XamlWP" HorizontalAlignment="Left" Width="100" Background="#FFFFFF"> <Label> LC Module : </Label> <TextBox BorderThickness="1" Name="TextBox2" Width="100" Height="20" > </TextBox> <TextBox BorderThickness="1" Name="TextBox3" Width="100" Height="20"> </TextBox> </WrapPanel> </Border> <Button Canvas.Top="100" Canvas.Left="15" Name="SaveButton" Width="60" Height="20"> Save </Button> </Canvas> </TabItem.Content> </TabItem> <TabItem Name="Tab2" Header="Page 2"> </TabItem> </TabControl> </Grid> </Page>
Grid
niveau: 0
TabControl
niveau: 1
TabItem
niveau: 2
Une exception de première chance de type 'System.InvalidCastException' s'est produite dans LoadXAMLdynamically.exe
Unable to cast object of type 'System.String' to type 'System.Windows.DependencyObject'.
Le "DependencyObject obj" de ma méthode BrowseTree est null au moment de l'exception.
Il doit probablement s'agir de quelque de stupide, mais je ne vois pas du tout :S
Cordialement,
Sacoum.
Partager