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