将资源数据绑定到自定义winjs控件

时间:2013-02-14 15:00:27

标签: winapi windows-8 windows-store-apps winjs

我正在尝试创建自定义占位符控件。以下是代码摘录:

(function () {
    var Placeholder = WinJS.Class.define(function ctor(elem, options) {
        this.element = elem || document.createElement('div');
        this.element.winControl = this;

        WinJS.UI.setOptions(this, options);

        this.init();
    },
    {
        element: {
            get: function () {
                return this._element;
            },
            set: function (value) {
                this._element = value;
            }
        },

        placeholder: {
            get: function () {
                return this._placeholder;
            },
            set: function (value) {
                this._placeholder = value;

                this._placeholderElement.textContent = value;
            }
        },

        placeholderClass: {
            get: function () {
                return this._placeholderClass;
            },
            set: function (value) {
                this._placeholderClass = value;

                this._placeholderElement.classList.add(value);
            }
        },

        _placeholderElement: null,

        init: function () {
            this._addPlaceholder();
        },

        _addPlaceholder: function () {
            /* Code to add placeholder. */
        }
    });

    WinJS.Namespace.define("ControlX", { Placeholder: Placeholder });
})();

我试图以这种方式在html中使用这个控件:

<div data-win-control="ControlX.Placeholder" data-win-res="{winControl:{placeholder:'resHeight'}}"><input type="text"/></div>

当我使用data-win-res属性设置占位符值时,我得到一个例外:

  

“无法设置未定义或空引用的属性'占位符'”

根据这篇文章: How to localize WinJS controls我们也可以将资源字符串绑定到winControl属性。

我做错了什么?

有没有其他方法可以将资源字符串绑定到winControl属性?

1 个答案:

答案 0 :(得分:1)

我正在使用C#开发Windows 8应用程序。 XAML,所以不知道具体的解决方案是什么,但我认为这个回复可能有效:

我建议,不是仅仅在参数中创建内部类和定义方法,而是应该将每个方法和类分开并在一个方法中与给予该全新Custom WinJS Control的适当名称相结合。 在给出该控件的名称后,您应该通过它在DIV标签中的名称来调用它。

我不是很强大并且用它的代码练习,最好不要为代码服务,对不起。但我敢肯定,通过给出名字并通过这个名字打电话会有所帮助 所有最好的......

(请原谅,如果您认为这不是您的回答。但我认为这可能会有所帮助)

相关问题