PresentationFramework.dll中发生未处理的"System.Windows.Markup.XamlParseException"类型异常

Mor*_*rty 4 c# wpf xaml dispatcher

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

_serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Receive);
Run Code Online (Sandbox Code Playgroud)

这是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");
    }            
}
Run Code Online (Sandbox Code Playgroud)

dispatcherTimer允许重发命令给单元上的串行线路,如果它未能得到在合理的时间量的任何数据.

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

public MainWindow()
{
    InitializeComponent();
    InitializeComponent();

    KeyGesture kg = new KeyGesture(Key.C, ModifierKeys.Control);
    InputBinding ib = new InputBinding(MyCommand, kg);
    this.InputBindings.Add(ib);

    Start();
}
Run Code Online (Sandbox Code Playgroud)

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

<Window.CommandBindings>
    <CommandBinding Command="{x:Static custom:MainWindow.MyCommand}"
                    Executed="MyCommandExecuted"
                    CanExecute="CanExecuteMyCommand" />
</Window.CommandBindings>
Run Code Online (Sandbox Code Playgroud)

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

PresentationFramework.dll中发生未处理的"System.Windows.Markup.XamlParseException"类型异常

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

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

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

有人建议吗?

Jef*_*Son 12

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

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

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