如何打开通用对话框 (WPF .NET)

本文演示如何在 Windows Presentation Foundation (WPF) 中显示通用系统对话框。 Windows 实现了所有应用程序通用的不同类型的可重用对话框,其中包括用于选择文件和打印的对话框。

由于这些对话框是由操作系统提供的,因此它们在操作系统上运行的所有应用程序之间共享。 这些对话框提供一致的用户体验,被称为通用对话框。 当用户在一个应用程序中使用通用对话框时,他们不需要学习如何在其他应用程序中使用该对话框。

消息框是另一种通用对话框。 有关详细信息,请参阅如何打开消息框

“打开文件”对话框

“打开文件”对话框由文件打开功能用于检索要打开文件的名称。

An Open dialog box showing the location to retrieve the file shown from a WPF application.

常见的打开文件对话框作为 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

保存文件对话框

“保存文件”对话框由文件保存功能用于检索要保存文件的名称。

A Save As dialog box showing the location to save the file shown from a WPF application.

常见的保存文件对话框作为 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 及更高版本中提供了“打开文件夹”对话框。

用户可使用“打开文件夹”对话框选择一个或多个文件夹,然后将其返回到程序。 例如,如果程序显示有关文件夹的信息,例如文件夹中的文件量和文件名,则可以使用“打开文件夹”对话框来支持用户选择文件夹。

An Open Folder dialog box showing the Pictures folder with the Camera Roll folder selected, shown from a WPF application.

常见的打开文件夹对话框是作为 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

打印对话框由打印功能用以选择和配置用户想要将数据打印到的打印机。

A print dialog box shown from a WPF application.

常见的打印对话框作为 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 中打印的详细讨论,请参阅打印概述

另请参阅