C#   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c# – 简单的silverlight打开文件对话框错误大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
前段时间我写了一个具有csv导入/导出功能的silverlight用户控件.这一直很好,直到最近我发现它在一个场景中出错.这可能是由于转向Silverlight 3.

错误
消息:Silverlight 2应用程序中的未处理错误
代码:4004
类别:ManagedRuntimeError
消息:System.Security.SecurityException:对话框必须由用户启动.
在System.Windows.Controls.OpenFileDialog.ShowDialog()
at 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()调用的同一行(或之前)上放置一个断点似乎也会导致出现此错误.
任何帮助,将不胜感激?

解决方法

您在一次用户点击时执行两项操作.

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

@R_772_9864@显示对话框,因为这是用户操作的第二个对话框,不允许这样做.

摆脱确认对话框,你会没事的.

大佬总结

以上是大佬教程为你收集整理的c# – 简单的silverlight打开文件对话框错误全部内容,希望文章能够帮你解决c# – 简单的silverlight打开文件对话框错误所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。