Silverlight: "The name already exists in the tree" - silverlight

this is a problem that regularly arises when I write Silverlight XAML. In this case, I've made a usercontrol VerticalTabStop (code attached) that has a ToolTip attached. I instanciate a couple of my usercontrols, and then I get the debugging window and the following error:
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
I get an awful lot of these messages as I hover my mouse over the buttons. Any suggestions to what I'm doing wrong here?
Cheers
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>

There is very similar bug even in Silverlight 4.
If you create custom usercontrol, usually:
<UserControl xmlns:MyNameSpace="clr-namespace:MyNameSpace" x:Class="MyNameSpace.MyClass"
x:Name="userControl" ... />
Then, if you add 2 controls without names to the xaml code (with preview):
<MyNameSpace:MyClass ... />
<MyNameSpace:MyClass ... />
There will be exception "The name already exists in the tree: userControl". It occurs because Silverlight can't find the name (unnamed [MyClass]) and looks to the UserControl where it finds "userControl" twice.
One of the solution is to give some names to the controls:
<MyNameSpace:MyClass x:Name = "MyControl1" ... />
Or initialize this control from code:
MyClass control = new MyClass();
SomeGrid.Children.Add(control);

This is a bug in Silvelight. The way to work around it is to remove the Name attribute on the TextBlock in the Tooltip.
I presume that you have the name there for a reason, and that not being able to refer to this element from code is going to be a problem for you. As a work around for that, try replacing the tooltip xaml with this:
<ToolTipService.ToolTip>
<ToolTip x:Name="AltLabel" Content="Substitute me" />
</ToolTipService.ToolTip>
Now you can get to the text by doing AltLabel.Content.
If this does not solve your problem, please let me know.

I was struggling with the same message yesterday...
ParserError - The name already exists in the tree: blah
In my case the problem was that somehow a reference was added... to itself. (The DLL of the project in the projects own bin/debug folder). Removing this reference sorted out the problem.
Seems that this error message is too vague.

Try to remove any name like ' x:Name="TabStopCanvas" ' in stack panel, it worked for me.

Related

Defining CustomMessageBox from Windows Phone Toolkit in XAML

Can I define CustomMessageBox in XAML? I have the code:
<phone:phone:PhoneApplicationPage.Resources>
<toolkit:CustomMessageBox x:Key="CustomMessageBox" Title="Blabla" IsLeftButtonEnabled="True" LeftButtonContent="OK" Content="blabla" />
</phone:PhoneApplicationPage.Resources>
And when I'm trying to run it:
(this.Resources["CustomMessageBox"] as CustomMessageBox).Show();
I get InvalidOperationException - "Element is already the child of another element.".
Is it possible to do it this way or I have to define it from code-behind? Is there any workaround?
Due to this book (not advertizing, just showing source), you can do follow:
1 Create new UserControl, name it CustomUserControl.xaml
2 Add custom UI elements to UC
<StackPanel x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}" Margin="0,0,0,10">
<TextBlock Text="Custom content inside the UserControl XAML" TextWrapping="Wrap"
HorizontalAlignment="Left"/>
<MediaElement Source="http://mschannel9.vo.msecnd.net/o9/mix/09/wmv/key01.wmv"/>
</StackPanel>
3 Run from codebehind of your main page
var messageBox = new CustomMessageBox
{
Content = new CustomUserControl(),
LeftButtonContent = "OK",
RightButtonContent = "Cancel",
};
messageBox.Show();
Seems now you can play video in message box :)
PS: also found some info here

Canvas RenderTransform error "Cannot convert" in VisualStudio 2010 SilverLight 4

<Canvas Width="945" Height="718" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Canvas RenderTransform="1.333333333,0,0,1.333333333,0,0" />
</Canvas>
This works fine in WPF but the following error in SilverLight
Error 3 Cannot convert "1.333333333,0,0,1.333333333,0,0".
Looks like a MatrixTransform to me, create it explicitly using element syntax as there does not appear to be a type converter.
<Canvas.RenderTransform>
<MatrixTransform>
<MatrixTransform.Matrix>
<!-- I do not know the WPF parsing order
so you'll need to find out what goes where yourself -->
<Matrix OffsetX="???" OffsetY="???"
M11="???" M12="???"
M21="???" M22="???" />
</MatrixTransform.Matrix>
</MatrixTransform>
</Canvas.RenderTransform>
Testing suggest that this is the respective Matrix:
<Matrix M11="1.333333333" M22="1.333333333"/>

PlaneProjection is not working well in silverlight

in silverlight project using name attribute in planeprojection gives Error 1 The type or namespace name 'PlaneProjection' could not be found (are you missing a using directive or an assembly reference?)
code i used for that
<Image Name="blabla.jpg" Height="200" Width="200" >
<Image.Projection>
<PlaneProjection Name="pp" />
</Image.Projection>
</Image>
It is a strange error in that it appears to be complaining about the PlaneProjection itself but that could be a red-herring.
The xaml is not correct because the PlaneProjection does not have a Name property. However you should be able to x:Name. My guess is you are trying to ensure you have a field called pp that you can manipulator in code. You can now use pp as a field in code or find the PlaneProjection using FindName on the control.
This is what I think your Xaml should look like:-
<Image x:Name="MyImage" Source="blabla.jpg" Height="200" Width="200" >
<Image.Projection>
<PlaneProjection x:Name="pp" />
</Image.Projection>
</Image>
TBH, I would simply access the projection as MyImage.Projection rather than add an x:Name to it.

How to update Dynamic Resource within a Dynamic Resource?

I have a visual brush which is a group of shapes, the main colour of which is a dynamic resource itself - so the shape is for example MyShape and the Colour, MyColour which is referenced by the Shape object.
My problem is when I update the colour for this - it only happens the first time the shape is loaded (the colour needs to be set first) however as much as I change the colour it won't update the dynamic resource that uses the colour - how do I make this work?
Just need to make a dynamic resource work within another dynamic resource and have them both update when I change the colour.
I have no idea how to get this to work - I spent time creating a colour-picker for WPF only to find I cannot change the colour of this item - 1-Tier resources work where I set the brush/colour directly but not a colour within another object or 2-Tier Resource.
Edit: My problem seems to be specific to using these in a seperate Resource / Dictionary as my program needs to access this item from a class not the Window, the main example mentioned does not work when the MyColor is in a seperate Resource.
Unless I misunderstand the situation, exactly what you're talking about works pretty well. I just tried it out with this Xaml:
<Window x:Class="ConditionalTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<SolidColorBrush x:Key="MyColor" Color="Aqua" />
<VisualBrush x:Key="MyBrush">
<VisualBrush.Visual>
<Ellipse Height="50" Width="100" Fill="{DynamicResource MyColor}" />
</VisualBrush.Visual>
</VisualBrush>
</Window.Resources>
<Grid Background="{DynamicResource MyBrush}">
<Button Height="30" Width="Auto" VerticalAlignment="Center" HorizontalAlignment="Center" Content="ChangeColor" Click="Button_Click" />
</Grid>
</Window>
And then changed the color in the click handler for that button:
private void Button_Click(object sender, RoutedEventArgs e)
{
((SolidColorBrush)Resources["MyColor"]).Color = Colors.Purple;
}
And it worked like a champ.
Can you post an example of how you are attempting to change the color in the resource dictionary?
When I make a sample app and try to change the resource value it appears that the SolidColorBrush in the resource dictionary has been frozen so it can't be modified. To get around this I just set the new value to a new SolidColorBrush.

Reuse of StaticResource in Silverlight 2.0

I am currently testing with Silverlight 2.0 Beta 2, and my goal is to define a resource element once and then reuse it many times in my rendering. This simple example defines a rectangle (myRect) as a resource and then I attempt to reuse it twice -- which fails with the error:
Attribute {StaticResource myRect} value is out of range. [Line: 9 Position: 83]
BTW, this sample works fine in WPF.
<UserControl x:Class="ReuseResourceTest.Page"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="200" Height="200">
<Canvas x:Name="LayoutRoot" Background="Yellow">
<Canvas.Resources>
<RectangleGeometry x:Key="myRect" Rect="25,50,25,50" />
</Canvas.Resources>
<Path Stroke="Black" StrokeThickness="10" Data="{StaticResource myRect}" />
<Path Stroke="White" StrokeThickness="4" Data="{StaticResource myRect}" />
</Canvas>
</UserControl>
Any thoughts on what's up here.
Thanks,
-- Ed
I have also encountered the same problem when trying to reuse components defined as static resources. The workaround I have found is not declaring the controls as resources, but defining styles setting all the properties you need, and instantiating a new control with that style every time you need.
EDIT: The out of range exception you are getting happens when you assign a control to a container that already is inside another container. It also happens in many other scenarios (such as applying a style to an object that already has one), but I believe this is your case.

Resources