7

我尝试,当我按下保存时,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”是一个“属性”,但用作“类型”

4

4 回答 4

17

我假设你指的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)
            {....}
于 2014-05-08T10:50:34.697 回答
2

DialogResult返回System.Windows.Forms.DialogResult。所以你可以这样使用=>

DialogResult result = dlg2.ShowDialog(); 
if (result == System.Windows.Forms.DialogResult.OK)
                {....}
于 2019-02-21T08:29:33.657 回答
0

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

if (dlg2.ShowDialog() == true)
{....}
于 2020-06-12T14:18:37.353 回答
0

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

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

或者

if (dlg2.ShowDialog() != DialogResult.Cancel)
于 2020-09-19T14:11:49.357 回答