【问题标题】:'System.Nullable<bool>' does not contain a definition for 'OK'“System.Nullable<bool>”不包含“OK”的定义
【发布时间】:2010-08-17 14:56:41
【问题描述】:

我正在尝试来自here 的基本文件对话框示例,但在“确定”时出现错误,我不知道为什么。

错误 1“System.Nullable”不包含“OK”的定义,并且找不到接受“System.Nullable”类型的第一个参数的扩展方法“OK”(您是否缺少 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);
        }
    }
}

【问题讨论】:

  • 您的 OpenFileDialog 来自哪个程序集?
  • 这行得通,你能更具体一点吗?错误可能不在这里...

标签: c# wpf file dialog


【解决方案1】:

.NET 框架中有两个版本的OpenFileDialogWinForms oneWPF one。看起来您正在使用 WPF,实际上,它确实从 OpenFile 返回了一个 Nullable&lt;bool&gt; 值。 WinForm 版本返回一个DialogResult 值,这似乎是您所期望的。

【讨论】:

  • 你链接的版本是WIN32的。我不认为有一个特定于 WPF。
  • @pug 请注意,有问题的类是在 PresentationFramework 程序集中定义的,它是 WPF 的核心程序集之一。
【解决方案2】:

听起来您有一个名为DialogResult 的本地属性。尝试改用System.Windows.Forms.DialogResult.OK

【讨论】:

    【解决方案3】:

    看起来它试图将ShowDialog 用于System.Windows.Controls。 尝试明确调用System.Windows.Forms

    喜欢:

    System.Windows.Forms.OpenFileDialog openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
    

    【讨论】:

      猜你喜欢
      • 2020-06-06
      • 1970-01-01
      • 1970-01-01
      • 2018-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多