# dialog
`dialog` 模塊提供了api來展示原生的系統對話框,例如打開文件框,alert框,所以web應用可以給用戶帶來跟系統應用相同的體驗.
對話框例子,展示了選擇文件和目錄:
```javascript
var win = ...; // BrowserWindow in which to show the dialog
const dialog = require('electron').dialog;
console.log(dialog.showOpenDialog({ properties: [ 'openFile', 'openDirectory', 'multiSelections' ]}));
```
**OS X 上的注意事項**: 如果你想像sheets一樣展示對話框,只需要在`browserWindow` 參數中提供一個 `BrowserWindow` 的引用對象.
## 方法
`dialog` 模塊有以下方法:
### `dialog.showOpenDialog([browserWindow, ]options[, callback])`
* `browserWindow` BrowserWindow (可選)
* `options` Object
* `title` String
* `defaultPath` String
* `filters` Array
* `properties` Array - 包含了對話框的特性值, 可以包含 `openFile`, `openDirectory`, `multiSelections` and
`createDirectory`
* `callback` Function (可選)
成功使用這個方法的話,就返回一個可供用戶選擇的文件路徑數組,失敗返回 `undefined`.
`filters` 當需要限定用戶的行為的時候,指定一個文件數組給用戶展示或選擇. 例如:
```javascript
{
filters: [
{ name: 'Images', extensions: ['jpg', 'png', 'gif'] },
{ name: 'Movies', extensions: ['mkv', 'avi', 'mp4'] },
{ name: 'Custom File Type', extensions: ['as'] },
{ name: 'All Files', extensions: ['*'] }
]
}
```
`extensions` 數組應當只包含擴展名,不應該包含通配符或'.'號 (例如
`'png'` 正確,但是 `'.png'` 和 `'*.png'` 不正確). 展示全部文件的話, 使用
`'*'` 通配符 (不支持其他通配符).
如果 `callback` 被調用, 將異步調用 API ,并且結果將用過 `callback(filenames)` 展示.
**注意:** 在 Windows 和 Linux ,一個打開的 dialog 不能既是文件選擇框又是目錄選擇框, 所以如果在這些平臺上設置 `properties` 的值為
`['openFile', 'openDirectory']` , 將展示一個目錄選擇框.
### `dialog.showSaveDialog([browserWindow, ]options[, callback])`
* `browserWindow` BrowserWindow (可選)
* `options` Object
* `title` String
* `defaultPath` String
* `filters` Array
* `callback` Function (可選)
成功使用這個方法的話,就返回一個可供用戶選擇的文件路徑數組,失敗返回 `undefined`.
`filters` 指定展示一個文件類型數組, 例子
`dialog.showOpenDialog` .
如果 `callback` 被調用, 將異步調用 API ,并且結果將用過 `callback(filenames)` 展示.
### `dialog.showMessageBox([browserWindow, ]options[, callback])`
* `browserWindow` BrowserWindow (可選)
* `options` Object
* `type` String - 可以是 `"none"`, `"info"`, `"error"`, `"question"` 或
`"warning"`. 在 Windows, "question" 與 "info" 展示圖標相同, 除非你使用 "icon" 參數.
* `buttons` Array - buttons 內容,數組.
* `defaultId` Integer - 在message box 對話框打開的時候,設置默認button選中,值為在 buttons 數組中的button索引.
* `title` String - message box 的標題,一些平臺不顯示.
* `message` String - message box 內容.
* `detail` String - 額外信息.
* `icon` [NativeImage](native-image.md)
* `cancelId` Integer - 當用戶關閉對話框的時候,不是通過點擊對話框的button,就返回值.默認值為對應 "cancel" 或 "no" 標簽button 的索引值, 或者如果沒有這種button,就返回0. 在 OS X 和 Windows 上, "Cancel" button 的索引值將一直是 `cancelId`, 不管之前是不是特別指出的.
* `noLink` Boolean - 在 Windows ,Electron 將嘗試識別哪個button 是普通 button (如 "Cancel" 或 "Yes"), 然后再對話框中以鏈接命令(command links)方式展現其它的 button . 這能讓對話框展示得很炫酷.如果你不喜歡這種效果,你可以設置 `noLink` 為 `true`.
* `callback` Function
展示 message box, 它會阻塞進程,直到 message box 關閉為止.返回點擊按鈕的索引值.
如果 `callback` 被調用, 將異步調用 API ,并且結果將用過 `callback(response)` 展示.
### `dialog.showErrorBox(title, content)`
展示一個傳統的包含錯誤信息的對話框.
在 `app` 模塊觸發 `ready` 事件之前,這個 api 可以被安全調用,通常它被用來在啟動的早期階段報告錯誤. 在 Linux 上,如果在 `app` 模塊觸發 `ready` 事件之前調用,message 將會被觸發顯示stderr,并且沒有實際GUI 框顯示.
- 介紹
- 常見問題
- Electron 常見問題
- 向導
- 支持平臺
- 分發應用
- 提交應用到 Mac App Store
- 打包應用
- 使用 Node 原生模塊
- 主進程調試
- 使用 Selenium 和 WebDriver
- 使用開發人員工具擴展
- 使用 Pepper Flash 插件
- 使用 Widevine CDM 插件
- 教程
- 快速入門
- 桌面環境集成
- 在線/離線事件探測
- API文檔
- 簡介
- 進程對象
- 支持的 Chrome 命令行開關
- 環境變量
- 自定義的 DOM 元素
- File 對象
- <webview> 標簽
- window.open 函數
- 在主進程內可用的模塊
- app
- autoUpdater
- BrowserWindow
- contentTracing
- dialog
- globalShortcut
- ipcMain
- Menu
- MenuItem
- powerMonitor
- powerSaveBlocker
- protocol
- session
- webContents
- Tray
- 在渲染進程(網頁)內可用的模塊
- desktopCapturer
- ipcRenderer
- remote
- webFrame
- 在兩種進程中都可用的模塊
- clipboard
- crashReporter
- nativeImage
- screen
- shell
- 開發
- 代碼規范
- 源碼目錄結構
- 與 NW.js(原 node-webkit)在技術上的差異
- 構建系統概覽
- 構建步驟(OS X)
- 構建步驟(Windows)
- 構建步驟(Linux)
- 在調試中使用 Symbol Server