简体   繁体   English

OpenFileDialog.ShowDialog()引发异常?

[英]OpenFileDialog.ShowDialog() throws an exception?

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? 我试图从我的WPF视图模型命令之一显示一个对话框,但是当我调用ShowDialog()它抛出System.ArgumentException ,我想知道是否有人可以给我提示为什么?

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: 根据要求的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

Because ShowDialog() itself returns a Nullable(Of Boolean) and your If statement is expecting a non-Nullable Boolean . 因为ShowDialog()本身返回Nullable(Of Boolean)并且If语句期望使用非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. 您必须将对话框的返回值转换为Boolean ,如果为True ,则可以通过对话框的Filename属性检索所选文件。

Example . 例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM