4

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?

Gustavo Mori
  • 8,319
  • 3
  • 38
  • 52
Morty
  • 43
  • 1
  • 1
  • 3
  • Why are there two `InitializeComponent();`? Furthermore, what's the InnerException? – JeffRSon May 16 '13 at 08:26
  • 1
    Well it remained from some code that I copied from the web. I guess I could just remove one of them, but the error still appears...:( – Morty May 16 '13 at 08:33
  • It's not only a "guess" - you shouldn't initialize twice. *And* again - what's the InnerException? – JeffRSon May 16 '13 at 08:35
  • Ok. The InnerException is: {"Index was outside the bounds of the array."} (of type System.Windows.Threading.DispatcherUnhandledExceptionEventArgs)... – Morty May 16 '13 at 08:39
  • So you need to look where this is thrown. What's the callstack of the exception? – JeffRSon May 16 '13 at 08:40
  • Oops. By debugging it exceeds the size of the array when reading from the textfile. Fixing it now... – Morty May 16 '13 at 08:46
  • Seems like the new textfile which replaced the old one was a few lines longer. I edited it and the Exception is now gone. Thanks a lot! Checking the InnerException was useful. :) – Morty May 16 '13 at 08:49
  • See answer for summary ;-) – JeffRSon May 16 '13 at 08:52

1 Answers1

13

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.

JeffRSon
  • 10,404
  • 4
  • 26
  • 51