我正在使用WinJS Windows Metro应用程序,并在我的一个页面上获取要显示为背景的图像的URL。通过使用url(图像的URL)并将其设置为style.backgroundImage,我可以很好地工作。在Windows Metro应用程序中缓存背景
我需要在链接页面上使用相同的图像,但这意味着我不得不做另一个HTTP请求,我试图避免。我查看了备选方案并找到了LocalFolder作为选项。唯一的问题是我不知道如何访问该文件并将其设置为背景。
这是正确的方式去缓存数据,以减少网络电话?
下面是我使用的代码:这取决于你想要的那种形象的转移和多少的这些
function saveBackground(url) {
localFolder.createFileAsync("background.jpg", Windows.Storage.CreationCollisionOption.replaceExisting).then(function (newFile) {
var uri = Windows.Foundation.Uri(url);
var downloader = new Windows.Networking.BackgroundTransfer.BackgroundDownloader();
var promise = downloader.createDownload(uri, newFile);
promise.startAsync().then(function() {
//set background here.
var wrapper = document.getElementById("wrapper").style;
localFolder.getFileAsync("background.jpg").then(function (image) {
console.log(image.path);
var path = image.path.split("");
var newLocation = [];
//This is just to make the backslashes work out for the url()
for (var i = 0; i < path.length; i++) {
if (path[i] != '\\') {
newLocation.push(path[i]);
} else {
newLocation.push('\\\\');
}
}
console.log(newLocation);
var newPath = newLocation.join("");
var target = "url(" + newPath + ")";
wrapper.backgroundImage = target;
console.log(wrapper.backgroundImage);
wrapper.backgroundSize = "cover";
});
});
});
}