-
Notifications
You must be signed in to change notification settings - Fork 30
AddHostObjectToScript
IPC with WebView2 using AddHostObjectToScript requires using the default host object or creating & registering a custom host object, registering relevant ActionController(s), using the default scheme or adding a new one.
In most cases, developers do not need to register new host object(s). They should just use the default host object which is preregisted for them.
However, developers who want to create new host object(s) can do so using the guide- here or other approaches outside EdgeSharp.
Every request is mapped to an Action (method) defined in an ActionController class. A sample action will be (HelloWorld):
[ActionController(Name = "HelloWorldActionController", Description = "HelloWorld controller")]
public class HelloWorldActionController : ActionController
{
[ActionRoute(Path = "/helloworld")]
public string HelloWorld()
{
return "Hello, World!";
}
}
Next, the ActionController class "HelloWorldActionController" will be registered in the Application 'ConfigureServices" method either by registering the ActionController itself or the Assembly that contains the controller.
Registering the ActionController:
public class HelloWorldApp : EdgeSharpApp
{
public override void ConfigureServices(IServiceCollection services)
{
base.ConfigureServices(services);
// other service configuration can be placed here
services.AddSingleton<ActionController, HelloWorldActionController>();
}
}
Registering the ActionController assembly:
public class HelloWorldApp : EdgeSharpApp
{
public override void ConfigureServices(IServiceCollection services)
{
base.ConfigureServices(services);
// other service configuration can be placed here
RegisterActionControllerAssembly(services, typeof(HelloWorldActionController).Assembly);
}
}
Using the path /helloworld to call the action HelloWorld in HelloWorldActionController above.
try {
let remObject = await window.chrome.webview.hostObjects.execute;
let result = await remObject.Send('/helloworld), null);
// result : "Hello, World!";
} catch (err) {
console.error(error);
}
EdgeSharp
Getting Started
Resource handling - How files are loaded
Inter-process Communication (IPC)
- Introduction
- AddHostObjectToScript
- PostWebMessage
- Ajax XHR
- Action Controllers
- Creating & Registering Host Objects
- Creating & Registering PostWebMessage Script Promise
- Registering Custom Request Scheme Handlers
Customizing and Extending EdgeSharp
EdgeSharp Samples
Debugging