2
在用JS編寫的Windows 8 Metro應用程序中,我打開一個文件,獲取流,使用'promise - .then'模式向其中寫入一些圖像數據。它工作正常 - 文件已成功保存到文件系統,除了在使用BitmapEncoder刷新文件流之後,流仍處於打開狀態。即;在我殺死應用程序之前我無法訪問該文件,但'流'變量超出了我的引用範圍,所以我無法關閉它()。是否有可與C#使用語句相媲美的東西?在WinJS Metro應用程序中使用BitmapEncoder後關閉流
...then(function (file) {
return file.openAsync(Windows.Storage.FileAccessMode.readWrite);
})
.then(function (stream) {
//Create imageencoder object
return Imaging.BitmapEncoder.createAsync(Imaging.BitmapEncoder.pngEncoderId, stream);
})
.then(function (encoder) {
//Set the pixel data in the encoder ('canvasImage.data' is an existing image stream)
encoder.setPixelData(Imaging.BitmapPixelFormat.rgba8, Imaging.BitmapAlphaMode.straight, canvasImage.width, canvasImage.height, 96, 96, canvasImage.data);
//Go do the encoding
return encoder.flushAsync();
//file saved successfully,
//but stream is still open and the stream variable is out of scope.
};
看來,如果在第一個或第二個'then>的任何地方拋出錯誤,它將使流關閉代碼不執行。最後'then'應該有關閉流的錯誤處理程序。 – 2015-07-07 17:25:09