WPF XAML解析异常发生了。

3
我遇到了以下异常:'PresentationFramework.dll' 中的 'System.Windows.Markup.XamlParseException'。我找到了这个帖子(链接)
显然,运用CTRL+ALT+E方法并检查所有异常并不能解决我的问题,或者是我使用方法不正确。我检查了所有异常,它导致我找到了我MainWindow的InitializedComponent()函数,问题就在这里。您有什么建议吗?我会将我所有的XAML代码:MainWindow发送给您。
<Window x:Class="DPCKOU_prog3hf_pong.MainWindow"
    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"
    xmlns:local="clr-namespace:DPCKOU_prog3hf_pong"
    mc:Ignorable="d"
    Title="PongGame" Height="406" Width="717"
    Loaded="Window_Loaded"
    KeyDown="Window_KeyDown"
    >
<Canvas Background="{StaticResource fieldBGSP}">
    <Rectangle Canvas.Left="{Binding Path=Pad.Area.X}"
                   Canvas.Top="{Binding Path=Pad.Area.Y}"
                   Width="{Binding Path=Pad.Area.Width}"
                   Height="{Binding Path=Pad.Area.Height}"
                   Fill="{StaticResource RectangleFill}"/>
    <Ellipse Canvas.Left="{Binding Path=Ball.Area.X}"
                   Canvas.Top="{Binding Path=Ball.Area.Y}"
                   Width="{Binding Path=Ball.Area.Width}"
                   Height="{Binding Path=Ball.Area.Height}"
                   Fill="{StaticResource EllipseFill}"/>
    </Canvas>
</Window>

还有我的 App.xaml 文件:

<Application x:Class="DPCKOU_prog3hf_pong.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:DPCKOU_prog3hf_pong"
         xmlns:sys="clr-namespace:System;assembly=mscorlib"
         StartupUri="MainWindow.xaml">
    <Application.Resources>
        <SolidColorBrush x:Key="RectangleFill" Color="#FF0F4228"/>
        <SolidColorBrush x:Key="EllipseFill" Color="#FF207CBB"/>
          <ImageBrush x:Key="fieldBGMP" ImageSource="pack://application:,,,/texturesExport/background.png"/>
        <ImageBrush x:Key="fieldBGSP" ImageSource="pack://application:,,,/texturesExport/pong_ingameSP.png"/>
    </Application.Resources>
</Application>

编辑

我在Canvas之前的孤立的>符号处遇到错误。在编辑器中,背景图像显示正常。不知道是什么原因导致了这个问题。

我使用了pack uri方法更新了我的代码。


1
请提供内部异常(如果存在),这将非常有助于解决问题。 - Hakan Fıstık
尝试像这样从Background属性中删除末尾的空格:<Canvas Background="{StaticResource fieldBGSP}"> - Hakan Fıstık
我已经将它们删除了,但没有任何区别。当然,我会更新帖子的。 我目前正在检查如何内联获取这些内部异常,但我找到的都是如何通过代码获取它们 :/ - agiro
2个回答

2
图片的构建操作是否设置为资源?另外,您可以尝试使用pack uri作为图像源,在这种情况下,它应该是pack://application:,,,/texturesExport/background.png

我使用了以下的 pack uri<ImageBrush x:Key="fieldBGMP" ImageSource="pack://application:,,,/texturesExport/background.png"/> 但不幸的是它没有起作用。我应该使用 Image 替代 ImageBrush 吗?编辑: 我的图片在项目中的位置是:C:\Users\agoston\Documents\Visual Studio 2015\Projects\DPCKOU_prog3hf_pong\DPCKOU_prog3hf_pong\texturesExport - agiro
不,我不这么认为。你能试着像这样内联资源吗? - czubehead
我尝试了,但是我得到了相同的错误。顺便说一下,该位置指向了那个 ImageBrush - agiro
显然,我认为图像构建操作不是资源。怎么做呢?我在网上找不到相关信息。 - agiro
我找到了它,但是我无法编辑我的图像的构建操作。但是我可以为我的 .cs 文件做到:/ 有什么想法吗? - agiro
1
显然将图像包含在项目中解决了构建操作问题,并解决了我的原始问题,谢谢。 - agiro

1
假设您在*.xaml.cs文件中没有任何代码,则问题可能与正确评估ImageSource属性有关。注释掉两个ImageBrush,看看这是否会导致异常消失。 这是解决您问题的方案

感谢您的输入。 现在我已经删除了我的ImageBrushesCanvasBackground属性,我得到了一个LoadFromContext error。顺便说一下,以前我是好的。 我的MainWindow.cs和其他类中确实有代码,但在添加ImageBrushes之前它们都运行良好。 编辑:顺便说一下,在那个error之后,我继续执行程序,一切都正常。 正在检查您提供的链接... - agiro
另一个更新:我使用了 pack ui,但不幸的是它不起作用。我可以启动,所以没有下划线或其他什么,但我得到了相同的 exception。 顺便说一下,我的图像在以下文件夹中: C:\Users\agoston\Documents\Visual Studio 2015\Projects\DPCKOU_prog3hf_pong\DPCKOU_prog3hf_pong\texturesExport - agiro
请提供更多的代码,以及完整的异常堆栈(包括内部异常等)。 - Eyal Perry
还没有完成,但我正在尝试为我的PNG图像设置构建操作。显然,对于这些图像,我没有build actions属性。有什么想法吗? - agiro
完成了,我没有将图像设置为资源。感谢所有的帮助。 - agiro
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接