Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

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

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 complains about invalid markup, but still used to run fine, until I started getting these errors when running the program:

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

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.

Suggestions anyone?

like image 267
Morty Avatar asked Dec 11 '22 15:12

Morty


1 Answers

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

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.

like image 52
JeffRSon Avatar answered Apr 27 '23 00:04

JeffRSon