javascript How to call c # method

Please post public support tickets here. Note: for private support tickets, please send an email to support@cshtml5.com instead.
tomny
Posts: 37
Joined: Sat Nov 19, 2016 7:23 pm

javascript How to call c # method

Postby tomny » Sun Jan 22, 2017 10:11 pm

Dear JS-Support,

javascript function How to call c# code (method or property)?

Thank you.
Regards,
Tomny

JS-Support @Userware
Site Admin
Posts: 886
Joined: Tue Apr 08, 2014 3:42 pm

Re: javascript How to call c # method

Postby JS-Support @Userware » Mon Jan 23, 2017 2:49 am

Hi,

Here is how to call C# from JavaScript:

Code: Select all

void Test()
{
    CSHTML5.Interop.ExecuteJavaScript(@"

        // This is JavaScript code
        alert('Hello from JavaScript');

        // Let's call the C# method "MyCSharpMethod". Due to the fact that it is passed as the first parameter to this ExecuteJavaScript call, we can access it with $0:
        $0();
       
    ", (Action)MyCSharpMethod);
}

void MyCSharpMethod()
{
    System.Windows.MessageBox.Show("Hello from C#");
}


For reference, here is a link to the documentation page "How to Call JavaScript from C#":
http://cshtml5.com/links/how-to-call-javascript.aspx

Regards,
JS-Support

tomny
Posts: 37
Joined: Sat Nov 19, 2016 7:23 pm

Re: javascript How to call c # method

Postby tomny » Mon Jan 23, 2017 8:39 am

Hi,

thank you for your reply. I now seek is the direct implementation of js c # method, not through

CSHTML5.Interop.ExecuteJavaScript ("$0()", (Action)MyCSharpMethod).

I tried: CSHTML5.Interop.ExecuteJavaScript ("var Method1 = $0", (Action)MyCSharpMethod),

and then in the implementation of js call Method1(). Also strangely, the simulator runs successfully, but the browser fails on the prompt

JS-Support @Userware
Site Admin
Posts: 886
Joined: Tue Apr 08, 2014 3:42 pm

Re: javascript How to call c # method

Postby JS-Support @Userware » Tue Jan 24, 2017 3:02 am

Hi,

Oh, I see, you want to make the C# method globally accessible from any JS code. To do that, you can add the method to the browser "window" object for example, like this:

Code: Select all

CSHTML5.Interop.ExecuteJavaScript ("window.method1 = $0", (Action)MyCSharpMethod);


It should work both in the browser and in the Simulator.

Regards,
JS-Support

tomny
Posts: 37
Joined: Sat Nov 19, 2016 7:23 pm

Re: javascript How to call c # method

Postby tomny » Tue Jan 24, 2017 10:08 pm

Dear JS-Support,

Thank you, the code can work. This function is useful, and sometimes when we call a js method, you need to let js automatically return to the c # method, you can achieve this.

Regards,
Tomny

kmatt
Posts: 30
Joined: Wed Feb 01, 2017 11:16 am

Re: javascript How to call c # method

Postby kmatt » Tue Jul 18, 2017 12:17 pm

I have the following C# code:

Interop.ExecuteJavaScript(@"window.method1 = $0;method1();",(Action)Dummy);

public void Dummy()
{
Debug.WriteLine("Called dummy");
}

Dummy is never called. I've also tried making the method static but it still doesn't work. Has something changed?

JS-Support @Userware
Site Admin
Posts: 886
Joined: Tue Apr 08, 2014 3:42 pm

Re: javascript How to call c # method

Postby JS-Support @Userware » Sat Aug 19, 2017 6:44 am

Hi,

Please find attached a project that works properly on my machine.

Let me know if you see any differences.

Thanks.
Regards,
JS-Support
Attachments
Application48.zip
(14.53 KiB) Downloaded 44 times


Return to “Technical Support”

Who is online

Users browsing this forum: No registered users and 1 guest

 

 

cron