【发布时间】:2021-07-18 13:23:42
【问题描述】:
如何在 xamrain c# 中获取一个标签中的符号和另一个标签中的价格 我需要帮助的是如何使符号和价格在所有不同硬币的列表视图中一起出现。您可以在 GetPrice 下的链接中看到不同的符号和不同的价格。我只想将它们分组并将它们分隔在列表视图中。请帮帮我。
这是我的 MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Crypto_Portfolio.MainPage">
<StackLayout>
<StackLayout Orientation="Horizontal" VerticalOptions="Start">
<ListView x:Name="priceList">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding symbol}" />
<Label Text="{Binding price}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
<StackLayout Orientation="Horizontal" VerticalOptions="End">
<Button Clicked="priceClick" Text="Fetch Price" HorizontalOptions="CenterAndExpand" VerticalOptions="End"/>
</StackLayout>
</StackLayout>
</ContentPage>
这是我的 MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net.Http;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Xamarin.Forms;
namespace Crypto_Portfolio
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
GetPrice();
}
public class Crypto
{
public string symbol { get; set; }
public string price { get; set; }
}
private async void GetPrice()
{
HttpClient client = new HttpClient();
var response = await client.GetStringAsync("https://api.binance.com/api/v3/ticker/price");
var cryptoconverted = JsonConvert.DeserializeObject<List<Crypto>>(response);
priceList.ItemsSource = cryptoconverted;
}
void priceClick(object sender, EventArgs e)
{
GetPrice();
}
}
}
【问题讨论】:
-
你的代码中没有ListView,只有一个标签。您希望我们为您编写所有这些内容吗?
-
“使符号和价格在所有不同硬币的列表视图中一起出现”到底是什么意思。 ?
-
@Jason 抱歉,我是新手。我不知道该怎么做。
-
@Cfun 您可以在 GetPrice 下的链接中看到不同的符号和不同的价格。我只想将它们分组并在列表视图中分开。
-
Xamarin 网站上有很多优秀的教程,包括多个完整的示例应用程序。我建议您花时间查看可用的内容