VisualTreeHelper.GetChildrenCount在从MainPage_Loaded运行时返回0
问题描述:
在我的WP7应用程序中,我使用这个家伙方法在ToggleButton
内找到TextBlock
。 https://stackoverflow.com/a/1759923/1339857VisualTreeHelper.GetChildrenCount在从MainPage_Loaded运行时返回0
当我在应用程序运行时进行调用时,它工作正常。
如果我尝试从MainPage_Loaded
,FindChild
返回null
完全相同的呼叫。
下面是简单的通话
TextBlock myText = FindChild<TextBlock>(myToggle, "toggleTitle");
myText.Text = "Some text";
看起来它是因为VisualTreeHelper.GetChildrenCount
将返回0。
为什么它会有一个值,当应用程序正在运行,但不会从MainPage_Loaded
?是否MainPage_Loaded
的目的是在应用程序加载之前等到应用程序加载完成之后才开始执行事件?
感谢
答
一招,你可以用它来对付,这是排队您的Loaded事件的呼叫。因此,在MainPage_Loaded处理程序中打包呼叫Dispatcher.BeginInvoke。
Dispatcher.BeginInvoke(() => {
TextBlock myText = FindChild<TextBlock>(myToggle, "toggleTitle");
myText.Text = "Some text";
}
这将您的呼叫添加到队列和当前事件周期完成后(这应该是当所有的子项已经加载),它会被调用。
这样做的科比科比。谢谢您的帮助。 – MickD 2012-04-24 23:08:39
脏,但作品,谢谢。 – 2013-01-23 12:18:08