wpf SaveFileDialog 上的 DialogResult.OK 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23539219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:41:13  来源:igfitidea点击:

DialogResult.OK on SaveFileDialog not work

c#wpfdialogsavefiledialogdialogresult

提问by Hassan

I try, when I press save in SaveFileDialogI do something. I trying fix but always something wrong.

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

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

But I have error on OK - which say:

但是我在 OK 上有错误 - 其中说:

Error: 'System.Nullable' does not contain a definition for 'OK' and no extension method 'OK' accepting a first argument of type 'System.Nullable' could be found (are you missing a using directive or an assembly reference?)

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

I try fix with this code:

我尝试使用此代码修复:

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

Now error is on DialogResult say: 'System.Windows.Window.DialogResult' is a 'property' but is used like a 'type'

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

回答by Hassan

I assume that you are referring to WPFnot Windows FormHere is example of using SaveFileDialog

我假设您指的是WPFnot Windows FormHere is the example of usingSaveFileDialog

//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;
}

Other Example:

其他示例

In WPFyou have to handle conflict between DialogResultEnumeration and Window.DialogResultProperty

WPF你要把手之间的冲突DialogResult枚举和Window.DialogResult属性

Try using fully qualified name to refer the enumeration:

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

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

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

回答by Loran

DialogResultreturn System.Windows.Forms.DialogResult.So u can use like that=>

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

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