我尝试,当我在 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/