3

不久前,我编写了一个具有 csv 导入/导出功能的 silverlight 用户控件。这一直运行良好,直到最近我发现它在一种情况下出错。这可能是因为迁移到 Silverlight 3。

错误:
消息:Silverlight 2 应用程序
代码中的未处理错误:4004
类别:ManagedRuntimeError
消息:System.Security.SecurityException:对话框必须由用户启动。
       在 System.Windows.Controls.OpenFileDialog.ShowDialog()
       在 MyControl.OpenImportFileDialog()
       在 ...

编码:

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() 调用的同一行(或之前)放置断点似乎也会导致此错误出现。任何帮助,将不胜感激?

4

2 回答 2

7

您只需单击一次用户即可执行两项操作。

您显示一个消息框,该消息框有效地使用您的权限来显示有关用户操作的对话框。

然后您尝试显示该对话框,因为这是关于用户操作的第二个对话框,这是不允许的。

摆脱确认对话框,你会没事的。

于 2009-09-08T10:33:06.437 回答
0

if (dlg.ShowDialog() ?? false)代码将为我运行它之前删除断点。

于 2013-02-19T11:43:56.730 回答