0

I hope I didn't miss any appropriate solution from another topic. Like the topic says, I'm using blazor with interop calls. That works fine, but now my TypeScript file with the class definition uses another TS module via import. Later I added the script with the type="module" attribute, to avoid another issue. Now my interop calls (via JSRuntime) to this script functions doesn't work anymore, probably because of the limited scope of the module classes and variables?

Before the code follows, thanks a lot for your help !

Sample class in CanvasFeeder.ts/js with Method to call via interop:

export class CanvasFeeder
{
   public InitCanvas(){ }
}
export * as CanvasFeed from "CanvasFeeder";

var MyCanvasFeeder = new CanvasFeeder();
var ToCallInterop = () => {MyCanvasFeeder.InitCanvas();};

Script reference index.html:

<script src="typescript/CanvasFeeder.js" type="module"></script>

What have I tried?

JSRuntime.InvokeVoidAsync("ToCallInterop");
JSRuntime.InvokeVoidAsync("MyCanvasFeeder.ToCallInterop");

I also tried to instantiate the class and function directly in the index.html.

What are the error message?

  • Unhandled exception: Could not find 'ToCallInterop'
  • Unhandled exception: 'ToCallInterop' is not a variable (when instantiating var directly in area of index.html
  • Blazor looks for functions to call on window, so you would need to store your MyCanvasFeeder object in the `window` object, I believe - e.g. `window.MyCanvasFeeder = new CanvasFeeder()` – Mister Magoo Jul 26 '20 at 02:12
  • Unfortunately this doesn't work for me. In TS I get a syntax error: "Property 'CanvasFeeder' does not exist on type 'Window&typeof globalThis". Anyway I also tried it in a new JS, where the syntax is ok. But the problem is still the same, when I add the script as type="module" to the index.html, the function could not be found and I got the same error message. – MowedMeadow Jul 26 '20 at 09:36
  • Oh, sorry wasn't paying enough attention - in TS, I think something like `window['MyCanvasFeeder'] = new CanvasFeeder()` should work – Mister Magoo Jul 26 '20 at 10:20
  • I have added it an answer - if you could accept that it will close the question. – Mister Magoo Jul 27 '20 at 13:28

1 Answers1

0

Blazor looks for functions to call on window, so you would need to store your MyCanvasFeeder object in the window object.

window['MyCanvasFeeder'] = new CanvasFeeder()

Mister Magoo
  • 7,452
  • 1
  • 20
  • 35