加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – ‘System.Nullable’不包含’OK’的定义

发布时间:2020-12-15 18:27:25 所属栏目:百科 来源:网络整理
导读:我正在尝试从 here开始的基本文件对话框示例,我在’确定’上收到错误,我不知道为什么. Error 1 ‘System.Nullable’ does not contain a definition for ‘OK’ and no extension method ‘OK’ accepting a first argument of type ‘System.Nullable’ cou
我正在尝试从 here开始的基本文件对话框示例,我在’确定’上收到错误,我不知道为什么.

Error 1 ‘System.Nullable’ does not contain a definition for ‘OK’ and no extension method ‘OK’ accepting a first argument of type ‘System.Nullable’ could be found (are you missing a using directive or an assembly reference?)

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);
        }
    }
}

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读