当一个程序试图在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;
}
答案 0 :(得分:0)
Result.Children.Add(new WordBlock(word))
如果控件具有same name as another
,则表示您将收到此错误。每个成员必须具有唯一的名称。