下面的代碼給了我以下錯誤:
CS1061 UIElementCollection 不包含 Where 的定義,并且沒有可訪問的擴展方法 Where 可以找到接受 UIElementCollection 型別的第一個引數(您是否缺少 using 指令或程式集參考?)
var object = main.Children.Where(c => "platform1".Equals(c.Tag)).First();
main.Children.Remove(object);
如何讓它作業?
uj5u.com熱心網友回復:
Children屬性型別UIElementCollection不實作泛型介面,因此IEnumerable<T>您不能將其用作Enumerable擴展方法的來源,例如Where.
您必須添加一個型別轉換方法,例如
var obj = main.Children.Cast<UIElement>().Where(...);
由于您還想訪問子類的Tag屬性,請FrameworkElement改用以下內容:
var obj = main.Children
.OfType<FrameworkElement>()
.Where(c => "platform1".Equals(c.Tag))
.First();
或更短:
var obj = main.Children
.OfType<FrameworkElement>()
.First(c => "platform1".Equals(c.Tag));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/482436.html
