"System.Windows.Markup.XamlParseException" 在 PresentationFramework.dll 中出现了?"

34

我在使用C#上的WPF,代码如下:

<Window       
    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"
    xmlns:utility="clr-namespace:DVRClientInterface.Utility"
    xmlns:toolkit="http://schemas.xceed.com/wpf/xaml/toolkit"     
    xmlns:wpftoolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"   
    xmlns:customControl="clr-namespace:DVRClientInterface.CustomControls"    
    x:Class="DVRClientInterface.MainWindow"
    WindowStyle="None" ResizeMode="CanMinimize"
    AllowsTransparency="True"
    WindowState = "Maximized"
    WindowStartupLocation="CenterScreen"    
    StateChanged="Window_StateChanged" 
    KeyUp="Window_KeyUp" 
    KeyDown="Window_KeyDown"    
    Closing="Window_Closing" Loaded="Window_Loaded"
     >
    <!-- WindowState = "Maximized" -->
    <Window.Background>
        <ImageBrush ImageSource="Resources\main_background.png"/>
    </Window.Background>

    <Grid x:Name="MainWindowGrid">
     //My GUI Code here

    </Grid>
</Window>

当我运行应用程序时,它会抛出以下异常

An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

Additional information: 'The invocation of the constructor on type 'DVRClientInterface.MainWindow' that matches the specified binding constraints threw an exception.' Line number '9' and line position '5'.

If there is a handler for this exception, the program may be safely continued.

第9行是xmlns:customControl="clr-namespace:DVRClientInterface.CustomControls"

但我不知道为什么,在那之前XAML仍然正常工作。 我只是编辑了C++项目中的代码,之后它总是抛出异常。

许多主题说这是由项目配置引起的,但我不知道我的项目配置在哪里引发了此异常。

有人可以建议我如何修复它吗?

更新:下面是输出调试信息

'MysticClient.exe': Loaded 'E:\TIN HOC\Learning\Chuong Tring Dao Tao\Working\Main_Code\SecurityLabCode\DVRClient\Bin\x86\Debug\MysticClient.exe', No native symbols in symbol file.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\mscoree.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\sechost.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\sspicli.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\cryptbase.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscoreei.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\shlwapi.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\gdi32.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\user32.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\msvcr110_clr0400.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\assembly\NativeImages_v4.0.30319_32\mscorlib\51e2934144ba15628ba5a31be2dae7dc\mscorlib.ni.dll', Cannot find or open the PDB file.
'MysticClient.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\ole32.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\uxtheme.dll', Cannot find or open the PDB file.
'MysticClient.exe' (Managed (v4.0.30319)): Loaded 'E:\TIN HOC\Learning\Chuong Tring Dao Tao\Working\Main_Code\SecurityLabCode\DVRClient\Bin\x86\Debug\MysticClient.exe', Symbols loaded.
'MysticClient.exe': Loaded 'C:\Windows\assembly\NativeImages_v4.0.30319_32\System\e40da7a49f8c3f0108e7c835b342f382\System.ni.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\assembly\NativeImages_v4.0.30319_32\WindowsBase\cd235caf797fb017f140016be88f33b7\WindowsBase.ni.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\cryptsp.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\rsaenh.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\assembly\NativeImages_v4.0.30319_32\PresentationCore\b52bc540630c3aa5de542c382af35c20\PresentationCore.ni.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\assembly\NativeImages_v4.0.30319_32\Presentatio5ae0f00f#\97e6b67983d07a066b68b3ae8be2f53d\PresentationFramework.ni.dll', Cannot find or open the PDB file.
'MysticClient.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework\v4.0_4.0.0.0__31bf3856ad364e35\PresentationFramework.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MysticClient.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\WindowsBase\v4.0_4.0.0.0__31bf3856ad364e35\WindowsBase.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MysticClient.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MysticClient.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\PresentationCore\v4.0_4.0.0.0__31bf3856ad364e35\PresentationCore.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MysticClient.exe': Loaded 'C:\Windows\assembly\NativeImages_v4.0.30319_32\System.Xaml\cc4d9093563dadee370788bbc3ecf4fb\System.Xaml.ni.dll', Cannot find or open the PDB file.
'MysticClient.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xaml\v4.0_4.0.0.0__b77a5c561934e089\System.Xaml.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\DWrite.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\WPF\wpfgfx_v0400.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\oleaut32.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\WPF\PresentationNative_v0400.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\clrjit.dll', Cannot find or open the PDB file.
Step into: Stepping over non-user code 'DVRClientInterface.App.App'
'MysticClient.exe': Loaded 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\nlssorting.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\assembly\NativeImages_v4.0.30319_32\System.Configuration\28586400bcaf94c13a9fd0dff4a1e090\System.Configuration.ni.dll', Cannot find or open the PDB file.
'MysticClient.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MysticClient.exe': Loaded 'C:\Windows\assembly\NativeImages_v4.0.30319_32\System.Xml\9ba07396ae369d010c5c3927a82ef426\System.Xml.ni.dll', Cannot find or open the PDB file.
'MysticClient.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\dwmapi.dll', Cannot find or open the PDB file.
Step into: Stepping over non-user code 'DVRClientInterface.App.InitializeComponent'
'MysticClient.exe': Loaded 'E:\TIN HOC\Learning\Chuong Tring Dao Tao\Working\Main_Code\SecurityLabCode\DVRClient\Bin\x86\Debug\CPPWrapper.dll', Symbols loaded.
'MysticClient.exe': Unloaded 'E:\TIN HOC\Learning\Chuong Tring Dao Tao\Working\Main_Code\SecurityLabCode\DVRClient\Bin\x86\Debug\CPPWrapper.dll'
'MysticClient.exe': Loaded 'E:\TIN HOC\Learning\Chuong Tring Dao Tao\Working\Main_Code\SecurityLabCode\DVRClient\Bin\x86\Debug\CPPWrapper.dll', Symbols loaded.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\RpcRtRemote.dll', Cannot find or open the PDB file.
'MysticClient.exe': Unloaded 'E:\TIN HOC\Learning\Chuong Tring Dao Tao\Working\Main_Code\SecurityLabCode\DVRClient\Bin\x86\Debug\CPPWrapper.dll'
'MysticClient.exe': Loaded 'E:\TIN HOC\Learning\Chuong Tring Dao Tao\Working\Main_Code\SecurityLabCode\DVRClient\Bin\x86\Debug\CPPWrapper.dll', Symbols loaded.
'MysticClient.exe': Loaded 'E:\TIN HOC\Learning\Chuong Tring Dao Tao\Working\Main_Code\SecurityLabCode\DVRClient\Bin\x86\Debug\Common.dll', Symbols loaded.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\IPHLPAPI.DLL', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\nsi.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\winnsi.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\msvcr110d.dll', Symbols loaded.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\msvcp110d.dll', Symbols loaded.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\ws2_32.dll', Cannot find or open the PDB file.
'MysticClient.exe': Loaded 'E:\TIN HOC\Learning\Chuong Tring Dao Tao\Working\Main_Code\SecurityLabCode\DVRClient\Bin\x86\Debug\DVRClientCore.dll', Symbols loaded.
'MysticClient.exe': Loaded 'E:\TIN HOC\Learning\Chuong Tring Dao Tao\Working\Main_Code\SecurityLabCode\DVRClient\Bin\x86\Debug\ClientCommunication.dll', Symbols loaded.
First-chance exception at 0x7757b499 in MysticClient.exe: 0xC0000005: Access violation writing location 0x00000014.
'MysticClient.exe': Unloaded 'E:\TIN HOC\Learning\Chuong Tring Dao Tao\Working\Main_Code\SecurityLabCode\DVRClient\Bin\x86\Debug\CPPWrapper.dll'
'MysticClient.exe': Unloaded 'E:\TIN HOC\Learning\Chuong Tring Dao Tao\Working\Main_Code\SecurityLabCode\DVRClient\Bin\x86\Debug\DVRClientCore.dll'
'MysticClient.exe': Unloaded 'E:\TIN HOC\Learning\Chuong Tring Dao Tao\Working\Main_Code\SecurityLabCode\DVRClient\Bin\x86\Debug\ClientCommunication.dll'
'MysticClient.exe': Unloaded 'E:\TIN HOC\Learning\Chuong Tring Dao Tao\Working\Main_Code\SecurityLabCode\DVRClient\Bin\x86\Debug\Common.dll'
'MysticClient.exe': Unloaded 'C:\Windows\SysWOW64\ws2_32.dll'
'MysticClient.exe': Unloaded 'C:\Windows\SysWOW64\msvcp110d.dll'
'MysticClient.exe': Unloaded 'C:\Windows\SysWOW64\msvcr110d.dll'
'MysticClient.exe': Unloaded 'C:\Windows\SysWOW64\IPHLPAPI.DLL'
'MysticClient.exe': Unloaded 'C:\Windows\SysWOW64\winnsi.dll'
'MysticClient.exe': Unloaded 'C:\Windows\SysWOW64\nsi.dll'
First-chance exception at 0x74e8b9bc in MysticClient.exe: Microsoft C++ exception: EEFileLoadException at memory location 0x00a7ab9c.
First-chance exception at 0x74e8b9bc in MysticClient.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.
First-chance exception at 0x74e8b9bc in MysticClient.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.
First-chance exception at 0x74e8b9bc in MysticClient.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.
First-chance exception at 0x74e8b9bc in MysticClient.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.
First-chance exception at 0x74e8b9bc in MysticClient.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.
First-chance exception at 0x74e8b9bc in MysticClient.exe: Microsoft C++ exception: Exception at memory location 0x00a7cae4.
First-chance exception at 0x74e8b9bc in MysticClient.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.
First-chance exception at 0x74e8b9bc in MysticClient.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.
First-chance exception at 0x74e8b9bc in MysticClient.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.
'MysticClient.exe': Loaded 'C:\Windows\SysWOW64\version.dll', Cannot find or open the PDB file.
An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: 'The invocation of the constructor on type 'DVRClientInterface.MainWindow' that matches the specified binding constraints threw an exception.' Line number '9' and line position '5'.
The program '[5664] MysticClient.exe: Native' has exited with code -1 (0xffffffff).
The program '[5664] MysticClient.exe: Managed (v4.0.30319)' has exited with code -1 (0xffffffff).

非常感谢!

T&T


1
你应该检查一下 "xmlns:utility="clr-namespace:DVRClientInterface.Utility" 和 "xmlns:customControl="clr-namespace:DVRClientInterface.CustomControls",看起来另一个是没问题的。 - J R B
我尝试移除自定义控件和其他控件,但是这个异常仍然发生。在此之前它还可以正常工作:( - TTGroup
我已经在我的帖子中添加了调试输出,您可以阅读它以获取更多信息。谢谢! - TTGroup
9个回答

44

这不是XAML的问题。错误信息表明它试图创建DVRClientInterface.MainWindow实例,但您的构造函数抛出了异常。

您需要查看"内部异常"属性以确定根本原因。可能是任何东西,但应提供方向。

在 Visual Studio 中显示内部异常的示例

例如,如果在窗口的构造函数中连接到数据库,而由于某种原因该数据库不可用,则内部异常可能是TimeoutExceptionSqlException或由数据库代码引发的任何其他异常。

如果在静态构造函数中引发异常,则可以从MainWindow引用的任何类生成异常。如果任何MainWindow字段调用可能会引发异常的方法,则还会运行类初始化程序。


谢谢您的帮助,但是当我在DVRClientInterface.MainWindow()函数中设置断点时,它甚至没有到达这个断点,我认为它在调用此构造函数之前就崩溃了。如何获取内部异常? - TTGroup
1
我在我的帖子中添加了调试输出,您可以阅读它以获取更多信息。谢谢! - TTGroup
2
你怎么看待这个窗口? - Demodave

31

问题已解决!我使用 Ctrl + Alt + E 打开异常窗口,并勾选了所有的 throw 复选框。这样调试器就可以在错误代码处停止。


8

我花了很长时间才解决了这个问题,所以为了搜索者的利益:

我遇到了一个奇怪的问题,在调试时应用程序可以正常工作,但发布后却出现了XamlParseException异常。

在按照Katjoek的说明修复了x86/x64问题之后,问题仍然存在。

问题是CEF教程中说要从NuGet下载System.Windows.Interactivity(即使它已经在.NET的引用扩展部分中)并将specific version设置为true

一旦部署后,由于不同的应用程序打包了不同版本的System.Windows.Interactivity,因此导致拒绝使用其他版本的dll,从而导致整个应用程序崩溃并抛出XamlParseException异常。


4
我使用"All cpu"目标构建应用程序时,遇到了问题,因为它引用了一个仅支持x64的第三方(managed) DLL。

3

更新:这不是解决方案,但是它是一个解决问题的变通方法,可以避免出现问题。

我已经通过将配置从发布配置更改为调试配置来解决问题。


2
我在Debug模式下运行成功,但在Release模式下失败了。我使用Ctrl Alt E检查所有内容,并找到异常发生的位置,然后我知道了问题所在。我使用了一些在Debug文件夹中存在但在Release文件夹中不存在的文件。 - yu yang Jian
1
更改配置不是解决方案。正如@yuyangJian所说,它可能会提供有关问题或缺失的信息。 - Jaime Marín

2

我在我的 Xaml 视图页面中有一个 ToggleSwitch:

        <StackPanel>
            <ToggleSwitch x:Name="myToggleStartButton" 
                          Header="START" CornerRadius ="20" 
                          OffContent="Show" OnContent="Hide"
                          Style="{StaticResource ToggleButtonRevealStyle}" 
                          IsOn ="{x:Bind Path=XamlProductionViewModel.XamlIsMainOnOffChecked, Mode=TwoWay}" 
                          Height="80" Width="100" Margin="20, 5">
            </ToggleSwitch>
        </StackPanel>

突然间我遇到了一个Xaml解析错误:

在部分类ProductionPage : global::Microsoft.UI.Xaml.Controls.Page (ProductionPage.g.i.cs)中:

Microsoft.UI.Xaml.Markup.XamlParseException
  HResult=0x802B000A
  Message=XAML parsing failed.
  Source=WinRT.Runtime
  StackTrace:
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
在异常窗口中点击“复制详细信息”可以读取堆栈信息,但仍无法给出崩溃的真正原因。
因此,对我来说解决方案是将此代码添加到我的视图页面代码后端(ProductionPage.xaml.cs)。
  try
        {
            InitializeComponent();
        }
        catch (XamlParseException parseException)
        {
            Debug.WriteLine($"[{DateTime.Now.ToString("HH:mm:ss.ff")}]: {parseException.Message}");
            foreach (var key in parseException.Data.Keys)
            {
                Debug.WriteLine($"{key.ToString()} : {parseException.Data[key]?.ToString()}");
            }
            throw;
        }

现在看到崩溃,我按一次F5继续执行,在即时窗口中可以读取到这个:
RestrictedDescription : Cannot apply a Style with TargetType 'Microsoft.UI.Xaml.Controls.Primitives.ToggleButton' 
to an object of type 'Microsoft.UI.Xaml.Controls.ToggleSwitch'. [Line: 192 Position: 35]
RestrictedErrorReference : 
RestrictedCapabilitySid : 
__RestrictedErrorObjectReference : WinRT.ExceptionHelpers+__RestrictedErrorObject
__HasRestrictedLanguageErrorObject : False

现在我知道 Style="{StaticResource ToggleButtonRevealStyle}" 导致了错误。这是我处理 Xaml 解析错误的方式。


0
在我的情况下,当我将动态创建的控件(组合框)分配给来自其他类的相同创建的控件时,出现了这个错误。
//dynamic created controls
ComboBox combobox1 = ManagerControls.myCombobox1;
...some events

ComboBox combobox2 = ManagerControl.myComboBox2;
...some events

.

//method in constructor
public static void InitializeDynamicControls()
{
     ComboBox cb = new ComboBox();
     cb.Background = new SolidColorBrush(Colors.Blue);
     ...
     cb.Width = 100;
     cb.Text = "Select window";
        
     ManagerControls.myCombobox1 = cb;
     ManagerControls.myComboBox2 = cb; // <-- error here
}

解决方案:创建另一个ComboBox cb2并将其分配给ManagerControls.myComboBox2
希望我能帮到某人。

0

当我遇到这个问题时,我实际上只是忘记在代码的XAML中填写参数值。

但出于某种原因,异常会将我发送到WPF程序的CS而不是XAML。我不知道为什么。


0

对我来说,简单的重新启动解决了问题。不确定原因。我也进行了清理和重建,但我相当确定是重新启动解决了问题。


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