title: Simple silverlight open-file-dialog errors
body: <p>A while back I wrote a silverlight user control which had a csv import/export feature. This has been working fine, until recently I discovered it erroring in one scenario. This may have been due to moving to Silverlight 3. </p>
  
  <p><strong>The Error:</strong><br>
  Message: Unhandled Error in Silverlight 2 Application<br>
  Code: 4004<br>
  Category: ManagedRuntimeError<br>
  Message: System.Security.SecurityException: Dialogs must be user-initiated.<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at System.Windows.Controls.OpenFileDialog.ShowDialog()<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at MyControl.OpenImportFileDialog()<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at ...</p>
  
  <p><strong>The Code:</strong></p>
  
  <pre><code>private void BrowseFileButton_Click(object sender, RoutedEventArgs e)
  {
      if (string.IsNullOrEmpty(lblFileName.Text))
      {
          if (MessageBox.Show("Are you sure you want to change the Import file?", "Import", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
          {
              return;
          }
      }
      EnableDisableImportButtons(false);
      var fileName = OpenImportFileDialog();
      lblFileName.Text = fileName ?? string.Empty;
      EnableDisableImportButtons(true);    
  }
  
  private string OpenImportFileDialog()
  {
      var dlg = new OpenFileDialog { Filter = "CSV Files (*.csv)|*.csv" };
      if (dlg.ShowDialog() ?? false)
      {
          using (var reader = dlg.File.OpenText())
          {
              string fileName;
              //process the file here and store fileName in variable
              return fileName;
          }
      }
  }
  </code></pre>
  
  <p>I can open an import file, but if i want to change the import file, and re-open the file dialog, it errors. Does anyone know why this is the case?<br>
  Also, I am having trouble debugging because placing a breakpoint on the same line (or prior) to the dlg.ShowDialog() call seems to cause this error to appear as well.
  Any help would be appreciated?</p>