在用 JS 编写的 Windows 8 Metro 应用程序中,我打开一个文件,获取流,使用“promise - .then”模式向其中写入一些图像数据。它工作正常 - 文件成功保存到文件系统,除了使用 BitmapEncoder 将流刷新到文件后,流仍然打开。IE; 在我终止应用程序之前,我无法访问该文件,但“流”变量超出了我可以参考的范围,因此我无法关闭()它。有没有可以与 C# using 语句相媲美的东西?
...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.
};