Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript WinJS自定义控件事件“;无效参数";_Javascript_Javascript Events_Winjs - Fatal编程技术网

Javascript WinJS自定义控件事件“;无效参数";

Javascript WinJS自定义控件事件“;无效参数";,javascript,javascript-events,winjs,Javascript,Javascript Events,Winjs,我正在创建一个带有事件侦听器的自定义WinJS控件。为简单起见,此示例应在每次点击时触发事件 这是使用以下标记创建的: <div class="alphaNavBar" data-win-control="MobMan.Controls.AlphaNavBar"></div> 本次活动的听众包括: var alphaNavBar = document.querySelector(".alphaNavBar"); alphaNavBar.addEventListener(

我正在创建一个带有事件侦听器的自定义WinJS控件。为简单起见,此示例应在每次点击时触发事件

这是使用以下标记创建的:

<div class="alphaNavBar" data-win-control="MobMan.Controls.AlphaNavBar"></div>
本次活动的听众包括:

var alphaNavBar = document.querySelector(".alphaNavBar");
alphaNavBar.addEventListener("mySelectionChanged", function (evt) {
    // Should fire when alphaNavBar is tapped
    debugger;
});
我做错了什么?提前感谢。

我也发布了我的问题,并得到了修改事件调度的答案,如下所示:

// Listen for tap
this._element.addEventListener("MSPointerDown", function (evt) {
    // Toggle selection
    this._selection = !this._selection;

    // Create the event.
    var _event = document.createEvent('customevent');

    // Define that the event name is 'mySelectionChanged' and pass details.
    _event.initCustomEvent('mySelectionChanged', true, true, { selection: this._selection });

    // Selection changed, fire event
    this.dispatchEvent(_event);
});

这能够为我正确地触发事件。仍然不确定我以前做错了什么,但现在已经修复。

您的代码看起来很好。WinJS作为源代码存在于您的项目中,因此您应该能够在调试器中直接进入DOMEventMixin.dispatchEvent,并更具体地查看异常来自何处。@KraigBrockschmidt MSFT在dispatchEvent(…)调用中单步执行会立即抛出错误,它不会转到任何可能导致问题的行。错误发生后,我进入
窗口。\u浏览器工具\u控制台\u SAFEFUNC(fn,safeAssert)
。单步执行任何操作都会导致调试程序和应用程序崩溃。请尝试调用self.dispatchEvent而不是self.\u element.dispatchEvent。后者直接调用DOM API,前者通过WinJS mixin中的方法——我以前应该注意到这种区别。如果它仍然崩溃,ad不允许您通过调试器介入,请直接打开WinJS base.js文件(在VS的解决方案资源管理器中的引用下),并在DOMEventMixin.dispatchEvent.上设置一个断点。将其更改为self.dispatchEvent(…)和this.dispatchEvent(…)都会产生与我得到的相同的无效参数错误。我查看了base.js,在我能找到的每个dispatchEvent(…)函数中都放置了一个断点,但在触发任何断点之前仍然会抛出错误。奇怪。你有没有可能把复制样品放在某个地方,这样我就可以直接通过?
// Listen for tap
this._element.addEventListener("MSPointerDown", function (evt) {
    // Toggle selection
    this._selection = !this._selection;

    // Create the event.
    var _event = document.createEvent('customevent');

    // Define that the event name is 'mySelectionChanged' and pass details.
    _event.initCustomEvent('mySelectionChanged', true, true, { selection: this._selection });

    // Selection changed, fire event
    this.dispatchEvent(_event);
});