mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-02 06:32:10 +08:00
141 lines
4.2 KiB
Plaintext
141 lines
4.2 KiB
Plaintext
---
|
|
title: 对话框
|
|
sidebar_position: 5
|
|
---
|
|
|
|
# 对话框
|
|
|
|
## 概述
|
|
|
|
运行时的这一部分提供对原生对话框的调用,例如文件选择器和消息框。上下文
|
|
|
|
:::info Javascript
|
|
JS 运行时当前不支持 Dialog。
|
|
:::
|
|
|
|
### 打开选择目录对话框
|
|
|
|
Go 方法签名: `OpenDirectoryDialog(ctx context.Context, dialogOptions OpenDialogOptions) (string, error)`
|
|
|
|
返回值: 所选目录(如果用户取消则为空白)或错误
|
|
|
|
打开一个对话框,提示用户选择目录。可以使用 [打开选择文件对话框参数选项](#打开选择文件对话框参数选项)进行自定义。
|
|
|
|
### 打开选择文件对话框
|
|
|
|
Go 方法签名: `OpenFileDialog(ctx context.Context, dialogOptions OpenDialogOptions) (string, error)`
|
|
|
|
返回值: 所选文件(如果用户取消则为空白)或错误
|
|
|
|
打开一个对话框,提示用户选择文件。可以使用[打开选择文件对话框参数选项](#打开选择文件对话框参数选项)进行自定义。
|
|
|
|
### 打开选择多个文件对话框
|
|
|
|
Go 方法签名: `OpenMultipleFilesDialog(ctx context.Context, dialogOptions OpenDialogOptions) ([]string, error)`
|
|
|
|
返回值: 选定的文件(如果用户取消则为零)或错误
|
|
|
|
打开一个对话框,提示用户选择多个文件。可以使用[打开选择文件对话框参数选项](#打开选择文件对话框参数选项)进行自定义。
|
|
|
|
### 保存文件对话框
|
|
|
|
Go 方法签名: `SaveFileDialog(ctx context.Context, dialogOptions SaveDialogOptions) (string, error)`
|
|
|
|
返回值: 所选文件(如果用户取消则为空白)或错误
|
|
|
|
打开一个对话框,提示用户选择文件名以进行保存。可以使用[保存文件对话框参数选项](#保存文件对话框参数选项)自定义。
|
|
|
|
### 消息对话框
|
|
|
|
Go 方法签名: `MessageDialog(ctx context.Context, dialogOptions MessageDialogOptions) (string, error)`
|
|
|
|
返回值: 所选按钮的文本或错误
|
|
|
|
使用消息对话框显示消息。可以使用[消息对话框参数选项](#消息对话框参数选项)进行自定义。
|
|
|
|
## 参数选项
|
|
|
|
### 打开选择文件对话框参数选项
|
|
|
|
```go
|
|
type OpenDialogOptions struct {
|
|
DefaultDirectory string
|
|
DefaultFilename string
|
|
Title string
|
|
Filters []FileFilter
|
|
AllowFiles bool // Mac Only
|
|
AllowDirectories bool // Mac Only
|
|
ShowHiddenFiles bool // Mac Only
|
|
CanCreateDirectories bool // Mac Only
|
|
ResolvesAliases bool // Mac Only
|
|
TreatPackagesAsDirectories bool // Mac Only
|
|
}
|
|
```
|
|
|
|
### 保存文件对话框参数选项
|
|
|
|
```go
|
|
type SaveDialogOptions struct {
|
|
DefaultDirectory string
|
|
DefaultFilename string
|
|
Title string
|
|
Filters []FileFilter
|
|
ShowHiddenFiles bool // Mac Only
|
|
CanCreateDirectories bool // Mac Only
|
|
TreatPackagesAsDirectories bool // Mac Only
|
|
}
|
|
```
|
|
|
|
### 消息对话框参数选项
|
|
|
|
```go
|
|
type MessageDialogOptions struct {
|
|
Type DialogType
|
|
Title string
|
|
Message string
|
|
Buttons []string
|
|
DefaultButton string // Mac Only
|
|
CancelButton string // Mac Only
|
|
Icon string // Mac Only
|
|
}
|
|
```
|
|
|
|
import Tabs from "@theme/Tabs";
|
|
import TabItem from "@theme/TabItem";
|
|
|
|
<Tabs
|
|
defaultValue="Windows"
|
|
values={[
|
|
{ label: "Windows", value: "Windows" },
|
|
{ label: "MacOS", value: "MacOS" },
|
|
{ label: "Linux", value: "Linux" },
|
|
]}
|
|
>
|
|
<TabItem value="Windows">
|
|
Windows
|
|
具有标准对话框类型并且按钮不可定制。返回的值将是以下之一:“确定”、“取消”、“中止”、“重试”、“忽略”、“是”、“否”、“再试一次”或“继续”
|
|
</TabItem>
|
|
<TabItem value="MacOS">“DefaultButton”和“CancelButton”都应与“Buttons”中的值匹配。</TabItem>
|
|
<TabItem value="Linux">即将推出...</TabItem>
|
|
</Tabs>
|
|
|
|
#### 对话框类型
|
|
|
|
```go
|
|
const (
|
|
InfoDialog DialogType = "info"
|
|
WarningDialog DialogType = "warning"
|
|
ErrorDialog DialogType = "error"
|
|
QuestionDialog DialogType = "question"
|
|
)
|
|
```
|
|
|
|
### 文件过滤
|
|
|
|
```go
|
|
type FileFilter struct {
|
|
DisplayName string // Filter information EG: "Image Files (*.jpg, *.png)"
|
|
Pattern string // semi-colon separated list of extensions, EG: "*.jpg;*.png"
|
|
}
|
|
```
|