0
foreach (UIElement el in GridBoard.Children.ToList())
{
if (el is Ellipse)
{
GridBoard.Children.Remove(el);
}
}
LINQ是否有與上述相同的功能?如果是,請提供代碼?謝謝LINQ刪除SilverLight中的UIElement兒童
foreach (UIElement el in GridBoard.Children.ToList())
{
if (el is Ellipse)
{
GridBoard.Children.Remove(el);
}
}
LINQ是否有與上述相同的功能?如果是,請提供代碼?謝謝LINQ刪除SilverLight中的UIElement兒童
LINQ用於查詢集合而不是導致副作用。根據MSDN Silverlight不支持List<T>
的RemoveAll
方法但支持的Remove
和RemoveAt
方法,否則你就已經可以寫:GridBoard.Children.ToList().RemoveAll(el => el is Ellipse);
你可以使用LINQ如下:
var query = GridBoard.Children.OfType<Ellipse>().ToList();
foreach (var e in query)
{
GridBoard.Children.Remove(e);
}
或者,你可以遍歷列表中的反向和使用RemoveAt
這會產生一些更好的性能,然後使用Remove
:
for (int i = GridBoard.Children.Count - 1; i >= 0; i--)
{
if (GridBoard.Children[i] is Ellipse)
GridBoard.Children.RemoveAt(i);
}
所以它和你所擁有的沒有什麼不同。也許RemoveAll
支持將使它成爲未來Silverlight版本的方式,它將是最好的選擇。
非常感謝你! – TechTravelThink 2010-11-05 07:17:11