2

我最近开始使用 HTML/CSS/JS 开发 Win8 应用程序。2 周后,出现了一个关于使用 WinJS 进行数据绑定的问题:

是否可以通过 WinJS 执行数据绑定,其功能与例如 knockout.js 一样强大(WinJS 可以做所有淘汰赛可以做的事情)?

ko.applyBindings()如果是这样,我将如何仅使用 WinJS执行淘汰赛?

4

3 回答 3

1

以下帖子中包含两种方法的完整示例:

http://www.progware.org/Blog/post/Data-binding-in-Windows-8-Apps-with-Knockout.aspx http://www.progware.org/Blog/post/Data-binding-in -Windows-81-Apps-with-WinJS.aspx

两种方法都应用于同一个 ViewModel,并且一切都得到了支持(双向绑定、转换器等)。

于 2013-12-13T19:14:57.047 回答
1

虽然“一样强大”是相当主观的,但 WinJS 有一个非常强大的绑定引擎。它不一样,但与 Knockout 有一些共同点。它以某些方式与以前称为 Microsoft ASP.NET Ajax 库的方式共享一些细节。

这是他们的一个例子:

<div id="boundDiv" data-win-bind="innerText: age"></div>
<script type="text/javascript">
    var person = { age: 0 };
    var span = document.getElementById("boundSpan");
    WinJS.Binding.processAll(span, person);
    var bindingPerson = WinJS.Binding.as(person);

    setInterval(function () {
        changeAge(bindingPerson);
    }, 500);

function changeAge(p) {
    p.age++;
};
</script>

每 500 毫秒,div将更新一次以age反映person. 由于它利用了 ECMAScript 5 的 Object 属性 getter 和 setter,可以轻松跟踪 age 的值,然后在div.

于 2013-10-27T20:03:42.370 回答
0

One thing that's unfortuantely missing in WinJS is two-way databinding. So you have to write some code to get user input in the ui back into your view model.

So you could either implement change handlers for your controls like in the SDK Sample or use the more generic solution form the expression blend blog

于 2013-10-28T10:26:28.310 回答