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

标签 c# silverlight debugging silverlight-3.0 openfiledialog

前一段时间我写了一个 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/

相关文章:

linux - 在 linux 中,如何在 gdb 中找到哪个线程处于挂起状态?

java - 如何使用 eclipse debug 识别谁调用了 gwt 服务?

android - eclipse调试中没有显示变量

c# - 避免 "Use of unassigned local variable"错误

银光网络服务

c# - MySql 从列中选择所有数据并计数

javascript - 用于基于信息亭的应用程序的 HTML 5/Javascript

silverlight - Caliburn Micro 中保护方法的强制重新评估

c# - 调整 WPF 中 ToolWindow 和内容的大小

c# - Assetbundle 不适用于 iOS