简单的silverlight打开文件对话框错误

Luk*_*lch 3 c# debugging silverlight openfiledialog silverlight-3.0

前段时间我写了一个具有csv导入/导出功能的silverlight用户控件.这一直很好,直到最近我发现它在一个场景中出错.这可能是由于转向Silverlight 3.

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

代码:

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以打开一个导入文件,但如果我想更改导入文件,并重新打开文件对话框,则会出错.有谁知道为什么会这样?
此外,我在调试时遇到问题,因为在dlg.ShowDialog()调用的同一行(或之前)上放置一个断点似乎也会导致出现此错误.任何帮助,将不胜感激?

Gra*_*ury 7

您在一次用户点击时执行两项操作.

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

然后尝试显示对话框,因为这是用户操作的第二个对话框,不允许这样做.

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