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

前端之家收集整理的这篇文章主要介绍了c# – ‘System.Nullable’不包含’OK’的定义前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试从 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?)

  1. private void button1_Click(object sender,System.EventArgs e)
  2. {
  3. Stream myStream = null;
  4. OpenFileDialog openFileDialog1 = new OpenFileDialog();
  5.  
  6. openFileDialog1.InitialDirectory = "c:\\" ;
  7. openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
  8. openFileDialog1.FilterIndex = 2 ;
  9. openFileDialog1.RestoreDirectory = true ;
  10.  
  11. if(openFileDialog1.ShowDialog() == DialogResult.OK)
  12. {
  13. try
  14. {
  15. if ((myStream = openFileDialog1.OpenFile()) != null)
  16. {
  17. using (myStream)
  18. {
  19. // Insert code to read the stream here.
  20. }
  21. }
  22. }
  23. catch (Exception ex)
  24. {
  25. MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
  26. }
  27. }
  28. }

解决方法

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

猜你在找的C#相关文章