OpenFileDialog.ShowDialog()抛出异常?

时间:2011-06-21 23:54:15

标签: wpf vb.net exception .net-4.0

我正在尝试从我的一个WPF视图模型命令中显示一个对话框但是当我调用ShowDialog()它会抛出一个System.ArgumentException时,我想知道是否有人可以给我一个暗示为什么?

这是我的代码:

Public ReadOnly Property OpenParser As ICommand
    Get
        Return New RelayCommand(Sub(param As Object) OpenParserExecute(DirectCast(param, Frame)))
    End Get
End Property

Public Sub OpenParserExecute(ByVal mFrame As Frame)
    SaveParserExecute()
    Dim mOpenDialog As OpenFileDialog = OpenDialog
    If mOpenDialog.ShowDialog() Then ' Lines the throws the exception
        CurrentParser = New ParserEditorModel(mOpenDialog.FileName)
        mFrame.Navigate(New ParserEditor(CurrentParser))
    End If
End Sub

StackTrace按要求:

at MS.Internal.Interop.HRESULT.ThrowIfFailed(String message)
at MS.Internal.AppModel.ShellUtil.GetShellItemForPath(String path)
at Microsoft.Win32.FileDialog.PrepareVistaDialog(IFileDialog dialog)
at Microsoft.Win32.FileDialog.RunVistaDialog(IntPtr hwndOwner)
at Microsoft.Win32.FileDialog.RunDialog(IntPtr hwndOwner)
at Microsoft.Win32.CommonDialog.ShowDialog()
at WinTransform.GUI.MainWindowModel.OpenParserExecute(Frame mFrame) in C:\Users\Alex\Desktop\MEDLI\branches\WinTransform\GUI\ViewModels\MainWindowModel.vb:line 38

1 个答案:

答案 0 :(得分:3)

由于ShowDialog()本身会返回Nullable(Of Boolean),而您的If语句预计会出现非可空Boolean

您必须将对话框的返回值转换为Boolean,如果True通过对话框的Filename属性检索所选文件。

Example