2011-06-21 52 views
0

我試圖從我的WPF視圖模型命令中顯示一個對話框,但是當我打電話給ShowDialog()時它會拋出一個System.ArgumentException,我想知道是否有人可以給我一個提示,告訴我爲什麼?OpenFileDialog.ShowDialog()拋出異常?

這裏是我的代碼:根據要求

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 

堆棧跟蹤:

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 
+0

你的OpenDialog對象有問題。很難猜測它可能是什麼,除非你發佈了異常的StackTrace。 –

+0

添加了堆棧跟蹤。 –

+1

OpenDialog.InitialDirectory屬性有問題。 –

回答

3

因爲ShowDialog()本身返回Nullable(Of Boolean)和你If語句期待一個非空的Boolean

您必須將對話框的返回值轉換爲Boolean,如果它是True則通過對話框的Filename屬性檢索選定的文件。

Example