【问题标题】:DialogResult.OK on SaveFileDialog not workSaveFileDialog 上的 DialogResult.OK 不起作用
【发布时间】:2014-06-25 16:40:25
【问题描述】:

我尝试,当我在SaveFileDialog 中按保存时,我会做一些事情。我尝试修复,但总是有问题。

SaveFileDialog dlg2 = new SaveFileDialog();
dlg2.Filter = "xml | *.xml";
dlg2.DefaultExt = "xml";
dlg2.ShowDialog();
if (dlg2.ShowDialog() == DialogResult.OK)
{....}

但我在 OK 上有错误 - 说:

错误: “System.Nullable”不包含“OK”的定义,并且找不到接受“System.Nullable”类型的第一个参数的扩展方法“OK”(您是否缺少 using 指令或程序集引用? )

我尝试用这段代码修复:

DialogResult result = dlg2.ShowDialog(); //here is error again
if (result == DialogResult.OK)
                {....}

现在错误出现在 DialogResult 上说: 'System.Windows.Window.DialogResult' 是一个 'property' 但被用作一个 'type'

【问题讨论】:

标签: c# wpf dialog savefiledialog dialogresult


【解决方案1】:

我假设您指的是WPF 而不是Windows Form 这是使用SaveFileDialog的示例

//configure save file dialog box
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "Document"; //default file name
dlg.DefaultExt = ".xml"; //default file extension
dlg.Filter = "XML documents (.xml)|*.xml"; //filter files by extension

// Show save file dialog box
Nullable<bool> result = dlg.ShowDialog();

// Process save file dialog box results
if (result == true)
{
   // Save document
   string filename = dlg.FileName;
}

其他示例

WPF 中,您必须处理DialogResult 枚举和Window.DialogResult 属性之间的冲突

尝试使用完全限定名称来引用枚举:

System.Windows.Forms.DialogResult result = dlg2.ShowDialog();

if (result == DialogResult.OK)
            {....}

【讨论】:

    【解决方案2】:

    DialogResult return System.Windows.Forms.DialogResult.所以你可以这样使用=>

    DialogResult result = dlg2.ShowDialog(); 
    if (result == System.Windows.Forms.DialogResult.OK)
                    {....}
    

    【讨论】:

      【解决方案3】:

      不是检查 dlg2.ShowDialog() 是否等于 DialogResult.OK 而是检查它是否等于 true

      if (dlg2.ShowDialog() == true)
      {....}
      

      【讨论】:

        【解决方案4】:

        这是一个非常古老的话题,但我会为您提供解决方案。您正在寻找的对话框结果(对于 savefiledialog)是 .Yes 或 !Cancel 所以它看起来像这样:

        if (dlg2.ShowDialog() == DialogResult.Yes)
        

        if (dlg2.ShowDialog() != DialogResult.Cancel)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多