0

当程序试图在 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;
}
4

1 回答 1

0
Result.Children.Add(new WordBlock(word))

如果控件有same name as another这就是您将收到的错误。每个成员必须有一个唯一的名称。

于 2013-09-14T23:39:57.927 回答