mslavchev / chakra-win10uap

2 stars 0 forks source link

Class type casting #2

Open pabloko opened 2 years ago

pabloko commented 2 years ago

Hello, im writting here because this is the only place on the internet that ive found some useful info about JSRT uwp projected namespaces.

Im working on a native xaml ui framework using JSRT, i had partial success loading XAML content and using it from javascript. Im using a very small executable that uses xaml islands on c++ and integrated jsrt, used JsProjectWinRTNamespace(L"Windows"); to expose all uwp namespaces, this is how i load content:

content = Windows.UI.Xaml.Markup.XamlReader.load(`<TextBlock Text="Hello from JSRT-UWP" Padding="20" Name="abc" HorizontalAlignment="Stretch" VerticalAlignment="Center" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" />`);
host.loadxaml(content)
content.text = "Hello from JSRT-UWP, edited from javascript";
content.textAlignment = 0;

So, breaking this, im using XamlReader to create xaml object from string, wich generates a [object Windows.UI.Xaml.Controls.TextBlock] using a native to plug this content on the xaml island, and following the type idl to work with it.

image

The native load part casts this object from IInspectable to UIElement and plug into the xaml island main content

JsObjectToInspectable(arguments[1], (IInspectable**)&MainContent);
_desktopWindowXamlSource.Content(MainContent.as<UIElement>());

Until here, everithing is good, but now i need to cast types in JSRT, for example from Windows.UI.Xaml.Controls.Button to Windows.UI.Xaml.Controls.Primitives.ButtonBase where i can effectively change the text of the button, or Windows.UI.Xaml.Controls.TextBlock to Windows.UI.Xaml.Controls.UIElement, wich allows to use AddHandler to use events, wich i need for this (i have to follow the idl and change the first letter to lowercase)

I tried to create a native and cast from xxxx to UIElement but JSRT always return the same main type. Any idea on how i could cast there?

JsValueRef CALLBACK JsIinsToUiElement(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState)
{
    winrt::Windows::Foundation::IInspectable MainContent = NULL;
    JsObjectToInspectable(arguments[1], (IInspectable **)&MainContent);
    auto uie = MainContent.as<UIElement>().as<IInspectable>();
    JsValueRef ins = JS_INVALID_REFERENCE;
    JsInspectableToObject(uie.get(), &ins);
    return ins;
}

image

Do you know any way i could achive type casting?

Regards!

PS: i know about react-native-windows project, they use corewindow to create a catch-all event handler and pass to JS, but i dont like this approach, nor like react at all, so im still exploring my way...