如何访问WindowsPhone页面的子项?

时间:2015-02-16 10:43:28

标签: c# windows-phone-8

我需要获取当前页面及其边界框的子项。我可以得到这样一个页面:

var currentPage = ((PhoneApplicationFrame)Application.Current.RootVisual).Content as PhoneApplicationPage;

但是我没有看到如何访问页面的子项。我走错了路吗?怎么做得好?

注意:我正在尝试在lib类中进行。

1 个答案:

答案 0 :(得分:0)

我这样处理 -

        var currentPage = ((PhoneApplicationFrame)Application.Current.RootVisual).Content as PhoneApplicationPage;
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(currentPage); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(currentPage, i);

            if (child is System.Windows.Controls.Control)
            {
                // do work
            }
            else if (child is System.Windows.FrameworkElement)
            {
                // do work
            }
            if (VisualTreeHelper.GetChildrenCount(child) > 0)
            {
                enumChildren(child); // recur. enumerate children
            }
        }