C# PresentationFramework.dll 中发生类型为“System.Windows.Markup.XamlParseException”的未处理异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16582374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
提问by Morty
I'm working on a small application in C# / WPF that is fed by data from the serial port. It also reads a text file containing some constants in order to calculate something. An event handler is made to handle the incoming data when it arrives:
我正在开发一个 C#/WPF 中的小应用程序,它由来自串行端口的数据提供。它还读取包含一些常量的文本文件以计算某些内容。事件处理程序用于在传入数据到达时对其进行处理:
_serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Receive);
Here is the Receive handler, along with a delegate that is created in a Dispatcher, to further update the UI.
这是 Receive 处理程序以及在 Dispatcher 中创建的委托,以进一步更新 UI。
private delegate void UpdateUiTextDelegate(string text);
private void Receive(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
// collect characters received to our 'buffer' (string)
try
{
// stops long running output timer if enabled
if (dispatcherTimer.IsEnabled)
{
dispatcherTimer.Stop();
}
message = _serialPort.ReadLine();
dispatcherTimer.Start();
Dispatcher.Invoke(DispatcherPriority.Send, new UpdateUiTextDelegate(updateUI), message);
}
catch (Exception ex)
{
// timeout
dispatcherTimer.Start();
SerialCmdSend("SCAN");
}
}
The dispatcherTimerallows resending commands to the unit on the serial line, if it fails to get any data in a reasonable amount of time.
的dispatcherTimer允许重发命令给单元上的串行线路,如果它未能得到在合理的时间量的任何数据。
In addition to also reading from a text file, the application has some keyboard shortcut gestures defined in the constructor of the main window:
除了从文本文件中读取之外,该应用程序在主窗口的构造函数中还定义了一些键盘快捷键手势:
public MainWindow()
{
InitializeComponent();
InitializeComponent();
KeyGesture kg = new KeyGesture(Key.C, ModifierKeys.Control);
InputBinding ib = new InputBinding(MyCommand, kg);
this.InputBindings.Add(ib);
Start();
}
So the MainWindow.xaml has this command binding code:
所以 MainWindow.xaml 有这个命令绑定代码:
<Window.CommandBindings>
<CommandBinding Command="{x:Static custom:MainWindow.MyCommand}"
Executed="MyCommandExecuted"
CanExecute="CanExecuteMyCommand" />
</Window.CommandBindings>
Visual Studio Designer complains about invalid markup, but still used to run fine, until I started getting these errors when running the program:
Visual Studio 设计器抱怨无效标记,但仍然可以正常运行,直到我在运行程序时开始收到这些错误:
An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: 'The invocation of the constructor on type 'Vaernes.MainWindow' that matches the specified binding constraints threw an exception.' Line number '4' and line position '9'.
PresentationFramework.dll 中发生类型为“System.Windows.Markup.XamlParseException”的未处理异常
附加信息:“对与指定绑定约束匹配的类型 'Vaernes.MainWindow' 的构造函数的调用引发了异常。” 行号“4”和行位置“9”。
This kind of error appears after making small code changes. The latest was replacing the text file read by the program, with another one with the same name (Add Existing item...). I have searched around some on the web for solutions but I can't find any that is quite similar to my problem.
进行小的代码更改后会出现这种错误。最新的是将程序读取的文本文件替换为另一个具有相同名称的文本文件(添加现有项目...)。我在网上搜索了一些解决方案,但找不到与我的问题非常相似的解决方案。
I suspect it has something to do with either the Dispatcher thread or the Input Bindings. I also tried to add a handler for the exception and noticed that the sender was System.Windows.Threading.Dispatcher.
我怀疑它与 Dispatcher 线程或输入绑定有关。我还尝试为异常添加一个处理程序,并注意到发件人是 System.Windows.Threading.Dispatcher。
Suggestions anyone?
任何人的建议?
采纳答案by JeffRSon
Runtime XamlParseExceptionis in most (if not all) cases an exception thrown from inside the constructor.
运行时XamlParseException在大多数(如果不是全部)情况下是从构造函数内部抛出的异常。
To solve it, you have to get the InnerException (and maybe its InnerException as well a.s.o.) and the callstack. Then fix it.
要解决它,您必须获得 InnerException(可能还有它的 InnerException)和调用堆栈。然后修复它。
If the exception does not occur during debugging, you may try/catch the exception inside the constructor and log all necessary data.
如果在调试过程中没有发生异常,您可以在构造函数中尝试/捕获异常并记录所有必要的数据。

