我有一个输入框,要求用户在我的扩展程序中输入他们的用户名。我希望当用户单击 esc 或取消扩展时停止运行。
这是我到目前为止尝试过的,但没有运气(似乎 onCancellationRequested
期待的是一个事件而不是一个方法..
await window.showInputBox(
{prompt: "UserName: ",
placeHolder: "UserName"},
{isCancellationRequested: true, onCancellationRequested: (cancelled)=> {throw new Error('cancelled')}})
最佳答案
这不是 CancellationToken
的用途。它的作用与您想要的几乎相反,它可用于在用户执行任何输入之前以编程方式取消输入弹出窗口。
如果想知道用户是否取消了输入框,需要检查showInputBox()
docs中提到的Thenable
的返回值:
The returned value will be undefined if the input box was canceled (e.g. pressing ESC). [...]
vscode.window.showInputBox({prompt: 'UserName', placeHolder: 'UserName'}).then(value => {
if (value === undefined) {
throw new Error('cancelled');
}
// handle valid values
});
关于javascript - VScode 扩展向 showInputBox 添加取消事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57054786/