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

时间:2013-05-16 08:22:23

标签: c# wpf xaml dispatcher

我正在使用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有这个命令绑定代码:

<Window.CommandBindings>
    <CommandBinding Command="{x:Static custom:MainWindow.MyCommand}"
                    Executed="MyCommandExecuted"
                    CanExecute="CanExecuteMyCommand" />
</Window.CommandBindings>

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

  

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

     

附加信息:'在类型上调用构造函数   'Vaernes.MainWindow'匹配指定的绑定约束   抛出一个例外。'行号'4'和行位置'9'。

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

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

建议任何人?

1 个答案:

答案 0 :(得分:12)

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

要解决它,你必须得到InnerException(也许还有它的InnerException以及a.s.o.)和callstack。然后解决它。

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

相关问题