简体   繁体   English

单击 VSCode 活动栏中的扩展程序图标后如何执行特定命令?

[英]How to execute a particular command after clicking an extension's icon in VSCode's activity bar?

I am developing a VSCode extension and just finished adding the icon to the activity bar.我正在开发一个 VSCode 扩展,刚刚完成将图标添加到活动栏。 What I want to do is, execute a command whenever the user clicks that icon.我想要做的是,每当用户单击该图标时执行一个命令。 This is what I have tried:这是我尝试过的:

Package.json Package.json

"viewsContainers": {
      "activitybar": [
        {
          "id": "myID",
          "title": "Testing",
          "icon": "res/logo.svg"
        }
      ]
    },
    "views": {
      "myID": [
        {
          "id": "Explorer",
          "name": "Browse contents",
          "commands": "vscode-myorg.getContents"
        }
      ]
    }

However, the vscode-myorg.getContents command is not getting executed after clicking the icon.但是,在单击图标后, vscode-myorg.getContents命令不会被执行。 How can I achieve this?我怎样才能做到这一点?

You have to define any new command of yours.您必须定义您的任何新命令。

 "commands": [

      {
        "command": "vscode-workat.getProblems",
        "title": "Get Problems"
      },
]

as well as register it in the extension.以及在扩展中注册它。

In looking at Feature Request: Add an API event to indicate when the sidebar viewlet changes which was closed as waiting for another still open issue on getContext to be resolved.在查看功能请求时:添加 API 事件以指示侧边栏视图何时更改,该事件已关闭,因为等待解决getContext上的另一个仍未解决的问题。 And using intellisense in package.json there is no other key besides id , title and icon .在 package.json 中使用智能感知除了idtitleicon之外没有其他键。 So you cannot put a command entry there for instance (I tried and nothing happened.)因此,例如,您不能在此处放置command条目(我尝试过,但什么也没发生。)

There are a couple of other options that might work for you:还有一些其他选项可能对您有用:

If your views are populated by a TreeView then you can detect when that treeViews' visibility changes:如果您的视图由TreeView填充,那么您可以检测到该树视图的visibility何时发生变化:

myTree.onDidChangeVisibility(ev => {
    console.log(ev);                      // ev.visibiltiy = true/false
})

So you can detect when the view becomes visible because the Activity Bar icon is clicked.因此,您可以检测到视图何时变得可见,因为单击了活动栏图标。 That does work.这确实有效。 However, this event is also triggered when the view is already opened and the user just collapses or expands that individual view.但是,当视图已经打开并且用户只是折叠或展开该单个视图时,也会触发此事件。 And I don't think there is really any way to distinguish between the two conditions.而且我认为没有任何方法可以区分这两种情况。

If you are populating your views with a WebView there is also an onDidChangeVisibility event for that.如果您使用onDidChangeVisibility WebView


There is an activation event for a View becoming active:视图变为活动状态时有一个激活事件:

"activationEvents": [
  "onView:package-dependencies"
]

but that will only fire the first time a view is activated - and starts your extension's activate function so is probably not what you are looking for.但这只会在第一次激活视图时触发 - 并启动您的扩展程序的activate function 所以可能不是您想要的。

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

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