银光: "The name already exists in the tree"

标签 silverlight xaml user-controls tooltip

这是我编写 Silverlight XAML 时经常出现的问题。在这种情况下,我制作了一个附加了 ToolTip 的用户控件 VerticalTabStop(附加代码)。我实例化了几个我的用户控件,然后我得到了调试窗口和以下错误:

Line:52
Error: Unhandled Error in Silverlight 2 Application
Code: 2028
Category: ParserError
Message: The name already exists in the tree: AltLabel.
File:
Line: 0
Position: 0

当我将鼠标悬停在按钮上时,我收到了很多这样的消息。对我在这里做错的任何建议?

干杯
Nik


<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    x:Class="SLEntityPresenterWebPart.VerticalTabStop"
    d:DesignWidth="20" d:DesignHeight="27">

    <Grid x:Name="LayoutRoot">
        <StackPanel>
            <Canvas x:Name="TabStopCanvas" Height="27" Width="20">
                <ToolTipService.ToolTip>
                    <TextBlock x:Name="AltLabel" Text="Substitute me"/>
                </ToolTipService.ToolTip>
                <Image x:Name="IconImg" Canvas.Left="7" Canvas.Top="9" Width="26" Height="26" Source="Contact.png" Canvas.ZIndex="5" Margin="0,-9,0,0" RenderTransformOrigin="0.5,0.5">
                    <Image.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform ScaleX="0.85" ScaleY="0.85"/>
                            <SkewTransform/>
                            <RotateTransform/>
                            <TranslateTransform X="0"/>
                        </TransformGroup>
                    </Image.RenderTransform>
                </Image>
                <Image Source="stop.png" Margin="3,0,0,0"/>
            </Canvas>

        </StackPanel>
    </Grid>
</UserControl>

最佳答案

即使在 Silverlight 4 中也有非常相似的错误。
如果您创建自定义用户控件,通常:

<UserControl xmlns:MyNameSpace="clr-namespace:MyNameSpace" x:Class="MyNameSpace.MyClass" 
x:Name="userControl" ... />

然后,如果您将 2 个没有名称的控件添加到 xaml 代码(带预览):
<MyNameSpace:MyClass ... />
<MyNameSpace:MyClass ... />

将出现异常“名称已存在于树中:userControl”。发生这种情况是因为 Silverlight 找不到名称(未命名的 [MyClass])并在 UserControl 中查找两次找到“userControl”。

解决方案之一是为控件命名:
<MyNameSpace:MyClass x:Name = "MyControl1" ... />

或者从代码初始化这个控件:
MyClass control = new MyClass();
SomeGrid.Children.Add(control);

关于银光: "The name already exists in the tree",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/605907/

相关文章:

c# - 在类型上找不到匹配的构造函数。您可以使用 Arguments 或 FactoryMethod 指令来构造类型

c# - ASP.Net 以声明方式将值从页面属性传递给用户控件

asp.net - 两个用户控件之间的交互

c# - 无法更改DataGrid中的前景

C# 在不调用 ValueChanged 事件的情况下设置 UserControl.Value

银光/WP7 : programmatically change the button background image

c# - 更新 silverlight 中的属性绑定(bind)

c# - 在 Silverlight 中显示文件类型图标

silverlight - 使用MVVM Light将消息发送到Silverlight中的未构造类

c# - 使用 C# 和 XAML 在 Metro 应用程序中自定义底部应用程序栏