BY Ajay Singh10 Sep 2021 Edit
Getting started with WebAtoms XFListView.

This section provides a quick overview of WebAtoms XFListView user interface control for Xamarin Forms.

Create a Simple XFListView

This section explains how to create a XFListView in WebAtoms Xamarin Forms. In this particular example we will show the real-time crypto data from free public APIs." Since the API gets many hits in a day, we are replacing it with mock data.

Creating a Data Model for XFListView.

Create a simple data source as shown in the following code example in a new class file. It has the all the properties which we are going to bind to our XFListView user interface control.

export default class ICoinWatchModel {
    symbol?: string;
    priceUsd?: string;
    changePercent24Hr?: string;
};

Creating a simple ViewModel.

This sections explains how to load the data source using mock data.

  export default class XFCoinWatchViewModel extends AtomViewModel {

    public coinList: ICoinWatchModel[] = [];

    public option = {
        style: 'percent',
        minimumFractionDigits: 2,
        maximumFractionDigits: 2
     };
     public percentageFormatter = new Intl.NumberFormat("en-US", this.option);

     public coins:ICoinWatchModel[] = [
       {
            symbol: "BTC",
            priceUsd: "46115.0480271388304650",
            changePercent24Hr: "-0.5095168590745112",
        },
        {
            symbol: "ETH",
            priceUsd: "3387.2839227538603109",
            changePercent24Hr: "-3.8610020887697443",
        },
        {
            symbol: "ADA",
            priceUsd: "2.4690387416910484",
            changePercent24Hr: "-5.1234648122416866"
        },
        {
            symbol: "BNB",
            priceUsd: "413.6486527570754022",
            changePercent24Hr: "-1.2227312211470998"
        },
        {
            symbol: "USDT",
            priceUsd: "1.0005463306761874",
            changePercent24Hr: "0.0139636869853852"

        },
        {
            symbol: "SOL",
            priceUsd: "182.0717581022814142",
            changePercent24Hr: "-14.0451497633219732"
        },
        {
            symbol: "XRP",
            priceUsd: "1.0861040549891727",
            changePercent24Hr: "-3.8228157386158285"
        }
     ]
    @Load({init: true})
    public async loadCoins() {
      this.coinList.replace(this.coins);
    }

   public formatPercentage(value:number) {
       return this.percentageFormatter.format(value);
   }

    public format(value: number) {
        return new Intl.NumberFormat("en-US",
             {
                style: 'currency',
                currency: 'USD',
                minimumIntegerDigits:2
             }).format(value);
    }

}

In the above ViewModel, we have loaded the data source in loadCoins method.If init is true, method will be executed when ViewModel is initialized.The difference in the current market value is shown as percentage; column named 'Change', the currency under Price column is formatted using Intl.NumberFormat.

Binding data to the XFListView.

To bind the data source of the XFListView, set the itemsSource property as shown as follows,The following code example binds the collection created in the ViewModel. Using the one way bindings and Math.sign function of JavaScript to check the number is positive or negative.

export default class XFCoinWatch extends AtomXFContentPage {

    public viewModel: XFCoinWatchViewModel;

    public create() {
        this.viewModel = this.resolve(XFCoinWatchViewModel);
        this.render(<XF.ContentPage 
                               title="Coin Watch" 
                               backgroundColor="White">
            <XF.Grid margin="5"
               rowDefinitions="auto,*" columnDefinitions="*">
                   <XF.StackLayout
                        backgroundColor="#34395A"
                        horizontalOptions="FillAndExpand">
                        <XF.Label
                            horizontalTextAlignment="Center"
                            horizontalOptions="CenterAndExpand"
                            text="Coin Watch"
                            fontAttributes="Bold"
                            fontSize="28"
                            padding="10"
                            textColor="White"/>
                    </XF.StackLayout>
                    <XF.Grid backgroundColor="lightgray" { ... XF.Grid.row(1)}>
                        <XF.Grid rowDefinitions="auto,*" columnDefinitions="*">
                            <XF.Grid
                                columnDefinitions="*,150,100"
                                backgroundColor="#f04726"
                                heightRequest="30"
                                padding="2">
                                <XF.Label
                                    { ... XF.Grid.column(0)}
                                    horizontalTextAlignment="Start"
                                    fontAttributes="bold"
                                    padding="5"
                                    text="Coin Name"
                                    textColor="White" />
                                <XF.Label
                                    { ... XF.Grid.column(1)}
                                    horizontalTextAlignment="End"
                                    padding="0,5,50,0"
                                    fontAttributes="bold"
                                    text="Price"
                                    textColor="White"/>
                                <XF.Label
                                    { ... XF.Grid.column(2)}
                                    horizontalTextAlignment="Start"
                                    fontAttributes="bold"
                                    padding="5"
                                    text="Change"
                                    textColor="White" />
                            </XF.Grid>
                            <XF.Grid
                                { ... XF.Grid.row(1)}>
                                <XF.ListView itemsSource={Bind.oneWay(() => this.viewModel.coinList)}>
                                    <XF.ListView.itemTemplate>
                                        <XF.DataTemplate>
                                            <XF.ViewCell>
                                                <XF.Grid
                                                    columnDefinitions="*,150,100"
                                                    padding="5"
                                                    backgroundColor={Colors.white}>
                                                    <XF.Label
                                                        { ... XF.Grid.column(0)}
                                                        padding="5"
                                                        fontAttributes="bold"
                                                        textColor={Colors.black}
                                                        text={Bind.oneWay((x) => x.data.symbol)}/>
                                                    <XF.Label
                                                    { ... XF.Grid.column(1)}
                                                        padding="5,5,40,0"
                                                        fontAttributes="bold"
                                                        horizontalOptions="End"
                                                        horizontalTextAlignment="End"
                                                        textColor={Colors.black}
                                                        text={Bind.oneWay((x) => this.viewModel.format(x.data.priceUsd))} />
                                                    <XF.Frame   { ... XF.Grid.column(2)}
                                                        padding="1"
                                                        cornerRadius="5"
                                                        hasShadow="false"
                                                        backgroundColor={Bind.oneWay((x) => Math.sign(x.data.changePercent24Hr) === -1 ? "#FFE1e1": "#EDF9f5")}
                                                        borderColor={Bind.oneWay((x) => Math.sign(x.data.changePercent24Hr) === -1 ? "#F76868": "#64CAAA")}>
                                                    <XF.StackLayout orientation="Horizontal"
                                                                padding="5">
                                                        <XF.Image horizontalOptions="Center">
                                                            <XF.Image.source>
                                                                <XF.FontImageSource
                                                                    color={Bind.oneWay((x) => Math.sign(x.data.changePercent24Hr) === -1 ? "#ff7060": "#41ba86")}
                                                                    size={20}
                                                                    fontFamily={FontAwesomeSolid}
                                                                    glyph={Bind.oneWay((x) => Math.sign(x.data.changePercent24Hr) === -1 ?
                                                                        FontAwesomeSolid.caretDown:
                                                                        FontAwesomeSolid.caretUp)}
                                                                    />
                                                            </XF.Image.source>
                                                        </XF.Image>
                                                        <XF.Label
                                                            horizontalOptions="EndAndExpand"
                                                            horizontalTextAlignment="Center"
                                                            fontAttributes="Bold"
                                                            padding="0,0,10,0"
                                                            textColor={Bind.oneWay((x) => Math.sign(x.data.changePercent24Hr) ===-1 ? "#F76868": "#64CAAA")}
                                                            text={Bind.oneWay((x) => this.viewModel.formatPercentage(x.data.changePercent24Hr) )}>
                                                        </XF.Label>
                                                    </XF.StackLayout>
                                                 </XF.Frame>
                                           </XF.Grid>
                                        </XF.ViewCell>
                                        </XF.DataTemplate>
                                    </XF.ListView.itemTemplate>
                                </XF.ListView>
                            </XF.Grid>
                    </XF.Grid>
                </XF.Grid>
            </XF.Grid>
        </XF.ContentPage>);
    }
}

See Also

WebAtoms Xamarin Forms Fiddle.
Full code example.

BY Ajay Singh
LikeCommentSave
LikeCommentSaveShare
0
Categories
General
YantraJS
Developer Guides
Tutorials
Web Atoms Updates

POPULAR POSTS
17 Mar 2021
LATEST ACTIVITY
Simmi Kava
commented this post.
Simmi Kava
liked this post.
Show more
ARCHIVES
2024
2023
2022
2021
TAGS
javascript (56)
developer (25)
javascriptdeveloper (16)
Xamarin.Forms (16)
Html (14)
typescript (12)
webatoms (12)
xamarin (11)
coding (10)
web-atoms (10)
arrays (9)
android (8)
javascript-developer (8)
csharp (7)
dotnet (7)
css (6)
update (6)
dotnet-standard (5)
function (5)
iOS (5)
methods (4)




Web Atoms: JSX (TSX + TypeScript) for Xamarin.Forms, Hot Reload Your App in Production Environment

PlaygroundSamples Repository