PresentationFramework.dll中出现未处理的“System.Windows.Markup.XamlParseException”类型exception

我正在使用C#/ WPF中的一个小应用程序,该应用程序由来自串行端口的数据提供。 它还会读取包含一些常量的文本文件以便计算某些内容。 事件处理程序在到达时处理传入的数据:

_serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Receive); 

这是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"); } } 

dispatcherTimer允许在串行线路上向设备重新发送命令,如果它无法在合理的时间内获取任何数据。

除了还从文本文件中读取外,应用程序还在主窗口的构造函数中定义了一些键盘快捷键手势:

 public MainWindow() { InitializeComponent(); InitializeComponent(); KeyGesture kg = new KeyGesture(Key.C, ModifierKeys.Control); InputBinding ib = new InputBinding(MyCommand, kg); this.InputBindings.Add(ib); Start(); } 

所以MainWindow.xaml有这个命令绑定代码:

    

Visual Studio Designer抱怨无效标记,但仍然习惯于运行正常,直到我在运行程序时开始出现这些错误:

PresentationFramework.dll中出现未处理的“System.Windows.Markup.XamlParseException”类型exception

附加信息:’对类型’Vaernes.MainWindow’的构造函数的调用与指定的绑定约束相匹配引发了exception。 行号’4’和行位置’9’。

在进行小代码更改后会出现此类错误。 最新版本正在替换程序读取的文本文件,另一个文件具有相同的名称(Add Existing item …)。 我在网上搜索了一些解决方案,但我找不到任何与我的问题非常相似的内容。

我怀疑它与Dispatcher线程或Input Bindings有关。 我还尝试为exception添加处理程序,并注意到发件人是System.Windows.Threading.Dispatcher。

有人建议吗?

运行时XamlParseException在大多数(如果不是全部)情况下是从构造函数内部抛出的exception。

要解决它,你必须得到InnerException(也许它的InnerException以及aso)和callstack。 然后解决它。

如果在调试期间没有发生exception,您可以尝试/捕获构造函数内的exception并记录所有必要的数据。