binding object (RegisterJsObject) qustion

2,790 views
Skip to first unread message

مهدی باقری

unread,
Dec 1, 2012, 8:54:14 AM12/1/12
to cefs...@googlegroups.com

hi a want to run a c# function from my button on click event on HTML page

i read this post that explained with IE component on .net

http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.objectforscripting.aspx

help me about it.
if you can write a little example for me thanks guys

Jack O'Connor

unread,
Dec 1, 2012, 12:44:09 PM12/1/12
to cefs...@googlegroups.com

There is a very similar interface for doing the same thing in CefSharp. Take a look at this thread: https://groups.google.com/forum/#!topic/cefsharp/FskXN1BC7WU

--
You received this message because you are subscribed to the Google Groups "CefSharp" group.
To view this discussion on the web visit https://groups.google.com/d/msg/cefsharp/-/oGL-kJ_9kB0J.
To post to this group, send email to cefs...@googlegroups.com.
To unsubscribe from this group, send email to cefsharp+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cefsharp?hl=en.

Mehdi

unread,
Dec 2, 2012, 4:48:33 AM12/2/12
to cefs...@googlegroups.com
thanks Jack O'Connor
here is my test class and html code

i run my program with this line

c#

------------------------------------------------------------------------
        public FormBrowser()
        {
            InitializeComponent();       
            webView = new WebView();
            webView.Address = "file:///E:/index.html";
            this.Controls.Add(webView);
            webView.RegisterJsObject("temp", new Temp());
       }
------------------------------------------------------------------------------------------

c# class
----------------------------------------------------
using System.Windows.Forms;

namespace AftabSoft
{
    class Temp
    {
        public void test()
        {
            new OpenFileDialog().ShowDialog();
        }
        public string strTest(string str)
        {
            string result = String.Empty;
            for (int i = 0; i < 10; i++)
            {
                result += str +" ";
            }
            return result;
        }
        public void messsage(string str)
        {
            MessageBox.Show(str);
        }

    }
}
--------------------------------------------------------------------------
HTML
--------------------------------------------------------------------------
<html>
<head>
    <title>Binding Test</title>
</head>
<body>
    <p>
       
        <script type="text/javascript">
            temp.test();
            var result = temp.strTest("hi");
            document.write('"' + result + '"');
        </script>
    </p>
    <script >
        function message(str)
        {
            temp.messsage(str);
        }
    </script>
    <button onclick="message('Hello World!')" />
</body>
</html>

----------------------------------------------------
i hope this help other programmers

Jan Apel

unread,
Dec 11, 2012, 11:54:23 AM12/11/12
to cefs...@googlegroups.com
Hey there!

this is not working for me :( tried the same as you, but I just don't get a MessageBox displayed :(
But maybe this is my wrong approach. I want to get the value of a javascript object into a c# variable, but I just don't get it done.

Are there any examples on this?

best regards,

Jan

Mehdi

unread,
Dec 11, 2012, 1:51:14 PM12/11/12
to cefs...@googlegroups.com
hi
first you have to create your class
then put a function in that class
and for example my class is Temp
class Temp
{

     public void messsage(string str)
{
MessageBox.Show(str);}
}
}

and whit blow function you can register your class woth a name
and with that name you can access the class from javascript
            webView.RegisterJsObject("temp", new Temp());
after that in your html use script like bloew
        

    <script >
            temp.messsage("Hello World!");
    </script>

Jack O'Connor

unread,
Dec 11, 2012, 2:12:33 PM12/11/12
to cefs...@googlegroups.com
There might be some confusion here. The RegisterJSObject interface is for exposing C# values/functions into the JS world. If you want to go the other way---reading a value from JS into C# for example---you might want to look at the EvaluateScript interface. That's called from C# and takes a string of JS code to evaluate inside the WebView, returning the value back into C#.

You could use RegisterJSObject to expose a function for JS to call, and then have C# take the values it wants out of the arguments it gets passed, but that's probably more complicated than what you need?


--
You received this message because you are subscribed to the Google Groups "CefSharp" group.
To view this discussion on the web visit https://groups.google.com/d/msg/cefsharp/-/8VRAJoknPTIJ.

Jan Apel

unread,
Dec 12, 2012, 4:07:21 AM12/12/12
to cefs...@googlegroups.com
Thank you both very much.
I tried with ExecuteScript, but didn't know what to do with a void. EvaluateScript is an Object which is much easier to convert.

now it works

Dan Mayor

unread,
Dec 18, 2013, 12:21:52 PM12/18/13
to cefs...@googlegroups.com
Has anyone ever actually gotten this to work?  I have tried numerous times and I can get javascript to recognize the object but none of the members or methods.

public class MyObject 
    public string Message = "Hello World";

    public void SayHello()
    {
        MessageBox.Show("Hello World");
    }
}

...

myWebView.RegisterJsObject("MyObject", new MyObject());

...

HTML:

<script type="text/javascript">
    alert(window.MyObject);  // <~ "MyNameSpace.MyObject"
    alert(window.MyObject.Message);  // <~ "Unknown"
    window.MyObject.SayHello();  // <~  Nothing happens, never enteres the MyObject.SayHello() method  (Tried setting breakpoint in C# code)
</script>


What gives?

Per Lundberg

unread,
Dec 25, 2013, 11:11:03 AM12/25/13
to cefs...@googlegroups.com
Hi Dan,

Thanks for your interest in the project. Yes, this should definitely work (I presume you are using CefSharp1, since it doesn't work with CefSharp3 yet).
I think it has to do with the automatic method name mangling that I added a while back - take a look at this wiki page for the details: https://github.com/cefsharp/CefSharp/wiki/Frequently-asked-questions
(I updated it just now with these details)

Best regards
Per

Dan Mayor

unread,
Jan 19, 2014, 6:25:17 PM1/19/14
to cefs...@googlegroups.com
I hate to be that guy, but that is an awful lot of reading not even knowing if it is reliable information or not.  "Should definitely work" sounds as if you have not gotten it to work either.  Sorry if that sounds rude but I was hoping for response from someone who has gotten it to work and how they went about doing it.

Mehdi Bagheri

unread,
Jan 19, 2014, 9:17:03 PM1/19/14
to cefs...@googlegroups.com
don't use window
try this

<script type="text/javascript">
    MyObject.SayHello(); 
</script>

Per Lundberg

unread,
Jan 20, 2014, 2:01:26 AM1/20/14
to cefs...@googlegroups.com
Hi,

Yes, you sound a bit rude. :-) But it's probably just your frustration shining through... "Awful lot of reading" - yes, if you read the whole FAQ, but keep it to the relevant paragraph (number 2: "How do you expose a .NET class to Javascript?") and you should definitely be able to get it working.

I have it working, in an app delivered to a 3rd party customer, so yes - it definitely works. As long as you remember the name mangling rules (which are described on the FAQ) and also the limitations for the types you can send to/from JavaScript, it works well.

To make it even more blatant: in your specific case, the method should be called not like SayHello(), but like sayHello() with a lowercase s - to make it more like JavaScript.
Fields (like Message in your case) are not exposed to JavaScript, only methods and properties. Properties can only be read though, not written, because of this issue: https://github.com/cefsharp/CefSharp/issues/189

So yes, there are some limitations, but it does work and for many use cases, it does the job quite well.

Best regards,
Per

Dan Mayor

unread,
Jan 23, 2014, 4:46:40 PM1/23/14
to cefs...@googlegroups.com
Much thanks, it is the camel casing that was causing the issue.  Using window. or not makes no difference.

window.MyObject.exitApp() <~ Good
MyObject.exitApp(); <~ Good

in C#,
public void ExitApp() {} <~ Gets called

Michal Tsadok

unread,
Apr 22, 2014, 11:29:58 AM4/22/14
to cefs...@googlegroups.com
Hi Per,

Your FAQ is the most clear and simple one that I saw
I manage to get things work sooooo quickly
Except this Upper/Lower case thingy

Could you please bold the camel casing issue in the FAQ, so the upper/lower thingy would catch our eyes

Thanks for being so kind and responsive - it makes stuff so much nicer :)
Cheers,
Michal
Reply all
Reply to author
Forward
0 new messages