c# - SaveFileDialog 上的 DialogResult.OK 不起作用

标签 c# wpf dialog savefiledialog dialogresult

我尝试,当我在 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”是一个“属性”,但像“类型”一样使用

最佳答案

我假设您指的是 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)
            {....}

关于c# - SaveFileDialog 上的 DialogResult.OK 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23539219/

相关文章:

wpf - 在 DataGrid 的单元格中设置 TextTrimming (CharacterEllipsis)

android - ProgressDialog 的自定义对话框问题

Javascript 关闭警告框

android - Google 帐户选择器未显示

c# - 实现一个支持实体对象延迟加载的存储库?

c# - Silverlight 的音频输出

c# - 如何使用 LINQ to Entities(带转换)返回嵌套数据集?

c# - 如何从 RichTextBox 中获取显示的文本?

c# - WPF ClickOnce 遗漏引用

wpf - 删除文本框内填充?