简体   繁体   English

Cefsharp Webview界面的打字稿定义

[英]Typescript definition for a cefsharp webview interface

I registered a RegisterAsyncJsObject in the CefSharp ChromiumWebBrowser. 我在CefSharp ChromiumWebBrowser中注册了RegisterAsyncJsObject。 This creates an additional JavaScript object instance in the web browser which looks like this: 这将在网络浏览器中创建一个附加的JavaScript对象实例,如下所示:

frame.openWindow(name, data);
frame.navigateToWindow(name, data);

It works great with plain JavaScript. 它与纯JavaScript一起很好地工作。 I'm able to access to this object and the methods do their job. 我可以访问该对象,并且方法可以完成其工作。

Now I want to program against this object with TypeScript, but the TypeScript compiler doesn't know the types of that object. 现在,我想使用TypeScript针对该对象进行编程,但是TypeScript编译器不知道该对象的类型。 I tried to create a type definition file of that object, but it doesn't really work. 我试图创建该对象的类型定义文件,但实际上并没有用。 it seems if I import a definition / declaration, what ever, it will override the 'frame' object. 看来,如果我导入定义/声明,无论如何,它将覆盖“框架”对象。

Do you have any idea how to use that object in TypeScript ? 您是否知道如何在TypeScript中使用该对象?

Do I really need to have a type definition for that object? 我真的需要该对象的类型定义吗? And how does it really work? 以及它如何真正起作用?

Until now I just used TypeScript and already done type definitions, but didn't created definitions by my own. 到目前为止,我只是使用TypeScript并已经完成了类型定义,但是还没有自己创建定义。

If frame doesn't yet have an existing type, you can define your own: 如果frame还没有现有的类型,则可以定义自己的类型:

interface MyFrame {
    //fill in the `any` and `void` types as appropriate
    openWindow(name: any, data: any): void; 
    navigateToWindow(name: any, data: any): void;
}

//if frame is initialized somewhere else:
declare let frame: MyFrame;

//if you want to initialize it here:
let frame: MyFrame = //some initialization code

If frame has an existing type, say HTMLFrameElement from lib.d.ts , then you can add an additional interface to extend the existing one. 如果frame具有现有的类型,例如lib.d.ts HTMLFrameElement ,则可以添加其他接口以扩展现有的接口。 This is known as declaration merging : 这称为声明合并

interface HTMLFrameElement {
    openWindow(name: any, data: any): void; 
    navigateToWindow(name: any, data: any): void;
}

This can be done either in a regular Typescript file ( .ts ) or in a definition file ( .d.ts ). 这可以在常规Typescript文件( .ts )或定义文件( .d.ts )中完成。


You can include the interface + declaration in the current file, and that should allow you to use these methods. 您可以在当前文件中包含接口+声明,这应该允许您使用这些方法。 If you want to place it in an external file, and in order to let the compiler know about the file, do one of the following: 如果要将其放置在外部文件中,并且为了让编译器知道该文件,请执行以下操作之一:

  • Include it in the project's tsconfig.json 将其包含在项目的tsconfig.json
  • Import it into each referencing file with import "/myfile" 使用import "/myfile"其导入到每个引用文件中

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

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