SaveFileDialog 在调用 showDialog() 后直接自动关闭

Posted

技术标签:

【中文标题】SaveFileDialog 在调用 showDialog() 后直接自动关闭【英文标题】:SaveFileDialog closes automatically directly after calling showDialog() 【发布时间】:2017-04-04 12:06:25 【问题描述】:

我正在使用带有 Visual Studio 2013 的 Windows 7。

我的应用程序是一个带有 GeckoFx 的网络浏览器组件。在下载事件中,我触发如下打开 SaveFileDialog。但在某些情况下,对话框会在调用 ShowDialog() 后直接消失,并返回一个 DialogResult.Cancel,它会跳转到 else 语句,尽管没有人按下取消。 不会抛出任何错误。

有什么建议为什么会在这里发生?我对此一无所知... :-(

        'Save file dialog
        Dim saveFileDialog1 As New SaveFileDialog()

        saveFileDialog1.Filter = "CSV file (*.csv)|*.csv|All files (*.*)|*.*"
        saveFileDialog1.FilterIndex = 2
        saveFileDialog1.RestoreDirectory = True
        saveFileDialog1.FileName = e.Filename
        saveFileDialog1.AutoUpgradeEnabled = False
        saveFileDialog1.CheckPathExists = False
        saveFileDialog1.InitialDirectory = globalParameters.getDownloadDirectory() 'globalParameters._downloadDirectory

        dialogResultValue = saveFileDialog1.ShowDialog()

        If dialogResultValue = DialogResult.OK Then
            'should go on here first, if user presses okay
        Else
            ' I am coming to this point, althoug nobody pressed any cancel button or any other input had happened yet
        End If

【问题讨论】:

@downvoter:你应该评论为什么你对我的问题投了反对票。也许我应该添加一些缺失的信息或其他......现在,我认为这是一个有效的问题 你在调试模式下试过什么? 键盘缓冲区中是否有错误的击键?对话是如何触发的?是通过键盘输入还是鼠标输入?表单的CancelButtonAcceptButton 属性设置为什么? @ChrisDunaway 它由 geckofx 框架的下载事件触发(例如“Public Sub LauncherDialog_Download(ByVal sender As Object, ByVal e As Gecko.LauncherDialogEvent)”);除了上面示例代码-sn-p 中提到的以外,我没有将该属性设置为其他特定设置。我猜那是标准的? @DannyJames 它发生在调试模式和应用程序正常运行时;我尝试调试,但我能看到的唯一效果是,如果调用 ShowDialog(),应用程序会直接返回 Cancel。没有错误或其他任何显示 【参考方案1】:

感谢@DannyJames 和@ChrisDunaway 的建议。

不知何故,我可以(通过我的问题和您的回答)发现SaveFileDialog.ShowDialog(Me) 需要引用Me 表单。

只有这样 SaveFileDialog 才能正确加载而不会出现错误,甚至在没有任何其他用户操作的情况下取消其调用。

不幸的是,我将下载部分放入了一个未被Inherits System.Windows.Forms.Form 继承的 vb 类,因此它没有对表单的引用(这显然是必需的)。

我更改了我的代码,因此我可以引用表单(这样我就可以使用对表单的引用,例如表单类中的Me)。它就像一个魅力。

下面是一个完整的例子:

Imports System.IO
Imports Gecko
Imports System
Imports System.Windows.Forms
Imports System.Drawing.Printing
Imports System.Management
Imports System.Threading
Imports System.Runtime.InteropServices
Imports System.Timers

Public Class frmMain

' [...] 
' ATTENTION, MORE CODE IS NEEDED TO RUN GECKOFX WITH AN URL BUT NOT DISPLAYED HERE AT THIS POINT, 
' SINCE IT ISN'T NEEDED HERE TO SHOW THE ACTUAL PROBLEM

''' <summary>
''' Startup-Functionalities, such as Gecko Xpcom-Start etc.
''' </summary>
''' <remarks></remarks>
Public Sub New()
    ' call initiliazer
    InitializeComponent()
    AddHandler Gecko.LauncherDialog.Download, AddressOf Me.LauncherDialog_Download
End Sub

''' <summary>
''' see also
''' http://quabr.com/19906621/how-to-handle-downloads-on-gecko15-with-mozilla-xul15-in-visual-basic
''' or
''' http://***.com/questions/19906621/how-to-handle-downloads-on-gecko15-with-mozilla-xul15-in-visual-basic
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Public Sub LauncherDialog_Download(ByVal sender As Object, ByVal e As Gecko.LauncherDialogEvent)

    Try
        Dim P As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & Path.DirectorySeparatorChar & "tmp" 'globalParameters._downloadDirectory '
        If Not System.IO.Directory.Exists(P) Then System.IO.Directory.CreateDirectory(P)

        Dim objTarget As nsILocalFile = Xpcom.CreateInstance(Of nsILocalFile)("@mozilla.org/file/local;1")

        Using tmp As New nsAString(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + vbTab & "temp.tmp")
            objTarget.InitWithPath(tmp)
        End Using

        If globalParameters._doNotShowDownloadPrompt Then
            'only if user does not want to load saveFileDialog; not interesting at this point
        Else
            'Save file dialog
            Dim saveFileDialog1 As New SaveFileDialog()
            saveFileDialog1.Filter = "CSV file (*.csv)|*.csv|All files (*.*)|*.*"
            saveFileDialog1.FilterIndex = 2
            saveFileDialog1.RestoreDirectory = False
            saveFileDialog1.FileName = e.Filename
            saveFileDialog1.AutoUpgradeEnabled = False
            saveFileDialog1.CheckPathExists = False
            saveFileDialog1.InitialDirectory = globalParameters.getDownloadDirectory() 'globalParameters._downloadDirectory

            Dim dialogResultValue As DialogResult
            Try
                dialogResultValue = saveFileDialog1.ShowDialog(Me)
            Catch ex As Exception
                logging.logInformation("Probleme beim laden des Dialogs: " & ex.ToString())
            End Try

            If dialogResultValue = DialogResult.OK Then
                Try
                    Dim par As New Parameters
                    par.sender = sender
                    par.e = e
                    par.mime = e.Mime
                    par.url = e.Url
                    par.fileName = saveFileDialog1.FileName
                    par.dialogResultValue = dialogResultValue
                    par.myStream = saveFileDialog1.OpenFile()
                    modMain.ThreadJob(par)
                Catch ex As Exception
                    logging.logInformation("Error during loading File" & e.ToString)
                End Try
            End If
        End If

    Catch ex As Exception
        logging.logInformation("Error during loading File" & ex.ToString)
    Finally
        ' nothing to to here
    End Try
End Sub


Private Sub frmMain_Disposed(sender As Object, e As EventArgs) Handles Me.Disposed
    RemoveHandler Gecko.LauncherDialog.Download, AddressOf Me.LauncherDialog_Download
End Sub
End Class

我希望我可以为其他搜索此问题的人正确描述该问题

【讨论】:

以上是关于SaveFileDialog 在调用 showDialog() 后直接自动关闭的主要内容,如果未能解决你的问题,请参考以下文章

使用 GetSaveFileName 创建 SaveFileDialog 时如何停止覆盖提示

从 SaveFileDialog 创建、写入和打开文本文件

SaveFileDialog c#的默认文件名

带有 Savefiledialog 的 *** 异常

如果取消,为啥我的 SaveFileDialog 会再次显示?

禁用部分 SaveFileDialog