How to update price of cryptocurrencys from binance.

cuong nguyen 1 Reputation point
2021-07-16T06:17:35.61+00:00

Hi Everyone.
I have issue with updating price from BinanceAPI. I am using Xamarin.

<ScrollView HeightRequest="300">
            <ListView x:Name="listView">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell >
                            <StackLayout Orientation="Horizontal" >
                                <Image  Source="{Binding Image}"></Image>
                                <Label  Text="{Binding Symbol}"
                               FontSize="24"
                               VerticalOptions="Center" />
                                <Label  Text="{Binding Price}" 
                               FontSize="24"
                               VerticalOptions="Center" />
                                <Button Text="Click me"></Button>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </ScrollView>

Code C#:

ObservableCollection<InForOfCrypto> myList = new ObservableCollection<InForOfCrypto>();
            string url = "https://icons.iconarchive.com/icons/cjdowner/cryptocurrency/256/Ethereum-icon.png";
            List<PRICE> listOfPrice = CallPriceAPIFromBinnces();
            for (int i = 0; i < 15; i++)
            {
                myList.Add(new InForOfCrypto() { Image = url, Symbol = listOfPrice[i].symbol, Price = listOfPrice[i].price });
            }

            listView.ItemsSource = myList;

I call binance api it will return list of price of pair crypto all 5s it will auto call to api.
And now I only want to update to price field in list and other fields still maintain.

Thank you

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,354 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 75,661 Reputation points Microsoft Vendor
    2021-07-16T09:31:24.543+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    If you want to change it at runtime, use MVVM is a good choose.

    Here is my edited layout. I bind a Command to Button.

       <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
                    x:Class="MyTabbedPaged.Views.Page2">  
           <ContentPage.Content>  
               <ScrollView HeightRequest="300">  
                   <ListView x:Name="listView" ItemsSource="{Binding myList}">  
                       <ListView.ItemTemplate>  
                           <DataTemplate>  
                               <ViewCell >  
                                   <StackLayout Orientation="Horizontal"  >  
                                       <Image  Source="{Binding Image}"></Image>  
                                       <Label  Text="{Binding Symbol}"  
                                      FontSize="24"  
                                      VerticalOptions="Center" />  
                                       <Label  Text="{Binding Price}"   
                                      FontSize="24"  
                                      VerticalOptions="Center" />  
                                       <Button Text="Click me" Command="{Binding Path=BindingContext.ChangeCommand, Source={x:Reference Name=listView}}" CommandParameter="{Binding .}"></Button>  
                                   </StackLayout>  
                               </ViewCell>  
                           </DataTemplate>  
                       </ListView.ItemTemplate>  
                   </ListView>  
               </ScrollView>  
           </ContentPage.Content>  
       </ContentPage>  
    

    Here is background code. If you want to update data at runtime, you should achieve INotifyPropertyChanged for InForOfCrypto.cs. Then you click the Button, the ChangeCommand will be executed. change the code like following command.

       ChangeCommand = new Command<InForOfCrypto>(async (key) =>  
       			{  
       				InForOfCrypto inForOfCrypto = key as InForOfCrypto;  
       				inForOfCrypto.Price = "111111111111111";  
       			});  
    

    Here is layout background code

       using System;  
       using System.Collections.Generic;  
       using System.Collections.ObjectModel;  
       using System.ComponentModel;  
       using System.Linq;  
       using System.Text;  
       using System.Threading.Tasks;  
       using System.Windows.Input;  
       using Xamarin.Forms;  
       using Xamarin.Forms.Xaml;  
         
       namespace MyTabbedPaged.Views  
       {  
       	[XamlCompilation(XamlCompilationOptions.Compile)]  
       	public partial class Page2 : ContentPage  
       	{  
       		public Page2 ()  
       		{  
       			InitializeComponent ();  
         
       			BindingContext = new MyViewModel();  
         
       			//ObservableCollection<InForOfCrypto> myList = new ObservableCollection<InForOfCrypto>();  
       			//string url = "https://icons.iconarchive.com/icons/cjdowner/cryptocurrency/256/Ethereum-icon.png";  
       			//List<PRICE> listOfPrice = CallPriceAPIFromBinnces();  
       			//for (int i = 0; i < 15; i++)  
       			//{  
       			//	myList.Add(new InForOfCrypto() { Image = url, Symbol = listOfPrice[i].symbol, Price = listOfPrice[i].price });  
       			//}  
       			//listView.ItemsSource = myList;  
       		}  
         
       	}  
         
         
       	public class MyViewModel  
           {  
       		public ICommand ChangeCommand { protected set; get; }  
         
       		public ObservableCollection<InForOfCrypto> myList { get; set; }  
       		public MyViewModel()  
       		{  
       			myList = new ObservableCollection<InForOfCrypto>();  
       			string url = "https://icons.iconarchive.com/icons/cjdowner/cryptocurrency/256/Ethereum-icon.png";  
       			List<PRICE> listOfPrice = CallPriceAPIFromBinnces();  
       			for (int i = 0; i < 15; i++)  
       			{  
       				myList.Add(new InForOfCrypto() { Image = url, Symbol = listOfPrice[i].symbol, Price = listOfPrice[i].price });  
       			}  
         
       			ChangeCommand = new Command<InForOfCrypto>(async (key) =>  
       			{  
       				InForOfCrypto inForOfCrypto = key as InForOfCrypto;  
       				inForOfCrypto.Price = "111111111111111";  
       			});  
       	    }  
         
               private List<PRICE> CallPriceAPIFromBinnces()  
               {  
       			// If you get data from api from here, I add some data to test  
         
       			var list = new List<PRICE>();  
       			list.Add(new PRICE() { price = "1", symbol = "23" });  
       			list.Add(new PRICE() { price = "2", symbol = "234" });  
       			list.Add(new PRICE() { price = "1", symbol = "23" });  
       			list.Add(new PRICE() { price = "2", symbol = "234" });  
       			list.Add(new PRICE() { price = "1", symbol = "23" });  
       			list.Add(new PRICE() { price = "2", symbol = "234" });  
       			list.Add(new PRICE() { price = "1", symbol = "23" });  
       			list.Add(new PRICE() { price = "2", symbol = "234" });  
       			list.Add(new PRICE() { price = "1", symbol = "23" });  
       			list.Add(new PRICE() { price = "2", symbol = "234" });  
       			list.Add(new PRICE() { price = "1", symbol = "23" });  
       			list.Add(new PRICE() { price = "2", symbol = "234" });  
       			list.Add(new PRICE() { price = "1", symbol = "23" });  
       			list.Add(new PRICE() { price = "2", symbol = "234" });  
       			list.Add(new PRICE() { price = "1", symbol = "23" });  
       			list.Add(new PRICE() { price = "2", symbol = "234" });  
       			return list;  
       		}  
       	}  
         
       	public class InForOfCrypto: INotifyPropertyChanged  
       	{  
            //   public string Image { get; set; }  
       		//public string Symbol { get; set; }  
       		//public string Price { get; set; }  
         
       		string _price;  
       		public string Price  
       		{  
       			set  
       			{  
       				if (_price != value)  
       				{  
       					_price = value;  
       					OnPropertyChanged("Price");  
         
       				}  
       			}  
       			get  
       			{  
       				return _price;  
       			}  
       		}  
         
         
       		string _symbol;  
       		public string Symbol  
       		{  
       			set  
       			{  
       				if (_symbol != value)  
       				{  
       					_symbol = value;  
       					OnPropertyChanged("Symbol");  
         
       				}  
       			}  
       			get  
       			{  
       				return _symbol;  
       			}  
       		}  
         
       		string _image;  
       		public string Image  
       		{  
       			set  
       			{  
       				if (_image != value)  
       				{  
       					_image = value;  
       					OnPropertyChanged("Image");  
         
       				}  
       			}  
       			get  
       			{  
       				return _image;  
       			}  
       		}  
         
       		public event PropertyChangedEventHandler PropertyChanged;  
         
       		protected virtual void OnPropertyChanged(string propertyName)  
       		{  
       			PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
       		}  
         
       	}  
         
         
       	public class PRICE  
       	{  
       		public string symbol { get; set; }  
       		public string price { get; set; }  
       	}  
       }  
    

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.