title: An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
body: <p>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:</p>
  
  <pre><code>_serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Receive);
  </code></pre>
  
  <p>Here is the Receive handler, along with a delegate that is created in a Dispatcher, to further update the UI.</p>
  
  <pre><code>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");
      }            
  }
  </code></pre>
  
  <p>The <code>dispatcherTimer</code> allows resending commands to the unit on the serial line, if it fails to get any data in a reasonable amount of time. </p>
  
  <p>In addition to also reading from a text file, the application has some keyboard shortcut gestures defined in the constructor of the main window:</p>
  
  <pre><code>public MainWindow()
  {
      InitializeComponent();
      InitializeComponent();
  
      KeyGesture kg = new KeyGesture(Key.C, ModifierKeys.Control);
      InputBinding ib = new InputBinding(MyCommand, kg);
      this.InputBindings.Add(ib);
  
      Start();
  }
  </code></pre>
  
  <p>So the MainWindow.xaml has this command binding code:</p>
  
  <pre><code>&lt;Window.CommandBindings&gt;
      &lt;CommandBinding Command="{x:Static custom:MainWindow.MyCommand}"
                      Executed="MyCommandExecuted"
                      CanExecute="CanExecuteMyCommand" /&gt;
  &lt;/Window.CommandBindings&gt;
  </code></pre>
  
  <p>Visual Studio Designer complains about invalid markup, but still used to run fine, until I started getting these errors when running the program:</p>
  
  <blockquote>
    <p>An unhandled exception of type
    'System.Windows.Markup.XamlParseException' occurred in
    PresentationFramework.dll</p>
    
    <p>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'.</p>
  </blockquote>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Suggestions anyone?</p>