如何从Xamarin的binance更新加密货币的价格



我对BinanceAPI的更新价格有问题。我正在使用Xamarin。

<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。

现在我只想更新列表中的价格字段和其他字段。

您可以使用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));
}
} 

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

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

最新更新