Xaml parser exception - wpf

This is something that's driving me crazy.. I have a WPF project which is already in production. Now, I have to make some fix for which I have to set x:Name property to couple of controls. But, when I set x:Name property for any control, it's throwing XamlParser exception (inner exception says cannot cast type XXX to type Button where XXX is type of control for which I am setting x:Name property).
It also shows an additional error like,
Additional information: Set connectionId threw an exception.
I have closed and reopened Visual Studio; I have cleaned the solution and rebuilt many times; but no use.
Can anybody let mw know what's going on?
Here is the code - I have just added x:Name = "PnlUpDown" to stack panel.
<StackPanel Name="PnlUpDown" VerticalAlignment="Center" Grid.Column="3">
<Button x:Name="BtnMoveUp" Padding="3,5" Margin="5,0,0,3" Tag="MOVEUP" >
<Polygon Points="0,15 5,0,10,15" Fill="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType=Button}}"/>
</Button>
<Button x:Name="BtnMoveDown" Padding="3,5" Margin="5,3,0,0" Tag="MOVEDOWN" >
<Polygon Points="0,0 5,15,10,0" Fill="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType=Button}}"/>
</Button>
</StackPanel>
Now.. I find that not only x:Name property.. It's not allowing me to set any other property to any control - In general, if I modify my XAML it's throwing error..!!!!!!! For everything it's throwing Set Connection Id error :(

First, check out this post: How do you debug a XamlParseException?. Specifically, look at the call stack as noted in the answer by Igor Zevaka.
Also check the answers to the following questions:
XamlParseException after deploying WPF project
xamlParseException on window launch
XamlParseException can be one of the more difficult exceptions to find. If none of those point you in the right direction, look at the code-behind and the data context. Perhaps there is an exception being thrown in one of those locations.

I don't know how it's related but I just restarted my machine and the issue got resolved !!!! May be Visual Studio 2013 gets refreshed when the machine reboots.. I don't know. Anyways, Thanks everyone for your replies.

Related

How to debug a "Specified element is already the logical child of another element" in the release

I know I'm supposed to post a very specific question with a code sample, but I wouldn't know where to start with this:
While running my app in Visual Studio it is ok, both in Debug and in Release modes. But when I create an InstallShield installer, in other computers I get an Exception:
System.Windows.Threading.Dispatcher WPF Specified element is already the logical child of another element. Disconnect it first
I've seen tons of posts regarding this issue in StackOverflow and in other webs, but there is no reference o this happening only in the installed package.
My question is double (sorry for that too):
How could I possibly debug such an issue? I mean, specifically if possible (not a generic runtime debug)
What kind of exception can I catch that gives me more information on the error? I'm displaying a MessageBox that shows a ReflectionTypeLoadException. I'd love to see which element is the one with the "double parent" or "double child".
To complicate things further, the app runs fine in the computers I use for development, but it crashes with this exception in others (say, my father's and sister's computers).
ANY clue will be appreciated and duely upvoted. Thanks
-----------------------------------EDIT-------------------------------------
I managed to corner the troublesome child to the TextBlock named "textAvisosSeccion" inside the ListView's ItemTemplate:
<ListView ItemsSource="{Binding Secciones}" Background="{Binding MiColor}"
Style="{StaticResource NavigationMenuStyle}"
IsSynchronizedWithCurrentItem="True"
SelectedItem="{Binding SeccionActiva}"
Margin="0" MinWidth="{Binding RelativeSource={RelativeSource
AncestorType=ListBoxItem}, Path=ActualWidth}" HorizontalContentAlignment="Stretch">
<ListView.ItemTemplate>
<DataTemplate>
<DockPanel LastChildFill="True" HorizontalAlignment="Stretch">
<Grid DockPanel.Dock="Right" Margin="2,2,8,2"
Visibility="{Binding ElementName=textAvisosSeccion, Path=Visibility, FallbackValue=Collapsed}">
<Ellipse Height="25" Width="25" VerticalAlignment="Center"
Stroke="WhiteSmoke" StrokeThickness="1" Fill="OrangeRed"/>
<TextBlock x:Name="textAvisosSeccion" FontSize="12"
FontWeight="Medium" Foreground="WhiteSmoke"
VerticalAlignment="Center" HorizontalAlignment="Center" TextAlignment="Center" Style="{StaticResource TextBlockNullZeroCollapsedStyle}"
Text="{Binding Value.Avisos}"/>
</Grid>
<TextBlock DockPanel.Dock="Left" Text="{Binding Metadata.NombreTraducido}" Margin="8,2" VerticalAlignment="Center" HorizontalAlignment="Stretch" TextAlignment="Center"/>
</DockPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Style x:Key="TextBlockNullZeroCollapsedStyle" TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="Text" Value="">
<Setter Property="Visibility" Value="Collapsed"/>
</Trigger>
<Trigger Property="Text" Value="0">
<Setter Property="Visibility" Value="Collapsed"/>
</Trigger>
</Style.Triggers>
</Style>
Check out the binding Text="{Binding Value.Avisos}". The ListView is bound to a collection of Lazy<T> imported with MEF, in which T has the "Avisos" property. The other TexkBlock is binding to the Lazy<T>'s Metadata and it's fine.
Besides the reasons why I am binding to a Lazy's value (which instantiates the object, losing the lazy's advantage), why am I getting a "Specified element is already the logical child of another element" exception because of this?!?!?
The "Avisos" property is just an integer inside a ViewModel (which is not inheriting from UIElement).
EDIT #2:
Exceptions like this one and others (problems in styles, MEF composition issues, etc.) were not being thrown in VS2012 because it that computer the .NET Framework 4.5 was installed. Even when all the projects targeted Framework 4.0.
If you are developing for 4.0 get an old machine with Vista, make sure you have 4.0 installed and use VS2010; it's the only way to make sure.
This is not a real answer to the question, but the solution I found to the problem: install .NET Framework 4.5 in the client machine, and the app runs like a charm.
After removing the troublesome TextBlock, the application opened but threw the same exception in a lot of places, but with no apparent reason: some ViewModels and Views worked fine, others did not. Of course, they are all built in the same way, so no explanation there.
I found this SO question referring this issue, but with no good answer:
Did WPF 4.5 parent-child behavior change: we can now add a child to another parent without exception?
For this guy it created a problem instead of solving it. A scary question arises though: how can we know if in the next Framework version everything will work the same? Maybe this was a bug in 4.0 that was fixed in 4.5, and they don't want to talk about it.
Another major issue was that the exception gives no real information about which elements have the problem, so it makes it almost useless to debug. I found in some forum that this was posted to Microsoft as an improvement suggestion and they repeatedly rejected it.
Hope this helps someone, and I also hope that someone tells me how to dig deeper into the exepction to know which element is complainig.
Copying EDIT #2 here too:
Exceptions like this one and others (problems in styles, MEF composition issues, etc.) were not being thrown in VS2012 because it that computer the .NET Framework 4.5 was installed. Even when all the projects targeted Framework 4.0.
If you are developing for 4.0 get an old machine with Vista, make sure you have 4.0 installed and use VS2010; it's the only way to make sure.

Bound Settings works in Visual Designer but I can't run the app

I am using Visual C# Studio 2010 Express and I have oakward problem. I wanted to bind few TextBoxes to application Settings. I've followed Paul Sheriff's solution Paul Sheriff's solution. It works only in visual designer - I can't run the app, cause i got:
XmlParseException 'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.
How is it possible, that it's working in visual designer (values are presented in TextBoxes correctly) but there is a XmlParseException?
App.xaml:
<Application.Resources>
<ObjectDataProvider x:Key="odpSettings" ObjectType="{x:Type setts:Settings}" />
</Application.Resources>`
The setts namespace is defined as xmlns:setts="clr-namespace:DbCopier.Properties"
My view:
<TextBox
Text="{Binding Source={StaticResource odpSettings}, Path=Default.DbName}"
Width="150" />
I got it!
The InnerException was pretty damn confusing. It's all about the Property Scope in Settings file. I've noticed it was changed to Application, so it was read-only. I just changed it to User so it became writable. So easy and yet - so annoying mistake.

Silverlight 4: "Invalid XAML" using Converter in DataTemplate

maybe you could help me understand why I get an unhandled exception "Invalid XAML" in Visual Studio 2010 designer when trying to do the following on a Page.
I have a Converter named DateTimeConverter that converts a date into a German date string. The converter works fine. I've included the namespace and added the following to the page's resources:
<navigation:Page.Resources>
<myClasses:DateTimeConverter x:Key="dateTime" />
</navigation:Page.Resources>
Now I have a list box that I want to bind to a list of objects. I do the binding in code, but I would like to define the data template. Thus I've added the following to my layout:
<ListBox x:Name="lbConversation" BorderBrush="#00000000">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="0" Padding="4">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Message, Mode=OneWay}" />
<TextBlock Text="{Binding TimeStamp, Mode=OneWay, Converter={StaticResource dateTime}}" />
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And this works fine when I run. However, in the code section, the code for the data template is undercurled and the designer says "Invalid XAML". When I remove the Converter=... part, this error is gone.
Is this a designer bug? What can I do?
EDIT
By the way: The exact same code does not throw the error within a WPF project!
Just adding to this question as I found a solution.
The solution to my case was here: http://forums.silverlight.net/post/618518.aspx
Apparently you must not have a space character in your project name or assembly name. D'oh!
Hope it helps.
Sorry, can't replicate this at all, do you have some design-time data that may be the cause of the weird error?
Also, since you said that you're using the converter to output german dates... wouldn't it be easier to let the framework do this kind of things, as it probably does them a lot better? Set the entire application thread's CultureInfo to german and all formatting will be done with that culture's settings; of course it's still possible you only want some controls internationalized...
I can't see anything wrong with your Xaml however I wonder if this is a result of the language setting used when the Xaml is parsed. By default Xaml is parsed using the InvariantCulture however it would appear that the designer in visual studio parses the Xaml using the current culture. Hence at times you can get unexpected differences in behaviour at design time than you do at runtime.
In fact if you do this in the constructor of your UserControl before calling InitializeComponent:-
this.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);
You might not need your converter at all.
I came across "Invalid XAML" error when I had my converter marked internal. Change the modifier to public and everything is as expected.
Hope this help.

Binding to images

I'm using a listbox with a template like the following.
<ListBox.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}" Width="100" />
</DataTemplate>
</ListBox.ItemTemplate>
I bind this to an observable collection conataining 130 paths to images (all do exist, read out using System.IO.Directory) but only a few get really displayed. No exceptions that come up. Only white places where the images should be. Has anybody seen something else.
Sascha
Never seen this before but Binding is suppressing errors by default. When there are errors you can see them when you run in debug en watch in the output window of visual studio and make sure the option of the combobox "Show output from:" is set to "Debug"
Hope that this will help you to the real problem.
EDITED:
If you don't see a problem may you can find it out by hooking into the Image.ImageFailed Event. More help can be found here :
http://msdn.microsoft.com/en-us/library/system.windows.controls.image.imagefailed%28v=VS.95%29.aspx

wpf visual studio designer won't load user controls

The view below is a container for three user controls, and I started getting this error after refactoring application resources:
Error 295 Could not create an instance of type 'FilterPanel'. C:...\ProjectPickerWindow.xaml
Here is the xaml for the view:
<Window x:Class="Smack.ConstructionAdmin.WpfPresentation.Views.ProjectPicker.ProjectPickerWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local ="clr-namespace:Smack.ConstructionAdmin.WpfPresentation.Views.ProjectPicker"
xmlns:res="clr-namespace:Smack.ConstructionAdmin.WpfPresentation"
Background="{DynamicResource {x:Static res:SmackResources.WaveWindowBackground}}"
Title="{Binding Path=DisplayName}" FontFamily="Arial" FontSize="12"
SizeToContent="WidthAndHeight" MinWidth="300"
>
<DockPanel LastChildFill="True">
<local:FilterPanel DockPanel.Dock="Top" DataContext="{Binding}" Padding="3" />
<local:StatusAndButtons DockPanel.Dock="Bottom" DataContext="{Binding}" Margin="3, 7" />
<local:Listing DataContext="{Binding}" Margin="3, 0"/>
</DockPanel>
The app runs fine, and I can undo the refactorings, but I would prefer not to. All of the user controls display fine in their designer windows.
Can someone tell me how to get this to display in the designer?
Cheers,
Berryl
=== ADD'L INFO # Andrew ===
Great general checklist, if not the fix yet.
1) no silent binding errors
2) the designer works great after commenting out the FilterPanel!
3) no noticeable behavior change; all tests past too
4) yeah, I may have not left enough bread crumbs to nail the exact refactor but major ones were:
-- put all converters for the presentation in the own ResourceDic
-- had the FilterPanel reference generic.xaml, which has all mergedResourceDics. It referenced a specific Dic called ListingStyles.xaml, which used to have the converters
As an aside, do any tools help find Resource 'problems' (Snoop, Mole?)? Is there anything like FxCop to find bad practices??
Not yet an answer but some suggestions as there is not enough information to go on without knowing more about the app: (Posted as an answer as I thought it too long for a comment).
These designer only issues are a pain to track down as the root cause is often not specifically related to where the error arises.
This looks as if FilterPanel has a dependency on an object that doesn't yet exist. I have usually found these to be either due to not resolving resource dictionaries correctly or ValueConverter parameters not cast properly to types or initialised at Design time.
Things to try - in ascending order of speed
1) Run the app and look at the Output window to ensure there are no silent binding errors.
2) Narrow down the issue - if you comment out the line that refers to the FilterPanel does the designer work? Often the error crops up in the next line.
3) Although the app appears to run OK has this introduced different behaviours?
4) Step back through your refactorings to find the point at which the problem arose.
I have found Expression Blend to be more tolerant but not necessarily more verbose when it does fall over.
UPDATE:
If you haven't already you may need to add the ThemeInfo custom attribute to your custom controls so they can find their resources - although I think if this mechanism were not working the app would throw an Exception. Anyhow the declaration is
[assembly:ThemeInfo(
//Theme specific resources,
//Generic resources
)]
There is a bunch of valid code/xaml that the VS2K8 designer cannot handle. A lot of them the Blend designer can handle.
If your control needs to be constructed in a special way before it can be 'used', then VS will blow up.
One VS2008 problem is controls from an abstract class. here or here which sounds like what may have happened if you are "refactoring"

Resources