microsoft / Chakra-Samples

Repository for Chakra JavaScript engine related samples.
MIT License
216 stars 84 forks source link

Can we create UI controls? #53

Closed harshbodhi03 closed 7 years ago

harshbodhi03 commented 7 years ago

Hi, I am new to chakra and going through the sample. I started with checking few Windows namespace properties like Windows.UI.Xaml.Window.current.height and it worked fine. So just to extend it a little further I thought to give a try to UI controls as below but ended up with exception "Object doesn't support this action". Can we not create UI controls?

function test() { var x=new Windows.UI.Xaml.Button(); return x; }

test();

liminzhu commented 7 years ago

I believe most xaml controls are hidden from Chakra because as you know JavaScript usually goes with html, therefore in your case the Button class is not constructible in JS. Is there anything particular you wish to achieve that is not doable with html?

harshbodhi03 commented 7 years ago

Thanks for your input on this. The tool that i am writing is cross platform where developer can write their app in javascript and it will generate the packages for android and windows. I do not want to involve html here to streamline the behavior with other platforms(android), that's the reason i was looking for a way to get UI controls created with javascript using chakra. I gave another try with the chakra sample where I created a separate library and defined a static function there which returns Button object. function test() { return FFILib.FFI.getControl(); }

test();

On calling that function using Javascipt in sample I am able to get the UI object but I don't know how to marshal it to actual button object. Below is the way I am marshalling a UI object. but I receive exception "Cannot pass a GCHandle across AppDomains.\r\nParameter name: handle" GCHandle handle = (GCHandle)returnValue; var objec = handle.Target;

If I marshal it to string as below, I get the type of control as "Window.Xaml.Controls.Button". Marshal.PtrToStringUni(returnValue);

So what I could figure out is, Chakra is returning the UI control object but a proper conversion is required.

harshbodhi03 commented 7 years ago

Hi @liminzhu , Any update on this?

liminzhu commented 7 years ago

Sorry for the delay @harshbodhi03 . IIUC you're trying to manually marshal xaml controls across to JavaScript, you can look at the JsBridge project for inspiration. The basic idea would be to make your own winrt component (again, xaml is hidden from Chakra) and project it to Chakra with JsProjectWinRTNamespace if it makes any sense.

harshbodhi03 commented 7 years ago

Thanks @liminzhu . This worked for me.