2017-02-18 45 views
1

提起我的XAML模板我的MainWindow类:无法访问XAML的模板,通过字段信息

...  
<Button x:Name="button1" Content="Button" Click="button1_Click" /> 
<Label x:Name="superLabel" Content="Super content!" /> 
... 

我想通过访问relfections superLabel领域,点击按钮后,像这样:

public void button1_Click(object sender, RoutedEventArgs e) 
{ 
    Type t = typeof(MainWindow); 
    FieldInfo test1 = t.GetField("superLabel"); // test1 == null 
    FieldInfo test2 = t.GetRuntimeField("superLabel"); // test2 == null 
    ... 
} 

但我在每次测试得到 ...

回答

1

你的标签不是public - 你应该添加BindingFlags作为第二个参数:

FieldInfo test1 = t.GetField("superLabel", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); 
+0

它的作品!谢谢@Roma –

+0

@DariuszFilipiak,不客气 –