首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单的silverlight打开文件对话框错误

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

Stack Overflow用户
提问于 2009-09-08 01:46:23
回答 2查看 12.7K关注 0票数 3

不久前,我编写了一个silverlight用户控件,它具有csv导入/导出功能。这一直很好,直到最近我才发现它在一个场景中出错。这可能是由于移动到Silverlight 3。

错误:

消息: Silverlight 2应用程序中未处理的错误

编号: 4004

类别: ManagedRuntimeError

消息: System.Security.SecurityException:对话框必须是用户发起的.

成本- System.Windows.Controls.OpenFileDialog.ShowDialog()

在MyControl.OpenImportFileDialog()

.=‘5’>.

代码:

代码语言:javascript
复制
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()调用的同一行(或之前)放置一个断点似乎也会导致此错误出现。有什么需要帮忙的吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-09-08 10:33:06

只需单击一次用户就可以执行两个操作。

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

然后尝试显示对话框,因为这是关于用户操作的第二个对话框,因此是不允许的。

去掉确认对话框,你就会没事的。

票数 7
EN

Stack Overflow用户

发布于 2013-02-19 11:43:56

if (dlg.ShowDialog()?)之前删除断点。代码将为我运行它的工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1391579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档