简体   繁体   English

VScode 扩展为 showInputBox 添加取消事件

[英]VScode extension add cancellation event to showInputBox

I have an input box that asks the user to enter their username in my extension.我有一个输入框,要求用户在我的扩展程序中输入他们的用户名。 I would like that when user click on esc or cancel the extensions stop running.我希望当用户单击 esc 或取消扩展停止运行时。

This is what I tried so far but no luck (it seems that onCancellationRequested is expecting an Event and not a method..这是我到目前为止尝试过的但没有运气(似乎onCancellationRequested期待一个事件而不是一个方法..

await window.showInputBox(
{prompt: "UserName: ",
    placeHolder: "UserName"}, 
{isCancellationRequested: true, onCancellationRequested: (cancelled)=> {throw new Error('cancelled')}})

This is not what the CancellationToken is for.这不是CancellationToken的用途。 It does pretty much the opposite of what you want, it can be used to cancel the input popup programatically before ther user has performed any input.它几乎与您想要的相反,它可用于在用户执行任何输入之前以编程方式取消输入弹出窗口。

If you want to find out whether the user has cancelled the input box, you need to check the return value of the Thenable as mentioned in the showInputBox() docs :如果您想了解用户是否取消了输入框,您需要检查返回值Thenable如提及showInputBox()文档

The returned value will be undefined if the input box was canceled (eg pressing ESC).如果输入框被取消(例如按 ESC),则返回值将是未定义的。 [...] [...]

vscode.window.showInputBox({prompt: 'UserName', placeHolder: 'UserName'}).then(value => {
    if (value === undefined) {
        throw new Error('cancelled');
    }
    // handle valid values
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM