只是一个简单的问题,是否可以在灯光开关的屏幕上显示静态图像?
我想单击“添加数据项”-> 选择“本地属性”并输入“图像”。现在与以前的版本不同,我无法选择文件路径,因此我需要通过渲染后部分编写一些js,我在这里输入什么?
感谢您给我的任何帮助,尝试了一些方法但没有成功。
最佳答案
最近解决了类似的情况,我们实现了以下辅助 promise 操作功能:-
function GetImageProperty (operation) {
var image = new Image();
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
// XMLHttpRequest used to allow external cross-domain resources to be processed using the canvas.
// unlike crossOrigin = "Anonymous", XMLHttpRequest works in IE10 (important for LightSwitch)
// still requires the appropriate server-side ACAO header (see https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image)
var xhr = new XMLHttpRequest();
xhr.onload = function () {
var url = URL.createObjectURL(this.response);
image.onload = function () {
URL.revokeObjectURL(url);
canvas.width = image.width;
canvas.height = image.height;
ctx.drawImage(image, 0, 0);
var dataURL = canvas.toDataURL("image/png");
operation.complete(dataURL.substring(dataURL.indexOf(",") + 1));
};
image.src = url;
};
xhr.open('GET', this.imageSource, true);
xhr.responseType = 'blob';
xhr.send();
};
添加本地属性(添加数据项 -> 本地属性 -> 类型:图像 -> 名称:ImageProperty)并将其放入内容项树中后,可以在 _postRender 例程中按以下方式执行 Promise 操作: -
msls.promiseOperation
(
$.proxy(
GetImageProperty,
{ imageSource: "content/images/user-splash-screen.png" }
)
).then(
function (result) {
contentItem.screen.ImageProperty = result;
}
);
或者,可以在屏幕的创建函数中按如下方式调用:-
msls.promiseOperation
(
$.proxy(
GetImageProperty,
{ imageSource: "https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/pie.png" }
)
).then(
function (result) {
screen.ImageProperty = result;
}
);
上面的调用还表明,除了显示 LightSwitch 项目本地的图像之外,还可以将 imageSource 设置为外部 url(前提是外部站点使用适当的服务器端 ACAO header 以允许跨域访问) .
编辑:我已经更新了这个辅助函数以稍微改进它以回答这篇文章 Adding static image to Lightswitch HTML 2013 Browse Screen .
关于javascript - 使用本地属性和文件位置将图像添加到灯光开关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24863250/