元素已经是另一个元素的子元素

发布于 2024-08-27 11:32:33 字数 1242 浏览 6 评论 0 原文

我的 Silverlight 应用程序中出现以下错误。但我无法弄清楚问题出在哪个控制上。如果我调试它不会破坏代码中的任何内容,它只会在仅包含框架代码的框架调用堆栈中失败。在这种情况下,有什么方法可以获取有关 Silverlight 应用程序的哪个部分出现问题的更多信息。

Message: Sys.InvalidOperationException: ManagedRuntimeError error #4004 in control    'Xaml1': System.InvalidOperationException: Element is already the child of another element.
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 System.Windows.Controls.AutoCompleteBox.OnApplyTemplate()
at System.Windows.FrameworkElement.OnApplyTemplate(IntPtr nativeTarget)

上下文中 AutoCompeletBox 的 XAML 为:

<tk:AutoCompleteBox 
    x:Name="acName" 
    Grid.Row="0" 
    Grid.Column="1" 
    LostFocus="acName_LostFocus"
    Height="20"
    Width="80" 
    HorizontalAlignment="Left"/>

I get the folowing error in my Silverlight application. But i cant figure out what control it is that is the problem. If i debug it don't break on anything in the code, it just fails in this framework callstack with only framework code. Is there any way to get more information on what part of a Silverlight app that is the problem in this case.

Message: Sys.InvalidOperationException: ManagedRuntimeError error #4004 in control    'Xaml1': System.InvalidOperationException: Element is already the child of another element.
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 System.Windows.Controls.AutoCompleteBox.OnApplyTemplate()
at System.Windows.FrameworkElement.OnApplyTemplate(IntPtr nativeTarget)

The XAML for the AutoCompeletBox that is in the context is:

<tk:AutoCompleteBox 
    x:Name="acName" 
    Grid.Row="0" 
    Grid.Column="1" 
    LostFocus="acName_LostFocus"
    Height="20"
    Width="80" 
    HorizontalAlignment="Left"/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

安穩 2024-09-03 11:32:34

该错误是一个通用的包罗万象的异常,有很多原因。我编写了一个调试器实用程序,可以帮助确定 XAML 的哪一部分实际上导致了错误。您可以从我的博客下载它: http://whydoidoit.com/2010/08/30/debug-xaml-element-is-already-the-child-of-another-element/

The error is a generic catch-all exception that has many many causes. I've written a debugger utility that can help to identify which part of the XAML is actually causing the error. You can download it from my blog: http://whydoidoit.com/2010/08/30/debug-xaml-element-is-already-the-child-of-another-element/

同展鸳鸯锦 2024-09-03 11:32:34

您的项目可能是视觉元素,而不是数据对象。

如果您提供 XAML,我可以帮助确保情况确实如此。

Your items may be visual elements, instead of data objects.

If you provide the XAML, I can help make sure that is the case.

新雨望断虹 2024-09-03 11:32:34

通常,当所述元素已经附加到现有父级并且您在代码中的某个位置尝试重新设置它的父级时,就会发生此错误(即,当您必须首先从父级中删除子级时,只需直接“添加”,然后将其添加到孩子等)。

具体来说,如果控制失败,上述信息不足以消化。

Usually this error occurs when the said Element is already attached to an existing Parent and somewhere in your code you're attempting to re-parent it (ie via just straight "add" when you in turn must remove the child from the parent first, then Add it to the children etc).

Where specifically the control is failing the above isn't enough info to digest.

轮廓§ 2024-09-03 11:32:34

简单而愚蠢的解决方案:

public class AutoCompleteTextBox : AutoCompleteBox
    {
        public override void OnApplyTemplate()
        {
            try
            {
                base.OnApplyTemplate();
            }
            catch { }
        }
    }

Simple and stupid solution:

public class AutoCompleteTextBox : AutoCompleteBox
    {
        public override void OnApplyTemplate()
        {
            try
            {
                base.OnApplyTemplate();
            }
            catch { }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文