Use querySelector in nativescript; - nativescript

i want to play an animation when a new message is added in the DOM.
but i don't know how to find my object and edit it with code in (this.zone.run) function :
addMessage(message: string){
this.messages.unshift(message);
// renderer
this.zone.run(() => {
});
}
here's the app.component.html
<StackLayout #container>
<ScrollView>
<WrapLayout #red>
<Label class="message" *ngFor="let message of messages" [text]="message"></Label>
</WrapLayout>
</ScrollView>
</StackLayout>
i want to edit the first child of the WrapLayout element

There is no DOM with NativeScript.
However a major community contributor wrote a plugin to help transition web developers into native development with NativeScript. This plugin provides helper methods that you'll find familiar to the web and DOM. https://github.com/NathanaelA/nativescript-dom
Just remember these are helper methods are not something provided out of the box by NativeScript. You can get any view by its id in NativeScript several ways and during different events (page/frame and component level).
I recall there's no page component with NativeScript with angular but I think you still have the frame module which you could do something like
frame.topmost().currentPage.getViewById('yourID');
Making sure you import(require) the frame module.

Related

Cancelcommand is not working in sfdatepicker in xamarin forms

I want normal cancel functionality on cancel button click of sfdatepicker footer, I am trying to achieve it by binding command but it is not working. Could any one suggest me any solution for that ?
You can use code like below:
<datePicker:SfDatePicker.FooterView>
<Grid>
<Button Text="Ok"
x:Name="footerViewButton"
Clicked="footerViewButton_Clicked"/>
</Grid>
</datePicker:SfDatePicker.FooterView>
Created in this way, and then create the corresponding method in the ViewModel to achieve logical operations.

How to create LibVLCSharp custom playback controls in Xamarin Forms?

I've been searching for days now for a guide on how to create the custom playback controls for LibVLCSharp that everyone seems to talk about, which I never found a guid for.
I simply want to create other buttons with event handlers for the bottom playback control panel, I tried this but throws a System.NullReferenceException exception on startup while getting into break mode...
<vlc:MediaPlayerElement MediaPlayer="{Binding MediaPlayer}" LibVLC="{Binding LibVLC}">
<vlc:MediaPlayerElement.PlaybackControls>
<vlc:PlaybackControls>
<vlc:PlaybackControls.ControlTemplate>
<ControlTemplate>
<Grid>
<StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
<Button Grid.Column="0" Text="Test 1"/>
<Button Grid.Column="1" Text="Test 1"/>
<Button Grid.Column="2" Text="Test 1"/>
</StackLayout>
</Grid>
</ControlTemplate>
</vlc:PlaybackControls.ControlTemplate>
</vlc:PlaybackControls>
</vlc:MediaPlayerElement.PlaybackControls>
</vlc:MediaPlayerElement>
I want it to act just like the original one (Auto hides, overlays on tapping, etc...) but with my own layout and controls. I also thought about using the existing one and try to override their handler to implement my own code and override the text property for each button to change its icon but no luck of finding any help.
Thanks in advance ^_^
The code you are interested in is here: https://code.videolan.org/videolan/LibVLCSharp/-/blob/3.x/src/LibVLCSharp.Forms/Shared/Themes/Generic.xaml
I also thought about using the existing one and try to override their handler to implement my own code and override the text property for each button to change its icon
That'd be the way to go.
This previous SO question might answer your question: https://stackoverflow.com/a/14217500/4064749
Just create a new Style based on PlaybackControlsStyle, override what you want and then set it on the PlaybackControls element.
I created https://code.videolan.org/videolan/LibVLCSharp/-/issues/309 recently to track the need of a tutorial to customize the MediaElement.
Further docs on style inheritance: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/styles/xaml/inheritance
I finally found the problem which was making exceptions, when I create custom control template which completely correct, the MediaPlayerElement code behind by LibVLCSharp developers itself cannot find the elements with the names defined anymore as they used hardcoded names for the buttons and views instead of using bindings and dynamic setters.
Thus, several workarounds could be made to fix such issue, here are some of my ideas:
Use the generic style documented here and modify it without removing any elements but rather hide them out or overlay them.
Create your own style with controls obtaining the same names of the original ones.
Find a way to modify or maybe create a whole new playback control element using the original one which can be found here and here.
Thanks to mfkl's answer which helped me find out how everything worked under the hood to come up with the explaination, even though this took me a couple of days to figure out.

NativeScript Reuse Actionbar (with RadSideDrawer)?

Is there a way to create a reusable ActionBar for all pages?
I followed this tutorial which walked me through the process of adding in a RadSideDrawer to my 4 page app (not 100% if using a sidedrawer would change how I could approach this problem).
The 4 pages all have the same ActionBar. Instead of copy/pasting the code on each page (and updating on each page with changes), how do I reuse the same code for them all?
I tried statically defining the ActionBar code in the Frame content...
<template lang="html">
<Page>
<RadSideDrawer>
<StackLayout ~drawerContent>
<slot name="drawerContent"></slot>
</StackLayout>
<Frame ~mainContent>
<!-- TRIED PUTTING ACTIONBAR STRUCTURE HERE? -->
<slot name="mainContent"></slot>
</Frame>
</RadSideDrawer>
</Page>
</template>
I've also tried creating a custom component (with my ActionBar code living in /widgets/header/header.xml), by following this tutorial...
<Page xmlns:header="widgets/header">
<header:header />
</Page>
But I got an error message in the console:
[Vue warn]: Unknown custom element: - did you register the component correctly? For recursive components, make sure to provide the "name" option.
My best guess, at this moment, is that NativeScript-Vue prefers the use of slots -- but how would I go about putting a slot into content that's populating another slot (<slot name="mainContent"></slot>)?

Populate HTML forms in WebView

I have a NativeScript-5 app (TypeScript flavor) with a simple page like this:
<Page class="page">
<StackLayout>
<WebView id="webView" loaded="onWebViewLoaded" src="http://google.com" />
</StackLayout>
</Page>
After loading the web page (onWebViewLoded()), I would like my app to populate certain HTML fields (access by id or name) and finally post the surrounding HTML form. Can this be done at all?
I know that there is a Nativescript-WebView-Interface plugin, but it requires me to embed a script in the loaded page (and I can't do this, because I don't own the page I am loading). So I assume I need another approach.
If anybody has a solution that works at least on Android, that would be great. Thanks guys!
Update:
In order to avoid misunderstandings: Submitting the page is optional. The important part is to load a web page and auto-fill some values that my app already knows (so the user does not have to enter these values in the HTML form himself).
You may easily execute JavaScript in the webpage context in Android.
export function onLoadFinished(args: EventData) {
const webView = (<WebView>args.object).nativeView;
if (isAndroid) {
// Make sure the element index is valid
const javaScript = `document.getElementsByTagName("input")[2].value = "It works!"`;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
webView.evaluateJavascript(javaScript, null);
} else {
webView.loadUrl(`javascript:${javaScript}`);
}
}
}
Here is the Playground Sample
It's also possible with iOS, but you may have to override / extend the existing {N} WebView inject JavaScript upon creating native view.

NativeScript: how to use android.support.v7.widget.SwitchCompat on Android 4

Is there a good way to use Material AppCompat Switch on Android 4? If so, please give an example of usage in NativeScript.
I made a little research and found that there is no easy way to use Material design for Switch with version lower then 5.
Switch component is available from API v7 as it has been described here. However you could change some basic style properties by accessing android property. I am attaching sample code:
main-page.xml
<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="load">
<StackLayout>
<Switch id="test" checked="{{ checked }}" class="style" />
</StackLayout>
</Page>
main-page.js
function load(args) {
var page = args.object;
var tmpswitch = page.getViewById("test");
tmpswitch.android.setShowText(false);
tmpswitch.android.setSwitchMinWidth(15);
}
exports.load = load;

Resources