OpenFileDialog.ShowDialog() throws an exception?

余生颓废 提交于 2019-12-04 06:42:26

问题


I am trying to show a dialog from one of my WPF view model commands however when i call ShowDialog() it throws a System.ArgumentException, I was wondering if anyone could give me a hint as to why?

Here is my code:

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 as requested:

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:


Because ShowDialog() itself returns a Nullable(Of Boolean) and your If statement is expecting a non-Nullable Boolean.

You'll have to cast the dialog's return value to a Boolean and if it's True retreive the selected file through your dialog's Filename property.

Example.



来源:https://stackoverflow.com/questions/6433373/openfiledialog-showdialog-throws-an-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!