Answers:
到目前为止,这两个答案都链接到Silverlight SaveFileDialog
类。在WPF变种是相当多的不同和不同的命名空间。
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".text"; // Default file extension
dlg.Filter = "Text documents (.txt)|*.txt"; // 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;
}
到目前为止,所有示例都使用Win32命名空间,但还有另一种选择:
FileInfo file = new FileInfo("image.jpg");
var dialog = new System.Windows.Forms.SaveFileDialog();
dialog.FileName = file.Name;
dialog.DefaultExt = file.Extension;
dialog.Filter = string.Format("{0} images ({1})|*{1}|All files (*.*)|*.*",
file.Extension.Substring(1).Capitalize(),
file.Extension);
dialog.InitialDirectory = file.DirectoryName;
System.Windows.Forms.DialogResult result = dialog.ShowDialog(this.GetIWin32Window());
if (result == System.Windows.Forms.DialogResult.OK)
{
}
我正在使用扩展方法IWin32Window
从可视控件获取:
#region Get Win32 Handle from control
public static System.Windows.Forms.IWin32Window GetIWin32Window(this System.Windows.Media.Visual visual)
{
var source = System.Windows.PresentationSource.FromVisual(visual) as System.Windows.Interop.HwndSource;
System.Windows.Forms.IWin32Window win = new OldWindow(source.Handle);
return win;
}
private class OldWindow : System.Windows.Forms.IWin32Window
{
private readonly System.IntPtr _handle;
public OldWindow(System.IntPtr handle)
{
_handle = handle;
}
System.IntPtr System.Windows.Forms.IWin32Window.Handle
{
get { return _handle; }
}
}
#endregion
Capitalize()
也是一种扩展方法,但不值得一提,因为这里有很多将首字母大写的例子。
GetIWin32Window
?