DialogResult.OK在SaveFileDialog不工作

本文关键字:工作 SaveFileDialog OK DialogResult | 更新日期: 2023-09-27 18:02:24

我尝试,当我在SaveFileDialog中按下保存时,我做了一些事情。我试图解决,但总是有问题。

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

但是我有错误的OK -说:

错误: '系统。Nullable'不包含'OK'的定义,也没有扩展方法'OK'接受类型为'System '的第一个参数。

我试着用这个代码修复:

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

现在错误是在对话框sult说: System.Windows.Window。"dialgresult"是一个"属性",但它的使用方式与"type"类似

DialogResult.OK在SaveFileDialog不工作

我假设你指的是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;
}
<<p> 其他例子/strong>:

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

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

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

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

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

不检查dlg2.ShowDialog()是否等于dialgresult。检查它是否等于true

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

这是一个非常古老的话题,但我会给你解决方案。您正在寻找的对话框结果(对于savefiledialog)是。yes或!Cancel,因此它看起来像这样:

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

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