Code Monkey home page Code Monkey logo

Comments (5)

joaompneves avatar joaompneves commented on August 19, 2024 1

Hi,

Its up to you to serialize the params passed in EvaluateScriptFunction. There's a EvaluateScriptFunctionWithSerializedParams but its protected. If you want to use it you may create an inherited Webview class.

As for passing large amounts of data from c# to js, I recommend following another approach, which is call a JS method that then requests the information to c#. That's probably more efficient when you have large amounts of data, check this test:

public async Task RegisteredJsObjectReturnObjectSerialization() {

from webview.

electronicbits avatar electronicbits commented on August 19, 2024 1

Ok got it !

I wrote this based on the unit test you gave me, on my own code and javascript is receiving the data.

var testObject = new TestObject()
{
Age = 33,
Kind = Kind.B,
Name = "John",
Parent = new TestObject()
{
Name = "John Parent",
Age = 66,
Kind = Kind.C
}
};

Func functionToCall = () => { return testObject; };
webviewChart.RegisterJavascriptObject("functionToCall", functionToCall);
var scriptAux = $"functionToCall.invoke().then(result=>console.log(result));";
var xresult = await webviewChart.EvaluateScript(scriptAux);

Thanks !

from webview.

electronicbits avatar electronicbits commented on August 19, 2024 1

Just to wrap up, I was doing some performance tests on the above code I posted, and I was having relatively bad performance for what we wanted.

So re-read again your comment, and had a different perspective the second time around.

I am now calling a pre-registered function in javascript from C# called executeFunctionInQueue.
This function in js will then call a function in C# which would request the next function to be executed in the queue (which is simply json data with the name of the function and a lot of test random data)
Javascript would receive the data , deserialize and execute the respective function with the embedded data.

Performance wise, the last post I was having around 30 to 40 ms, no less than that per execution.

I am now getting 1 to 3 ms average.

It is quite the convoluted way of calling functions and passing data around, but at least got a good performance

from webview.

joaompneves avatar joaompneves commented on August 19, 2024 1

Since there's no issue on the WebView side I'll close this

from webview.

electronicbits avatar electronicbits commented on August 19, 2024

I think I've found a workaround for this.

The EvaluateScriptFunction still doesn't work for me if I want to pass non integer data

However, using the function EvaluateScript I managed to create a script where I do the actual call of the function.

I ran this from C#:
(Note: see the sendDataToGuest('testdata'); fuction call)

string script = @"
console.log(before);
var result = hostToGuestSystemInterface.sendDataToGuest('testdata');
console.log(after);
return result`;
".Replace(Environment.NewLine, "");
var result = await webviewChart.EvaluateScript(script);

This works... the plan is to pass in json serialized object to that function to pass relatively large amounts of data.

Either way, would be good to know if this is the optimal way to pass data to js.

Thanks in advance

from webview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.