'System.Nullable <bool>'不包含'OK'的定义

rd4*_*d42 6 c# wpf dialog file

我正在尝试一个基本的文件对话框示例,在这里,我收到一个关于'确定'的错误,我不知道为什么.

错误1'System.Nullable'不包含'OK'的定义,并且没有扩展方法'OK'可以找到接受类型'System.Nullable'的第一个参数(你是否缺少using指令或汇编引用?)

private void button1_Click(object sender, System.EventArgs e)
{
    Stream myStream = null;
    OpenFileDialog openFileDialog1 = new OpenFileDialog();

    openFileDialog1.InitialDirectory = "c:\\" ;
    openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
    openFileDialog1.FilterIndex = 2 ;
    openFileDialog1.RestoreDirectory = true ;

    if(openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        try
        {
            if ((myStream = openFileDialog1.OpenFile()) != null)
            {
                using (myStream)
                {
                    // Insert code to read the stream here.
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*att 16

OpenFileDialog.NET框架中有两个版本:WinForms oneWPF one.看起来你正在使用WPF,实际上它确实Nullable<bool>从中返回一个值OpenFile.WinForm版本返回一个DialogResult值,这似乎是您所期望的.


Mar*_*ell 10

听起来你有一个叫当地的房产DialogResult.请尝试使用System.Windows.Forms.DialogResult.OK.