微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何让符号和价格单独显示在 xamarin c# 的列表视图中

如何解决如何让符号和价格单独显示在 xamarin c# 的列表视图中

如何在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();
        }


    }
}


解决方法

首先,您需要将来自 Web 服务的 json 反序列化到 C# 类中。您可以使用 json2csharp.com 来做到这一点

public class Stock
{
    public string symbol { get; set; }
    public string price { get; set; }
}

然后在您的 GetPrice 方法中

var response = await client.GetStringAsync("https://api.binance.com/api/v3/ticker/price");
var stocks = JsonConvert.DeserializeObject<List<Stock>>(response);

接下来,将 ListView 添加到您的 XAML

<ListView x:Name="stockList">
  <ListView.ItemTemplate>
    <DataTemplate>
       <ViewCell>
          <StackLayout>
            <Label Text="{Binding symbol}" />
            <Label Text="{Binding price}" />
          </StackLayout>
       </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>
   

最后,将您的数据分配给 ListView 中的 GetPrice

stockList.ItemsSource = stocks 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。