前一段时间我写了一个 silverlight 用户控件,它具有 csv 导入/导出功能。这一直运行良好,直到最近我发现它在一种情况下出错。这可能是由于迁移到 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() 调用的同一行(或之前)放置一个断点似乎也会导致出现此错误。
任何帮助将不胜感激?
最佳答案
一次用户点击执行两项操作。
您显示一个消息框,该消息框有效地使用您的权限来显示有关用户操作的对话框。
然后您尝试显示该对话框,因为这是不允许的关于用户操作的第二个对话框。
摆脱确认对话框,你会没事的。
关于c# - 简单的 silverlight 打开文件对话框错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1391579/