簡體   English   中英

“System.Nullable <bool> &#39;不包含&#39;確定&#39;的定義

[英]'System.Nullable<bool>' does not contain a definition for 'OK'

我正在嘗試一個基本的文件對話框示例, 在這里 ,我收到一個關於'確定'的錯誤,我不知道為什么。

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

.NET框架中有兩個版本的OpenFileDialogWinForms oneWPF one 看起來你正在使用WPF,實際上,它確實從OpenFile返回Nullable<bool>值。 WinForm版本返回一個DialogResult值,這似乎是您所期望的。

聽起來你有一個名為DialogResult的本地屬性。 請嘗試使用System.Windows.Forms.DialogResult.OK

看起來它試圖將ShowDialog用於System.Windows.Controls 嘗試將調用顯式System.Windows.Forms

喜歡:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM