本文演示如何在 Windows Presentation Foundation (WPF) 中显示通用系统对话框。 Windows 实现了所有应用程序通用的不同类型的可重用对话框,包括用于选择文件和打印的对话框。
由于这些对话框是由操作系统提供的,因此它们在操作系统上运行的所有应用程序之间共享。 这些对话框提供一致的用户体验,被称为通用对话框。 当用户在一个应用程序中使用通用对话框时,他们不需要学习如何在其他应用程序中使用该对话框。
消息框是另一种通用对话框。 有关详细信息,请参阅
如何打开消息框
。
“打开文件”对话框
“打开文件”对话框由文件打开功能用于检索要打开文件的名称。
常见的打开文件对话框作为
OpenFileDialog
类实现,并位于
Microsoft.Win32
命名空间中。 以下代码显示了如何创建、配置和显示打开文件对话框以及如何处理结果。
// Configure open file dialog box
var dialog = new Microsoft.Win32.OpenFileDialog();
dialog.FileName = "Document"; // Default file name
dialog.DefaultExt = ".txt"; // Default file extension
dialog.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension
// Show open file dialog box
bool? result = dialog.ShowDialog();
// Process open file dialog box results
if (result == true)
// Open document
string filename = dialog.FileName;
' Configure open file dialog box
Dim dialog As New Microsoft.Win32.OpenFileDialog()
dialog.FileName = "Document" ' Default file name
dialog.DefaultExt = ".txt" ' Default file extension
dialog.Filter = "Text documents (.txt)|*.txt" ' Filter files by extension
' Show open file dialog box
Dim result As Boolean? = dialog.ShowDialog()
' Process open file dialog box results
If result = True Then
' Open document
Dim filename As String = dialog.FileName
End If
有关打开文件对话框的详细信息,请参阅
Microsoft.Win32.OpenFileDialog
。
保存文件对话框
“保存文件”对话框由文件保存功能用于检索要保存文件的名称。
常见的保存文件对话框作为
SaveFileDialog
类实现,并位于
Microsoft.Win32
命名空间中。 以下代码显示了如何创建、配置和显示打开文件对话框以及如何处理结果。
// Configure save file dialog box
var dialog = new Microsoft.Win32.SaveFileDialog();
dialog.FileName = "Document"; // Default file name
dialog.DefaultExt = ".txt"; // Default file extension
dialog.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension
// Show save file dialog box
bool? result = dialog.ShowDialog();
// Process save file dialog box results
if (result == true)
// Save document
string filename = dialog.FileName;
' Configure save file dialog box
Dim dialog As New Microsoft.Win32.SaveFileDialog()
dialog.FileName = "Document" ' Default file name
dialog.DefaultExt = ".txt" ' Default file extension
dialog.Filter = "Text documents (.txt)|*.txt" ' Filter files by extension
' Show save file dialog box
Dim result As Boolean? = dialog.ShowDialog()
' Process save file dialog box results
If result = True Then
' Save document
Dim filename As String = dialog.FileName
End If
有关保存文件对话框的详细信息,请参阅
Microsoft.Win32.SaveFileDialog
。
“打印”对话框
打印对话框由打印功能用以选择和配置用户想要将数据打印到的打印机。
常见的打印对话框作为
PrintDialog
类实现,并位于
System.Windows.Controls
命名空间中。 以下代码显示了如何创建、配置和显示打印对话框。
// Configure printer dialog box
var dialog = new System.Windows.Controls.PrintDialog();
dialog.PageRangeSelection = System.Windows.Controls.PageRangeSelection.AllPages;
dialog.UserPageRangeEnabled = true;
// Show save file dialog box
bool? result = dialog.ShowDialog();
// Process save file dialog box results
if (result == true)
// Document was printed
' Configure printer dialog box
Dim dialog As New System.Windows.Controls.PrintDialog()
dialog.PageRangeSelection = System.Windows.Controls.PageRangeSelection.AllPages
dialog.UserPageRangeEnabled = True
' Show save file dialog box
Dim result As Boolean? = dialog.ShowDialog()
' Process save file dialog box results
If result = True Then
' Document was printed
End If
有关打印对话框的详细信息,请参阅
System.Windows.Controls.PrintDialog
。 有关在 WPF 中打印的详细讨论,请参阅
打印概述
。
如何打开消息框
对话框概述
WPF 窗口概述
Microsoft.Win32.OpenFileDialog
Microsoft.Win32.SaveFileDialog
System.Windows.Controls.PrintDialog