VS Code 扩展可以从其他程序获取 URI 请求吗? [英] Can VS Code extensions get URI requests from other programs?
问题描述
我在官方文档中找到了 onUri
可以用作激活事件的信息.因此,我的扩展程序可以通过调用 URI(例如 vscode://myextension/arg1=foo&arg2=bar
)从用 C# 编写的 WPF 程序激活.但是它没有说明我如何获取随 URI 请求传递的参数.或者甚至只是获取它的原始字符串.
I found information on the official docs that onUri
can be used as an activation event. So, my extension can be activated from, say, a WPF program written in C# by calling the URI such as vscode://myextension/arg1=foo&arg2=bar
. But nowhere does it say how I can get the arguments that were passed with the URI request. Or even just get a raw string of it.
我的问题是,能不能实现,如果不能,有没有其他方法可以让 VS Code 扩展与另一个程序交互?
My question is, can it be done and if not, is there any other way to make a VS Code extension interact with another program?
推荐答案
是的,你可以使用 vscode.window.registerUriHandler()
为此:
Yes, you can use vscode.window.registerUriHandler()
for this:
注册一个 uri 处理程序,能够处理系统范围的uris.如果有多个窗口打开,最上面的窗口将处理 uri.uri 处理程序的范围限定于它所提供的扩展;它只能处理指向扩展本身的 uri.uri 必须遵守以下规则:
Registers a uri handler capable of handling system-wide uris. In case there are multiple windows open, the topmost window will handle the uri. A uri handler is scoped to the extension it is contributed from; it will only be able to handle uris which are directed to the extension itself. A uri must respect the following rules:
uri-scheme 必须是产品名称;
The uri-scheme must be the product name;
uri-authority 必须是扩展名(例如 my.extension
);
The uri-authority must be the extension id (eg. my.extension
);
一个扩展在其整个激活生命周期内只能注册一个 uri 处理程序.
An extension can only register a single uri handler in its entire activation lifetime.
- 注意:有一个激活事件
onUri
在即将处理指向当前扩展的 uri 时触发.
- Note: There is an activation event
onUri
that fires when a uri directed for the current extension is about to be handled.
用法很简单:
vscode.window.registerUriHandler({
handleUri(uri:vscode.Uri) {
// do something with the URI
}
});
这篇关于VS Code 扩展可以从其他程序获取 URI 请求吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!