Simple Silverlight open-file-dialog errors

A back, I wrote a custom silverlight control that had a csv import / export function. This works fine, until recently, I found an error in one scenario. Perhaps this was due to the transition to Silverlight 3.

Error:
Message: Unhandled error in Silverlight 2 application Code: 4004
Category: ManagedRuntimeError
Message: System.Security.SecurityException: Dialogs must be triggered by the user.
        in System.Windows.Controls.OpenFileDialog.ShowDialog ()
        in MyControl.OpenImportFileDialog ()
      with ...

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

, , . - , ? , , ( ) dlg.ShowDialog(), , . ?

+3
2

.

, .

, , .

, .

+7

, (dlg.ShowDialog()?? false) .

0

Source: https://habr.com/ru/post/1717101/


All Articles