设置文本块的Text属性时出错:在附加到新父Visual之前,必须断开指定子视图与当前父Visual的连接。 [英] Error while setting Text property of a text block : Must disconnect specified child from current parent Visual before attaching to new parent Visual.

查看:1255
本文介绍了设置文本块的Text属性时出错:在附加到新父Visual之前,必须断开指定子视图与当前父Visual的连接。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我运行我的代码时,在指定的操作之后,我会得到一个AgrumentException,引用:

必须在连接到新的父Visual之前断开指定的子视图与当前父视频的连接。



经过大量挖掘后,我缩小到以下结论...

Whenever I run my code, after a specified action, I get an AgrumentException citing:
Must disconnect specified child from current parent Visual before attaching to new parent Visual.

After lot of digging, I narrowed down to the following conclusion...

private void ShowChecksum(string hash) {
Checksum.Text = hash; //if I comment this line... everything is fine.
}



其中Checksum是一个文本块,hash是一个字符串,它由一个方法参数从另一个线程通过Dispatcher传递。如果我评论上面这行,我的代码运行正常。



任何想法?我已经有一段时间了。

如果你需要一些模式代码,我很乐意提供它。



编辑:这是宣言(只有一部分不是全部。)


Where Checksum is a text block and hash is a string that is being passed by a method parameter from another thread through the Dispatcher. If I comment the above line, my code runs fine.

Any Ideas? I''ve been after this for a while.
And If you require some mode code, I will gladly provide it.

Here''s the declaration (only a part not all.)

<StackPanel x:Name="Result" DockPanel.Dock="Top" Width="Auto" Height="Auto" HorizontalAlignment="Left" VerticalAlignment="Top" Visibility="Collapsed" Margin="40">
                <StackPanel Orientation="Horizontal">
                    <Image x:Name="FileIcon" Source="Resources/file.png" Width="48"/>
                    <Label x:Name="FileName" VerticalAlignment="Bottom" FontSize="24"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Label Content="{Binding SelectedItem, ElementName=Algorithm}" FontSize="14" />
                    <TextBlock x:Name="Checksum" FontSize="14" />
                </StackPanel>
                <StackPanel>
                    <Button Content="Save to file" Style="{DynamicResource BlueButton}" Width="auto" Height="25" Margin="30"/>
                </StackPanel>
            </StackPanel>



现在这种情况有点奇怪,因为当我们尝试将元素添加到任何其他容器时,通常会发生这种异常。



在此先感谢,

Amitosh


Now this is kinda weird error in this situation, coz this exception is generally happens when we try to add an element to any other container.

Thanks in Advance,
Amitosh

推荐答案

这是WPF的一个特殊功能:它保护开发人员免受某些情况的影响操作尝试打破基于它们之间的父子关系的 System.Windows.Media.Visual 实例的层次结构。 层次结构表示关系形成树。通常,当您尝试插入一些 Visual 的实例(通常是一些 UIElement ,例如在某个容器中控制 Visual< code>(通常是< code> System.Windows.Media.ContainerVisual ),如果此实例已经是某个其他实例的子实例。它可能是 VisualCollection 的元素 ContainerVisual 内容



不幸的是,您提供的代码没有显示此问题,但我希望当你理解这个想法时,你可以找到结束。基本上,你需要理解内容模型和WPF逻辑和可视树(与当前问题相关,只是视觉树)。请参阅:

http://msdn.microsoft.com/en-us/library/bb613548 .aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/ms753391.aspx [ ^ ]。



此CodeProject文章也很有用:了解WPF中的可视树和逻辑树 [ ^ ]。< br $>


参见:

http://msdn.microsoft.com/en-us/library/system.windows.media.visual.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.media。 containervisual.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.media.containervisual.children.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.windows.media.visualcollection.aspx [ ^ ]。



基本上,你应该在元素中找到一个地方,你插入一些 Visual 实例是一个不同的地方作为一个孩子。



-SA
This is a special feature of WPF: it protects the developer from the cases when some operations tries to break the hierarchy of System.Windows.Media.Visual instances based in parent-child relationship between them. "Hierarchy" means that the relationship forms a tree. Usually, the problem happens when you try to insert some instance of Visual (typically, some UIElement, such as Control in some container Visual<code> (typically, one of the derived classed of <code>System.Windows.Media.ContainerVisual), if this instance was already a child of some other instance. It could be an element of VisualCollection property of ContainerVisual or a Content.

Unfortunately, the code you presented does not show this problem, but I hope you can find end when you understand the idea. Basically, you need to understand the content model and WPF logical and visual trees (in relation to the current problem, just the visual tree). Please see:
http://msdn.microsoft.com/en-us/library/bb613548.aspx[^],
http://msdn.microsoft.com/en-us/library/ms753391.aspx[^].

This CodeProject article can also be useful: Understanding the Visual Tree and Logical Tree in WPF[^].

See also:
http://msdn.microsoft.com/en-us/library/system.windows.media.visual.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.media.containervisual.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.media.containervisual.children.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.media.visualcollection.aspx[^].

Basically, you should locate a place in your element where you insert some Visual instance is some different places as a child.

—SA


哦!在另一个不相关的组件中设置绑定时出现了一个小的愚蠢错误。绑定成功解决但导致后续调用View以更新任何组件时出现问题。
Oh! There was a small silly error in setting the binding in another, unrelated component. The binding resolved successfully but caused problems with subsequent calls to the View for updating any component.


这篇关于设置文本块的Text属性时出错:在附加到新父Visual之前,必须断开指定子视图与当前父Visual的连接。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆