[英]ArgumentException when adding UserControl to StackPanel
當程序嘗試在DoSend方法中將WordBlock(這是我擴展UserControl的類)添加到StackPanel的內容時,有時(實際上非常常見,尤其是當查詢返回多個結果時)拋出ArgumentException,這肯定與它有關線程化(SearchThreadEngine是在第二個線程中運行的方法),但是我在主題上很虛弱,也不知道為什么會發生這種情況。 因此,我將很樂意接受任何幫助。 這是堆棧跟蹤:
{System.ArgumentException: Value does not fall within the expected range.
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.Collection_AddValue[T](PresentationFrameworkCollection`1 collection, CValue value)
at MS.Internal.XcpImports.Collection_AddDependencyObject[T](PresentationFrameworkCollection`1 collection, DependencyObject value)
at System.Windows.PresentationFrameworkCollection`1.AddDependencyObject(DependencyObject value)
at System.Windows.Controls.UIElementCollection.AddInternal(UIElement value)
at System.Windows.PresentationFrameworkCollection`1.Add(T value)
at Dictionary.MainPage.DoSend(IQueryable`1 words, WordContext context)} System.Exception {System.ArgumentException}
public void DoSend(IQueryable<Word> words, WordContext context)
{
Result.Children.Clear();
using (context)
{
foreach (Word word in words)
{
Result.Children.Add(new WordBlock(word));
}
waitHandle.Set();
}
}
public void SearchThreadEngine()
{
while (!abort)
{
if (ToSearch != "")
{
string toSearch = ToSearch;
Thread.Sleep(200);
if (toSearch != ToSearch)
continue;
WordContext wc = new WordContext(WordContext.connectionString);
ToSearch = "";
IQueryable<Word> result = (from w in wc.Words where w.Foreign.Equals(toSearch) || w.Foreign.StartsWith(toSearch+" ") select w);
if(result.Count() == 0)
result = (from w in wc.Words where w.Foreign.Equals("to "+toSearch) || w.Foreign.StartsWith("to "+toSearch + " ") select w);
if (result.Count() != 0)
{
Result.Dispatcher.BeginInvoke(new SendResult(DoSend), new Object[] { result, wc });
waitHandle.WaitOne();
}
}
}
abort = false;
}
Result.Children.Add(new WordBlock(word))
如果該控件same name as another
控件具有same name as another
這將是您收到的錯誤。 每個成員必須具有唯一的名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.