如何从 xaramin 中的币安更新加密货币的价格

时间:2021-07-16 08:23:23

标签: c# xamarin xamarin.forms

我在从 BinanceAPI 更新价格时遇到问题。我正在使用 Xaramin。

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

代码 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;

我调用 Binance api 它将返回所有 5 对加密的价格列表,它将自动调用 api。

现在我只想更新到列表中的价格字段,其他字段仍然保持。

1 个答案:

答案 0 :(得分:0)

您可以使用 INotifyPropertyChanged 进行更新。

public class ViewModel : INotifyPropertyChanged
{
    private string _price;

    public string Price
    {
        get { return _price; }
        set
        {
            _price= value;
            OnPropertyChanged("Price");
        }
    } 

  public event PropertyChangedEventHandler PropertyChanged;
   protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
   {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
   }
 } 

然后重置价格值进行更新。

或者您可以更新列表中的价格值,然后重置项目来源。