how to get UniqueIdentifier of converted HTML5 object ?

Please post public support tickets here. Note: for private support tickets, please send an email to support@cshtml5.com instead.
Sesztak
Posts: 172
Joined: Fri Jun 24, 2016 2:19 am

how to get UniqueIdentifier of converted HTML5 object ?

Postby Sesztak » Wed Aug 31, 2016 4:11 am

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

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

Re: how to get UniqueIdentifier of converted HTNL5 object ?

Postby JS-Support @Userware » Wed Aug 31, 2016 4:34 am

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

Sesztak
Posts: 172
Joined: Fri Jun 24, 2016 2:19 am

Re: how to get UniqueIdentifier of converted HTML5 object ?

Postby Sesztak » Wed Aug 31, 2016 5:47 am

Really thanks ! :)

Sesztak
Posts: 172
Joined: Fri Jun 24, 2016 2:19 am

Re: how to get UniqueIdentifier of converted HTML5 object ?

Postby Sesztak » Thu Sep 01, 2016 4:31 am

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

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

Re: how to get UniqueIdentifier of converted HTML5 object ?

Postby JS-Support @Userware » Thu Sep 01, 2016 7:13 am

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);

Sesztak
Posts: 172
Joined: Fri Jun 24, 2016 2:19 am

Re: how to get UniqueIdentifier of converted HTML5 object ?

Postby Sesztak » Thu Sep 01, 2016 8:02 am

perfect !!

Sesztak
Posts: 172
Joined: Fri Jun 24, 2016 2:19 am

Re: how to get UniqueIdentifier of converted HTML5 object ?

Postby Sesztak » Fri Sep 02, 2016 12:42 am

Just a two small corrections of you -it should help others:
please, take care :
-When you get CSharpReference: use ExecuteJavaScript, not ExecuteJavaScriptAsync,
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 ?

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

Re: how to get UniqueIdentifier of converted HTML5 object ?

Postby JS-Support @Userware » Fri Sep 02, 2016 2:32 am

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


Return to “Technical Support”

Who is online

Users browsing this forum: No registered users and 41 guests