0

是否可以在 Windows Phone 应用程序中按名称搜索控件?我知道在用 c# 编写的普通桌面应用程序中是可能的:

string s = "label2";
            Control[] controls = this.Controls.Find(s, false);
            if (controls.Length > 0)
            {
                Label found = controls[0] as Label;
                if (found != null) found.Text = "new label2 text";
            }

在 Windows Phone 应用程序中,“this.Controls”无法识别...

4

2 回答 2

3

使用FindName

var control = this.FindName(s);

if (control != null)
{
    // Whatever
}
于 2013-01-10T20:57:11.927 回答
1

是的,这是可能的——但在 Windows Phone 中,您的控件位于称为可视树的树状结构中。为了找到控件,您必须浏览此树。我不久前写了一个名为Linq-to-VisualTree的实用程序类,它简化了这个过程。

于 2013-01-10T20:57:59.770 回答