OpenFileDialog.ShowDialog()抛出异常? [英] OpenFileDialog.ShowDialog() throws an exception?

查看:901
本文介绍了OpenFileDialog.ShowDialog()抛出异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是我的代码:

 公共ReadOnly属性OpenParser作为ICommand 
获取
返回新的RelayCommand(Sub(param As Object)OpenParserExecute(DirectCast(param,Frame)) )
结束Get
结束属性

Public Sub OpenParserExecute(ByVal mFrame As Frame)
SaveParserExecute()
Dim mOpenDialog As OpenFileDialog = OpenDialog
如果mOpenDialog.ShowDialog()然后'线条抛出异常
CurrentParser =新的ParserEditorModel(mOpenDialog.FileName)
mFrame.Navigate(新的ParserEditor(CurrentParser))
结束如果
End Sub

StackTrace请求:

 在MS.Internal.Interop.HRESULT.ThrowIf失败(字符串消息)
在MS.Internal.AppModel.ShellUtil.GetShellItemForPath(字符串路径)
在Microsoft.Win32.FileDialog.PrepareVistaDialog(IFileDialog对话框)
在Microsoft.Win32.FileDialog。在Microsoft.Win32.CommonDialog.ShowDialog()
在WinTransform.GUI.MainWindowModel.OpenParserExecute(框架mFrame)中的$ Microsoft $ WinCE )在C:\Users\Alex\Desktop\MEDLI\branches\WinTransform\GUI\ViewModels\MainWindowModel.vb:行38


解决方案

因为 ShowDialog()本身返回一个 Nullable(Of Boolean)和你的如果语句期待一个非Nullable Boolean



您必须将对话框的返回值转换为布尔值,如果它是 True 通过对话框的文件名检索所选文件财产。



示例


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

解决方案

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.

这篇关于OpenFileDialog.ShowDialog()抛出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆