Page 1 of 1
how to get UniqueIdentifier of converted HTML5 object ?
Posted: Wed Aug 31, 2016 4:11 am
by Sesztak
Dear JS-Support,
A simple question: how we can get UniqueIdentifier of converted HTML5 object ?
e.g. we have a .NET object (XAML/C#):
<TextBlock x:Name="nameOfTextBlock" />
As we assume the .NET name of object (e.g.: "nameOfTextBlock") will be converted to certain id by CSHTML5:
e.g.:
UniqueIdentifier = "id27"
Q1: m'I right ?
When we debug the project under VisualStudio, we should get it by:
object Object = CSHTML5.Interop.GetDiv(nameOfTextBlock);
// Object = {DotNetForHtml5.Core.INTERNAL_HtmlDomElementReference}
// UniqueIdentifier = "id27"
Q2: how we can get the UniqueIdentifier of sample TextBlock in non-debug mode ?
Background info: for a 3rd party js lib, we would like to get object reference by id.
Thanks in advance,
Best Regards,
Péter
Re: how to get UniqueIdentifier of converted HTNL5 object ?
Posted: Wed Aug 31, 2016 4:34 am
by JS-Support @Userware
Hi Péter,
Q1: Yes, but only when running in the Simulator, not in the browser.
Q2: You can use the following code to retrieve the ID:
Code: Select all
object div = Interop.GetDiv(this);
string id = CSHTML5.Interop.ExecuteJavaScript("$0.id", div).ToString();
As said, this will only return a non-empty string in the Simulator.
If you want to ensure that the DIV has an ID, you can use the following code:
Code: Select all
object div = Interop.GetDiv(this);
// Make sure that the Div has an ID, because the 3rd party library requires it:
Interop.ExecuteJavaScript("if (!$0.id) { $0.id = $1 }", div, Guid.NewGuid().ToString());
Then, you can pass the ID to the JS 3rd party library with code like the following:
Code: Select all
object div = Interop.GetDiv(this);
CSHTML5.Interop.ExecuteJavaScript("ThisIsMyThirdPartyLibraryThatRequiresAnIdAsParameter($0.id)", div);
(notice that we never need to put the ID into a C# string)
You can see a full working example in the
ArcGIS Mapping Control.
Regards,
JS-Support
Re: how to get UniqueIdentifier of converted HTML5 object ?
Posted: Wed Aug 31, 2016 5:47 am
by Sesztak
Really thanks !

Re: how to get UniqueIdentifier of converted HTML5 object ?
Posted: Thu Sep 01, 2016 4:31 am
by Sesztak
Dear JS-Support,
another similar question:
How to get C# reference of normal javascript object created by Interop.ExecuteJavaScriptAsync ? (non UI / No HTML5! )
e.g.
// we create a javascript object, called 'spinner' :
CSHTML5.Interop.ExecuteJavaScriptAsync(@"var spinner = new Spinner({color:'#000', lines: 10, fps: 20, hwaccel: true})");
Question: how we can get C# reference of 'spinner' javascript object ? e.g. we would like to modify color or fps property ??
Thanks in advance really !
Best Regards,
Péter
Re: how to get UniqueIdentifier of converted HTML5 object ?
Posted: Thu Sep 01, 2016 7:13 am
by JS-Support @Userware
Does this suit your needs?
Code: Select all
var spinnerCSharpReference = CSHTML5.Interop.ExecuteJavaScript(@"new Spinner({color:'#000', lines: 10, fps: 20, hwaccel: true})");
Please note that every time you want to modify it, you will need to use ExecuteJavaScript again, like this:
Code: Select all
CSHTML5.Interop.ExecuteJavaScript("$0.fps = 30", spinnerCSharpReference);
Re: how to get UniqueIdentifier of converted HTML5 object ?
Posted: Thu Sep 01, 2016 8:02 am
by Sesztak
perfect !!
Re: how to get UniqueIdentifier of converted HTML5 object ?
Posted: Fri Sep 02, 2016 12:42 am
by Sesztak
Just a two small corrections of you -it should help others:
please, take care :
-When you get CSharpReference: use ExecuteJavaScript, not ExecuteJavaScript
Async,
as the Async version has no object return value as it is void

- use not var, but object type for CSharpReference.
Thanks again !!,
Péter
p.s.: Question: why ExecuteJavaScriptAsync has no no-void version ?
Re: how to get UniqueIdentifier of converted HTML5 object ?
Posted: Fri Sep 02, 2016 2:32 am
by JS-Support @Userware
Thanks Péter. I didn't notice I copy/pasted the "Async" version. I just updated the original post to avoid errors.
p.s.: Question: why ExecuteJavaScriptAsync has no no-void version ?
Because there is currently no way to know when the async call has finished. If you want a callback, you can include the callback in the JS that you pass to the ExecuteJavaScriptAsync method, like this:
Code: Select all
ExecuteJavaScriptAsync(@"
// Enter some JavaScript here...
// Then, when finished, we call the calback:
$0();
", (Action)myCSharpCallBack);
Regards,
JS-Support