本文演示如何在 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。
“打开文件夹”对话框
重要
.NET 8.0 及更高版本中提供了“打开文件夹”对话框。
用户使用“打开文件夹”对话框选择一个或多个文件夹,并将其返回到程序。 例如,如果程序显示有关文件夹的信息,例如文件夹中的文件量和文件名,则可以使用“打开文件夹”对话框让用户选择文件夹。
公共打开文件夹对话框作为 OpenFolderDialog 类实现,位于命名空间中 Microsoft.Win32 。 以下代码演示如何创建、配置和显示对话框。
// Configure open folder dialog box
Microsoft.Win32.OpenFolderDialog dialog = new();
dialog.Multiselect = false;
dialog.Title = "Select a folder";
// Show open folder dialog box
bool? result = dialog.ShowDialog();
// Process open folder dialog box results
if (result == true)
{
// Get the selected folder
string fullPathToFolder = dialog.FolderName;
string folderNameOnly = dialog.SafeFolderName;
}
' Configure open folder dialog box
Dim dialog As New Microsoft.Win32.OpenFolderDialog()
dialog.Multiselect = True
dialog.Title = "Select a folder"
' Show open folder dialog box
Dim result As Boolean? = dialog.ShowDialog()
' Process open folder dialog box results
If result = True Then
' Get multiple folder names
For index = 0 To dialog.FolderNames.Length
' Get the selected folder
Dim fullPathToFolder As String = dialog.FolderNames(index)
Dim folderNameOnly As String = dialog.SafeFolderNames(index)
Next
End If
有关打开文件夹对话框的详细信息,请参阅 Microsoft.Win32.OpenFolderDialog。
“打印”对话框
打印对话框由打印功能用来选择和配置用户希望将数据打印到的打印机。
通用打印对话框作为 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 中打印的详细讨论,请参阅 打印概述。