不久前,我编写了一个silverlight用户控件,它具有csv导入/导出功能。这一直很好,直到最近我才发现它在一个场景中出错。这可能是由于移动到Silverlight 3。
错误:
消息: Silverlight 2应用程序中未处理的错误
编号: 4004
类别: ManagedRuntimeError
消息: System.Security.SecurityException:对话框必须是用户发起的.
成本- System.Windows.Controls.OpenFileDialog.ShowDialog()
在MyControl.OpenImportFileDialog()
.=‘5’>.
代码:
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()调用的同一行(或之前)放置一个断点似乎也会导致此错误出现。有什么需要帮忙的吗?
发布于 2009-09-08 10:33:06
只需单击一次用户就可以执行两个操作。
您将显示一个消息框,它有效地使用您的权限来显示有关用户操作的对话框。
然后尝试显示对话框,因为这是关于用户操作的第二个对话框,因此是不允许的。
去掉确认对话框,你就会没事的。
发布于 2013-02-19 11:43:56
在if (dlg.ShowDialog()?)之前删除断点。代码将为我运行它的工作。
https://stackoverflow.com/questions/1391579
复制相似问题