PresentationFramework.dll中发生了类型为'System.Windows.Markup.XamlParseException'的未处理异常 [英] An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

查看:242
本文介绍了PresentationFramework.dll中发生了类型为'System.Windows.Markup.XamlParseException'的未处理异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

此处是Receive处理程序以及在Dispatcher中创建的委托,以进一步更新

Here is the Receive handler, along with a delegate that is created in a Dispatcher, to further update the 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 允许将命令重新发送到串行线路上的设备。

The dispatcherTimer allows resending commands to the unit on the serial line, if it fails to get any data in a reasonable amount of time.

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

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();
}

因此MainWindow.xaml具有以下命令绑定代码:

So the MainWindow.xaml has this command binding code:

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

Visual Studio Designer抱怨标记无效,但一直运行良好,直到我开始遇到这些错误运行程序时:

Visual Studio Designer complains about invalid markup, but still used to run fine, until I started getting these errors when running the program:


$ b $中发生了类型为
'System.Windows.Markup.XamlParseException'的未处理异常b PresentationFramework.dll

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

其他信息:'对类型为
'Vaernes.MainWindow'的构造函数的调用与指定的绑定约束
一起抛出行号 4和行位置 9。

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'.

在进行少量代码更改后,会出现这种错误。最新的做法是将程序读取的文本文件替换为另一个具有相同名称的文件(添加现有项...)。我已经在网上搜索了一些解决方案,但是找不到与我的问题非常相似的解决方案。

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.

我怀疑这与调度程序线程或输入绑定。
我还尝试为异常添加处理程序,并注意到发件人是System.Windows.Threading.Dispatcher。

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.

建议有人吗?

推荐答案

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

Runtime XamlParseException is in most (if not all) cases an exception thrown from inside the constructor.

要解决此问题,您必须获取InnerException(以及它的InnerException以及)。然后修复它。

To solve it, you have to get the InnerException (and maybe its InnerException as well a.s.o.) and the callstack. Then fix it.

如果在调试过程中未发生异常,则可以尝试/捕获构造函数中的异常并记录所有必要的数据。

If the exception does not occur during debugging, you may try/catch the exception inside the constructor and log all necessary data.

这篇关于PresentationFramework.dll中发生了类型为'System.Windows.Markup.XamlParseException'的未处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆