Hi Amir,
Yes, you can easily get callbacks from JavaScript to C#.
To do so, please place the following C# code in your CSHTML5 project:
Code: Select all
void MyMethodThatCallsJavaScriptCode()
{
JSIL.Verbatim.Expression(@"
// Place your JavaScript code here ...
map.on('click', function (e) {
$0(e);
});
", (Action<dynamic>)OnClick);
}
static void OnClick(dynamic eventArgs)
{
// This is C# code:
double value = (double)eventArgs.latlng.lat;
MessageBox.Show("Latitude in degrees: " + value.ToString());
}
In the code above, we access the C# "OnClick" method from within JavaScript. This is achieved by passing it as the first argument of the "JSIL.Verbatim.Expression" method, which makes it available to the JavaScript code via the "$0" keyword. Similarly, you can use "$1", "$2", "$3", etc. keywords to reference the second, third, and fourth arguments of the "JSIL.Verbatim.Expression" method.
Please note that, unlike the "String.Format" method which replaces "{0}" at RUNTIME, the "JSIL.Verbatim.Expression" method replaces "$0" at COMPILE TIME. In fact, if you look at the generated JavaScript code (in the Output folder), you will notice that the "$0" code has already been replaced.
If something is not very clear, please let me know.
Please note that the "dymanic" keyword is currently not well supported under VS 2015 as of the Beta 5.2 - even though it works very well under VS 2012 and VS 2013.
Regards,
JS-Support