【问题标题】:XAML Parse Exception - xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"XAML 解析异常 - xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
【发布时间】:2012-01-28 12:54:16
【问题描述】:

我昨晚在我的 PC 上使用 VS 2010 Ultimate 处理 WPF 项目。

我刚刚使用 VS C# 2010 Express 在我的上网本上打开了它。

当我尝试运行它时,我抛出了一个 XAML Parse Exception,它告诉我该行:

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

是问题所在。我看不出它有什么问题,我从未更改过它,它看起来应该是它应该是的。

错误:

'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'.

完整的 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>

【问题讨论】:

  • 这是一个常见错误。然而,来源并不总是直截了当的。有时它与 XAML 本身无关。一种情况是我在序列化后通过文件流将对象写入磁盘。这在 Win7 中完美运行,并在 WinXP 中引发了这个异常。尝试在代码中放置断点并尝试找出导致错误的特定行。然后深入调用堆栈。干杯。

标签: c# wpf xaml


【解决方案1】:

XAMLParseException 是 WPF 中抛出的常见异常。不幸的是,它不是很有帮助。

为了帮助找出真正的错误是什么,您可以更早地在 Visual Studio 中打开异常报告。默认组合键是 Ctrl + Alt + E。从那里选中所有框。

现在,代码中引发的异常将在调试器中突出显示。

【讨论】:

  • 已排序。谢谢。窗口构造函数抛出 ArrayIndexOutOfBounds 异常。这与那条线无关。真烦人。
  • 哇,这让我的生活变得轻松多了。
  • 我希望我能早点找到这个。原来我的问题是在 SQLite 查询中拼错了 SELECT。我一直认为默认情况下会突出显示诸如此类的错误。我觉得很愚蠢。
  • 在我的自定义 wpf 控件中发现,我为依赖属性的默认值声明了错误的类型,在 CLR 的 throw 上打开异常后,Visual Studio 能够向我展示真正的问题
【解决方案2】:

从参考资料中试试这个

"Ur required dll"->properties->copy to local->true(从false改)

保存并清理解决方案,然后重建它。我认为它会起作用(对我有用)。

【讨论】:

    【解决方案3】:

    xaml 看起来不错。不应该有任何问题。 我认为您尝试获取一些数据。您是否正在尝试通过您的应用程序获取一些消息?如果是这样,则默认消息大小为 65536 字节。在您的 app.config 中增加该数字。 在运行程序的同时打开输出窗口,检查是否有绑定错误。

    【讨论】:

      【解决方案4】:

      我遇到了这个错误,更改异常报告似乎没有帮助

      我注意到我正在尝试调用:

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

      \server 应该是\\server

      更改此设置可解决问题,但如果出现一条错误消息,却没有告诉您有关错误的任何信息,这很烦人。

      当字符串在 wpf 中没有正确形成时,我实际上已经在几个地方注意到了这个错误,所以这可能是一个很好的起点。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-13
        • 1970-01-01
        相关资源
        最近更新 更多