代码之家  ›  专栏  ›  技术社区  ›  Crezzer7

使用本地属性和文件位置将图像添加到光开关

  •  0
  • Crezzer7  · 技术社区  · 11 年前

    只是一个简单的问题,有可能在lightswitch上的屏幕上显示静态图像吗?

    我要单击“添加数据项”->选择“本地属性”并键入“图像”。现在,与以前的版本不同,我无法选择文件路径,因此我需要通过postRender部分编写一些js,我应该在这里输入什么?

    谢谢你给我的任何帮助,尝试了一些方法,但没有成功。

    1 回复  |  直到 11 年前
        1
  •  1
  •   Community CDub    8 年前

    最近解决了类似的情况,我们实现了以下helper 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();
    };
    

    添加了一个本地属性(添加数据项->本地属性->类型:图像->,名称:图像属性)并将其放置到内容项树中后,promise操作可以在_postRender例程中以以下方式执行:-

    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标头以允许跨域访问)。

    编辑:我已经更新了这个助手函数,以稍微改进它,以回应这篇文章 Adding static image to Lightswitch HTML 2013 Browse Screen .