回调函数未正确执行 - Outlook 加载项
Callback functions not executed correctly - Outlook add-in
我正在尝试创建一个 Outlook 加载项用于测试目的,但我遇到了一个奇怪的行为。我在 XML Manifest 中声明了以下键:
<FunctionFile resid="functionFile" />
<Action xsi:type="ExecuteFunction">
<FunctionName>myEntryPoint</FunctionName>
</Action>
'functionFile' 是我的 HTML 文件,其中包含 Javascript 代码。
在那个 HTML 文件中,我有以下代码:
(function () {
Office.initialize = function (reason) { //Nothing here };
})();
function myEntryPoint(event) {
Office.context.mailbox.displayNewMessageFormAsync({
toRecipients: ["firstname.name@email.com"],
subject: "Test Subject",
htmlBody: "Internet headers: ",
}, function (result) {
console.log(result);
});
event.completed();
}
事实上,方法displayNewMessageFormAsync被执行但没有执行回调(console.log(result))。
如果我将该代码放入 'Office.initialize' 函数中,就会执行回调函数。
有什么想法吗?
关于 Outlook 加载项团队 - MSFT 的评论:
you are calling event.completed() after calling displayNewMessageFormAsync(). Which means the callback is not guaranteed to run. You can try setting a global variable to event, and calling event.completed() in the callback. or pass the event via the asyncContext.
我修改了代码以在 asyncContext 中传递事件:
function myEntryPoint(event) {
Office.context.mailbox.displayNewMessageFormAsync({
toRecipients: ["firstname.name@email.com"],
subject: "Test Subject",
htmlBody: "Internet headers: ",
},
{ asyncContext : event },
function (result) {
console.log(result);
event.completed();
}
);
}
有效!谢谢。
我正在尝试创建一个 Outlook 加载项用于测试目的,但我遇到了一个奇怪的行为。我在 XML Manifest 中声明了以下键:
<FunctionFile resid="functionFile" />
<Action xsi:type="ExecuteFunction">
<FunctionName>myEntryPoint</FunctionName>
</Action>
'functionFile' 是我的 HTML 文件,其中包含 Javascript 代码。 在那个 HTML 文件中,我有以下代码:
(function () {
Office.initialize = function (reason) { //Nothing here };
})();
function myEntryPoint(event) {
Office.context.mailbox.displayNewMessageFormAsync({
toRecipients: ["firstname.name@email.com"],
subject: "Test Subject",
htmlBody: "Internet headers: ",
}, function (result) {
console.log(result);
});
event.completed();
}
事实上,方法displayNewMessageFormAsync被执行但没有执行回调(console.log(result))。
如果我将该代码放入 'Office.initialize' 函数中,就会执行回调函数。
有什么想法吗?
关于 Outlook 加载项团队 - MSFT 的评论:
you are calling event.completed() after calling displayNewMessageFormAsync(). Which means the callback is not guaranteed to run. You can try setting a global variable to event, and calling event.completed() in the callback. or pass the event via the asyncContext.
我修改了代码以在 asyncContext 中传递事件:
function myEntryPoint(event) {
Office.context.mailbox.displayNewMessageFormAsync({
toRecipients: ["firstname.name@email.com"],
subject: "Test Subject",
htmlBody: "Internet headers: ",
},
{ asyncContext : event },
function (result) {
console.log(result);
event.completed();
}
);
}
有效!谢谢。