19

I was working on a WPF project on my PC last night, using VS 2010 Ultimate.

I have just opened it up on my Netbook with VS C# 2010 Express.

When I try to run it, it I am thrown an XAML Parse Exception which tells me that the line:

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Is the problem. I can't see a problem with it, I have never changed it and it looks like it is what it should be.

The Error:

'The invocation of the constructor on type 'WpfApplication1.MainWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '9'.

Full XAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Camera" Height="550" Width="826" Background="#ddd" ResizeMode="NoResize" WindowStyle="None" MouseLeftButtonDown="Window_MouseLeftButtonDown" BorderBrush="#FF8D8D8D" BorderThickness="2" >
<Window.Resources>
    <Style TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                   <ContentPresenter
              Margin="{TemplateBinding Control.Padding}"
              HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
              VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
              SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"
              ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
              RecognizesAccessKey="True"
              Content="{TemplateBinding ContentControl.Content}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid Width="830" Height="510">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="132" />
        <ColumnDefinition Width="698*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="29" />
        <RowDefinition Height="319" />
        <RowDefinition Height="121" />
        <RowDefinition Height="16" />
        <RowDefinition Height="25*" />
    </Grid.RowDefinitions>
    <ComboBox Name="PortNames" Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" SelectionChanged="PortNames_SelectionChanged" Margin="12,6,0,0" />
    <Ellipse Grid.Row="2"  Height="120" HorizontalAlignment="Left" Name="ellipse1" StrokeThickness="6" VerticalAlignment="Top" Width="120" MouseLeftButtonDown="ellipse1_MouseLeftButtonDown" Margin="12,0,0,0">
        <Ellipse.Fill>
            <LinearGradientBrush EndPoint="1,1" StartPoint="0,0">
                <GradientStop Color="#89FF0000" Offset="0" />
                <GradientStop Color="Red" Offset="1" />
            </LinearGradientBrush>
        </Ellipse.Fill>
        <Ellipse.Stroke>
            <LinearGradientBrush EndPoint="1,1" StartPoint="0,0">
                <GradientStop Color="#FFB10000" Offset="0" />
                <GradientStop Color="#83B20707" Offset="0" />
                <GradientStop Color="#F7B00000" Offset="1" />
            </LinearGradientBrush>
        </Ellipse.Stroke>
    </Ellipse>
    <Button Content="Sync" Height="23" HorizontalAlignment="Left" Margin="57,6,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" Grid.Row="1" />
    <Rectangle Height="23" HorizontalAlignment="Left" Margin="12,6,0,0" Name="rectangle2" Stroke="Black" VerticalAlignment="Top" Fill="Red" Width="39" OpacityMask="#AA000000" Grid.Row="1" />
    <ProgressBar Grid.Row="4"  Height="19" HorizontalAlignment="Left" Name="progressBar1" VerticalAlignment="Top" Width="120" Margin="12,2,0,0" />
    <Rectangle Grid.RowSpan="5" Grid.Column="1" Height="500" Name="rectangle1" Stroke="#aaa" Width="660" Fill="#eee" Margin="12,5,26,5" />
    <Button Grid.Column="1" Height="18" HorizontalAlignment="Left" Margin="630,-20,0,0" Name="image1" VerticalAlignment="Top" Width="40" Click="image1_Click"><Image Stretch="Fill" Source="/PhotoProgram;component/Images/Close.png" /></Button>
    <Button Grid.Column="1" Height="18" HorizontalAlignment="Left" Margin="587,-20,0,0" Name="minimize" VerticalAlignment="Top" Width="40" Click="minimize_Click"><Image Stretch="Fill" Source="/PhotoProgram;component/Images/Minimize.png"  /></Button>
</Grid>

Luke
  • 2,434
  • 9
  • 39
  • 64
  • 1
    This is a common error. However the source is not always straightforward. And sometimes it has nothing to do with the XAML itself. One case I had what writing an object via filestream to the disk after serialization. This worked perfectly in Win7 and throwed exactly this exception in WinXP. Try putting the breakpoints in your code and try to figure out what specific line causes the error. And then go deeper in the call stack. Cheers. – Oybek Jan 28 '12 at 13:00

4 Answers4

74

XAMLParseException is a common exception thrown in WPF. Unfortunately it isn't very helpful.

To help find out what the real error is, you can turn on exception reporting much earlier in Visual Studio. Default key combination is Ctrl + Alt + E. From there, check all the boxes.

Now the exception that is thrown in your code will be highlighted in the debugger.

ose
  • 4,065
  • 2
  • 24
  • 40
  • 3
    Sorted. Thanks. The window constructor was throwing an ArrayIndexOutOfBounds Exception. It was nothing to do with that line. Really annoying. – Luke Jan 28 '12 at 13:09
  • 1
    Wow, that made my life so much easier. – B.K. Feb 12 '14 at 18:53
  • I wish I had found this hours earlier. Turns out my problem was misspelling SELECT in an SQLite query. I had always thought that errors such as those got highlighted by default. I feel so stupid. – Gilgamesh Apr 06 '15 at 00:37
  • 1
    turns out in my custom wpf control, I declare the wrong type for a dependency property's default value, after turning on the exception on CLR's throw, visual studio was able to show me the real issue – gavin Mar 02 '16 at 19:17
3

Try this one from reference

"Ur needed dll"->properties->copy to local->true (change it from false)

save & clean solution & then rebuild it. I think it will work(worked for me).

Taryn
  • 242,637
  • 56
  • 362
  • 405
1

that xaml looks ok. there shouldnt be any problem with that. i think you try to get some data. are you trying get some message with your application? if so the default message size is 65536 bytes. increase that number in your app.config. also open the output windows while running you program and check if there are binding errors.

Hashmat Khalil
  • 1,826
  • 1
  • 25
  • 49
0

I had this error and changing exception reporting did not seem to help

I noticed that I was trying to call:

var processClass = new ManagementClass(@"\server.domain1.co.uk\root\cimv2:Win32_Process");

Where \server should be \\server

Changing this fixed the issue but it is pretty annoying having an error message that doesn't tell you anything about your error.

I have actually noticed this error in a few places when a string has not been properly formed in wpf, so that could be a good place to start.

Bassie
  • 9,529
  • 8
  • 68
  • 159